Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce CoercePointeeWellformed for coherence checks at typeck stage #136107

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

Fix #135206

This is the first PR to introduce the "wellformedness" check for derive(CoercePointee).

This patch introduces a new error code to cover all the prerequisites of the said macro. The checks that is enforced with this patch is whether the data is indeed struct and whether the layout is set to repr(transparent).

A following series of patch will arrive later to address the following concern.

  1. CoercePointee leaks unstable unsizing impls #135217 so that we would only admit one single coercion on one type parameter, and leave the rest for future consideration in tandem of development of other coercion rules.
  2. Enforcement of data field requirements.

An open question is whether there is a good schema to encode the #[pointee] as well, so that we could also check if the #[pointee] type parameter is indeed ?Sized.

@rustbot label F-derive_coerce_pointee

@rustbot
Copy link
Collaborator

rustbot commented Jan 26, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 26, 2025

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

@rustbot rustbot added T-libs Relevant to the library team, which will review and decide on the PR/issue. F-derive_coerce_pointee Feature: RFC 3621's oft-renamed implementation labels Jan 26, 2025
@rust-log-analyzer

This comment has been minimized.

};
let did = def.did();
let span =
if let Some(local) = did.as_local() { tcx.source_span(local) } else { tcx.def_span(did) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this as_local check?

The def id always should be local, right? if it's not, then it's an incoherent impl and we should probably just delay a bug.

};
let did = def.did();
let span =
if let Some(local) = did.as_local() { tcx.source_span(local) } else { tcx.def_span(did) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use source_span -- the query says:

. This span is meant for dep-tracking rather than diagnostics. It should not be used outside of rustc_middle::hir::source_map.

@@ -790,3 +794,27 @@ fn visit_implementation_of_pointer_like(checker: &Checker<'_>) -> Result<(), Err
.with_note(why_disqualified)
.emit())
}

fn visit_implementation_of_coerce_pointee_wellformed(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we still doing validation inside of the proc macro? Shouldn't we remove the validation from the proc macro since we are doing it here now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking from another perspective, rejecting the derivation earlier as well means that we can refuse to generate the unnecessary ASTs that later compilation passes needs to process. Given the current state of the unstable features involved, it seems to me that it could probably be better for us to intercept inadmissible uses of the macro so that we would not inadvertently expose the behaviour of those unstable features that we had not committed to yet.

I am honestly not sure. This can go either way to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we should keep both validation. Please remove it :)


#[cfg(not(bootstrap))]
#[lang = "coerce_pointee_wellformed"]
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be gated behind a different feature gate, since users should never be able to implement CoercePointeeWellFormed manually. You can follow StructuralPartialEq to see how to gate an internal derive-only trait properly.

@@ -110,6 +110,52 @@ pub(crate) fn expand_deriving_coerce_pointee(
// Declare helper function that adds implementation blocks.
// FIXME(dingxiangfei2009): Investigate the set of attributes on target struct to be propagated to impls
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span),];
// # Wellformed-ness assertion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate what well-formedness means.

I think you may also want to strongly consider using a different term than "well-formed". We already use the term within the type system for a different meaning. I think it's more correct to call this something like "DeriveValidity" or something, to make it clear that we use this impl to validate the correcness of the derive.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs fixing?

/// and shall not ever be stabilised.
#[cfg(not(bootstrap))]
#[lang = "coerce_pointee_validated"]
#[unstable(feature = "coerce_pointee_validated", issue = "123430")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a tracking issue since it's never going to be stabilized. Just put issue = "none" I think.

@dingxiangfei2009 dingxiangfei2009 force-pushed the coerce-pointee-wellformed branch from 4c42131 to 645b231 Compare February 9, 2025 12:44
@rust-log-analyzer

This comment has been minimized.

@dingxiangfei2009 dingxiangfei2009 force-pushed the coerce-pointee-wellformed branch from 645b231 to e55e437 Compare February 9, 2025 16:09
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Feb 9, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

compiler-errors commented Feb 9, 2025

I'm a bit confused why this PR has gotten so large. It's now introducing a query, interacting with specialization graph somehow, and doing a lot more than it originally set out to do, which as far as I could tell is just a check for the pointee ADT being transparent.

I think you should revert most of this PR and explain more clearly in a follow-up issue or zulip thread what you intend to achieve with this validation before committing something that is very difficult to review 😓

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2025
@dingxiangfei2009
Copy link
Contributor Author

@compiler-errors Thank you for reviewing! I am going to truncate the commit logs and focus only on the repr(transparent) validation first.

I am moving the discussion to Zulip.

@dingxiangfei2009 dingxiangfei2009 force-pushed the coerce-pointee-wellformed branch from 5324992 to aea5595 Compare February 9, 2025 20:39
@dingxiangfei2009
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-derive_coerce_pointee Feature: RFC 3621's oft-renamed implementation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
5 participants