Skip to content

Structurally normalize types as needed in projection_ty_core #141703

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

Merged
merged 2 commits into from
May 30, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented May 28, 2025

Introduce a structurally_normalize callback to projection_ty_core, and then use it before we match on the ty kind in projection_ty_core.

Previously we were only structurally normalizing the return type of the handle_field struct, but if we were to (e.g.) apply a deref projection to that type, then the resulting type is not guaranteed to be structurally normalized and any subsequent projections applied would ICE.

Fixes rust-lang/trait-system-refactor-initiative#221

I'll leave a few comments inline to explain the changes.

r? lcnr


Also fixes #141708

@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 May 28, 2025
);
curr_projected_ty = projected_ty;
}
trace!(?curr_projected_ty);

let ty = curr_projected_ty.ty;
let ty = self.normalize(curr_projected_ty.ty, locations);
Copy link
Member Author

Choose a reason for hiding this comment

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

We have to normalize the final output type only in the old solver, since it's no longer guaranteed to be normalized.

Copy link
Member Author

Choose a reason for hiding this comment

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

This normalize call actually makes it easier to hit #141708, which is why CI was failing on icu_provider.

The bug report demonstrates that it's still possible to hit today, and it's fixed by the second commit which always normalizes the type for a.

Unfortunate, but necessary unless we wanted to (e.g.) eagerly normalize all closure signatures in writeback, which presumably has its own issues.

@@ -164,8 +168,9 @@ impl<'tcx> PlaceTy<'tcx> {
self,
tcx: TyCtxt<'tcx>,
elem: &ProjectionElem<V, T>,
mut handle_field: impl FnMut(&Self, FieldIdx, T) -> Ty<'tcx>,
mut handle_opaque_cast_and_subtype: impl FnMut(&Self, T) -> Ty<'tcx>,
Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit awkward for these callbacks to take a PlaceTy, since then we can't structurally normalize it without mutating it in place or something.

So I blew up the struct into its fields, which is most of the changes in this PR.

pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: FieldIdx) -> Ty<'tcx> {
if let Some(variant_index) = self.variant_index {
match *self.ty.kind() {
pub fn field_ty(
Copy link
Member Author

Choose a reason for hiding this comment

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

See comment below for why I split the self arg up into its fields.

PlaceTy::from_ty(handle_opaque_cast_and_subtype(&self, ty))
}
ProjectionElem::Field(f, fty) => PlaceTy::from_ty(handle_field(
structurally_normalize(self.ty),
Copy link
Member Author

Choose a reason for hiding this comment

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

See above, here we'd need to do something like:

{
    self.ty = structurally_normalize(self.ty);
    PlaceTy::from(handle_field(self, f, fty))
}

which I think is kinda awkward.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) label May 28, 2025
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member Author

A wonderful and preexisting bug in the compiler: #141708

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

r=me after nit

@compiler-errors
Copy link
Member Author

@bors r=lcnr

@bors
Copy link
Collaborator

bors commented May 29, 2025

📌 Commit 9871e16 has been approved by lcnr

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-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2025
bors added a commit that referenced this pull request May 30, 2025
Rollup of 5 pull requests

Successful merges:

 - #141703 (Structurally normalize types as needed in `projection_ty_core`)
 - #141719 (Add tls_model for cygwin and enable has_thread_local)
 - #141736 (resolve stage0 sysroot from rustc)
 - #141746 (Rework `#[doc(cfg(..))]` checks as distinct pass in rustdoc)
 - #141749 (Remove RUSTC_RETRY_LINKER_ON_SEGFAULT hack)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit bace7f9 into rust-lang:master May 30, 2025
9 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 30, 2025
rust-timer added a commit that referenced this pull request May 30, 2025
Rollup merge of #141703 - compiler-errors:deref-place, r=lcnr

Structurally normalize types as needed in `projection_ty_core`

Introduce a `structurally_normalize` callback to `projection_ty_core`, and then use it before we match on the ty kind in `projection_ty_core`.

Previously we were only structurally normalizing the return type of the `handle_field` struct, but if we were to (e.g.) apply a deref projection to that type, then the resulting type is not guaranteed to be structurally normalized and any subsequent projections applied would ICE.

Fixes rust-lang/trait-system-refactor-initiative#221

I'll leave a few comments inline to explain the changes.

r? lcnr

---

Also fixes #141708
@jhpratt
Copy link
Member

jhpratt commented Jun 7, 2025

still in the queue

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken MIR: relate_type_and_user_type may receive unnormalized types redscript regression
6 participants