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

TransmuteFrom: Gracefully handle unnormalized types and normalization errors #131112

Merged
merged 2 commits into from
Oct 4, 2024

Conversation

jswrenn
Copy link
Member

@jswrenn jswrenn commented Oct 1, 2024

Refactor to share code between TransmuteFrom's trait selection and error reporting code paths. Additionally normalizes the source and destination types, and gracefully handles normalization errors.

Fixes #130413

r​? @compiler-errors

@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2024

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
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 Oct 1, 2024
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.

So I think this PR is overengineered 😅 I left some inline comments, but realized that they're kinda ignoring the fact that this isn't the right general fix, I think.

Factoring out the confirmation code may be useful, but right now it's tied together with unnecessary/incorrect normalization calls which mask the real issue here:

The correct fix is just to add checks in get_safe_transmute_error_and_reason for the invariants that the transmutability code currently relies on. Specifically, we should be checking the same things as we do in assemble_candidates_for_transmutability -- i.e.

        if obligation.predicate.has_non_region_param()

since right now, the transmutability code doesn't handle type/const parameters, and

        if obligation.has_non_region_infer()

which implies ambiguity.

use rustc_infer::infer::TyCtxtInferExt;

use crate::traits::ObligationCtxt;
let infcx = tcx.infer_ctxt().with_next_trait_solver(true).build();
Copy link
Member

Choose a reason for hiding this comment

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

Making a new inference context is definitely not the right approach.

compiler/rustc_trait_selection/src/transmute.rs Outdated Show resolved Hide resolved
compiler/rustc_trait_selection/src/transmute.rs Outdated Show resolved Hide resolved
@compiler-errors
Copy link
Member

Also unrelated, I believe the transmutability code should be resilient to NormalizationFailure layout errors. This PR doesn't, afaict, fix this ICE:

#![feature(transmutability)]

trait A {
    type AssocA;
}

trait B {
    type AssocB: std::mem::TransmuteFrom<()>;
}

impl<T> B for T
where
    for<'a> &'a i32: A,
{
    type AssocB = <&'static i32 as A>::AssocA;
}

@rust-log-analyzer

This comment has been minimized.

@@ -2221,6 +2224,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
) -> GetSafeTransmuteErrorAndReason {
use rustc_transmute::Answer;

if obligation.predicate.has_non_region_param() || obligation.has_non_region_infer() {
Copy link
Member

Choose a reason for hiding this comment

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

Could use a comment

where
T: A,
{
type AssocB = T::AssocA; //~ERROR: the trait bound `<T as A>::AssocA: TransmuteFrom<(), Assume { alignment: false, lifetimes: false, safety: false, validity: false }>` is not satisfied [E0277]
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm personally fine to punt on making this error message prettier — my top priority is making TransmuteFrom work — but happy to try to improve this further if you'd like to block the PR on it.

Copy link
Member

Choose a reason for hiding this comment

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

No, it's fine. But yeah, it could be customized to say something like "cannot transmute types with ty/ct params in it".

Copy link
Member

Choose a reason for hiding this comment

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

But happy to do that later.

@jswrenn
Copy link
Member Author

jswrenn commented Oct 1, 2024

@compiler-errors, you're right, that was a lot more simple: https://github.com/rust-lang/rust/pull/131112/files

@jswrenn jswrenn changed the title TransmuteFrom: normalize types, unify confirmation and error reporting TransmuteFrom: Gracefully handle unnormalized types and normalization errors Oct 1, 2024
@compiler-errors
Copy link
Member

Also please run ./x.py test crashes, you need to remove the relevant crashes tests.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

@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 Oct 3, 2024
@compiler-errors
Copy link
Member

I went ahead and removed the crashes and added that comment I asked for, since this is blocking (well, mostly conflicting with) #130950.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Oct 3, 2024

📌 Commit bc5f952 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 3, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 3, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#131024 (Don't give method suggestions when method probe fails due to bad implementation of `Deref`)
 - rust-lang#131112 (TransmuteFrom: Gracefully handle unnormalized types and normalization errors)
 - rust-lang#131176 (.gitignore files for nix)
 - rust-lang#131183 (Refactoring to `OpaqueTyOrigin`)
 - rust-lang#131187 (Avoid ICE in coverage builds with bad `#[coverage(..)]` attributes)
 - rust-lang#131192 (Handle `rustc_query_impl` cases of `rustc::potential_query_instability` lint)
 - rust-lang#131197 (Avoid emptiness check in `PeekMut::pop`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 33b4947 into rust-lang:master Oct 4, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 4, 2024
Rollup merge of rust-lang#131112 - jswrenn:fix-130413, r=compiler-errors

TransmuteFrom: Gracefully handle unnormalized types and normalization errors

~~Refactor to share code between `TransmuteFrom`'s trait selection and error reporting code paths. Additionally normalizes the source and destination types, and gracefully handles normalization errors.~~

Fixes rust-lang#130413

r​? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: 🌲 not implemented: NormalizationFailure(Alias(Projection, AliasTy...
6 participants