Skip to content

Introduce Unnormalized wrapper#155083

Open
adwinwhite wants to merge 3 commits intorust-lang:mainfrom
adwinwhite:introduce-unnormalized
Open

Introduce Unnormalized wrapper#155083
adwinwhite wants to merge 3 commits intorust-lang:mainfrom
adwinwhite:introduce-unnormalized

Conversation

@adwinwhite
Copy link
Copy Markdown
Contributor

@adwinwhite adwinwhite commented Apr 10, 2026

View all comments

This is the first step of the eager normalization series.

This PR introduce an Unnormalized wrapper and make most normalization routines consume it. The purpose is to make normalization explicit.
This PR contains no behavior change.

API changes are in the first two commit.
There're some normalization routines left untouched:

  • normalize in the type checker of borrowck: better do it together with field.ty() returning Unnormalized.
  • normalize_with_depth: only used inside the old solver. Can be done later.
  • query_normalize: rarely used.
  • misc local normalization helpers.

The compiler errors are mostly fixed via ast-grep, with exceptions handled manually.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations 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) labels Apr 10, 2026
@adwinwhite
Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 10, 2026
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 10, 2026
@rust-log-analyzer

This comment has been minimized.

@adwinwhite
Copy link
Copy Markdown
Contributor Author

@bors try cancel

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 10, 2026

Try build cancelled. Cancelled workflows:

@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label Apr 10, 2026
@adwinwhite adwinwhite force-pushed the introduce-unnormalized branch from 309b0de to a616210 Compare April 10, 2026 07:51
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 10, 2026
@adwinwhite
Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 10, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 10, 2026

☀️ Try build successful (CI)
Build commit: 10d0ef4 (10d0ef4e7995ca1d162f402be9f0b39b15d7a78a, parent: 7659cec4ed1457bc0d1f636127e66e8fe5008928)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (10d0ef4): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -2.4%, secondary -6.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
-6.4% [-6.4%, -6.4%] 1
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Cycles

Results (secondary -0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.1%, 2.7%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-3.8%, -3.6%] 2
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 492.281s -> 489.321s (-0.60%)
Artifact size: 395.57 MiB -> 395.56 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 10, 2026
@adwinwhite adwinwhite force-pushed the introduce-unnormalized branch from 3b64c2b to 5407a88 Compare April 10, 2026 13:37
@adwinwhite
Copy link
Copy Markdown
Contributor Author

r? @lcnr

Copy link
Copy Markdown
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.

Things I'd like to see in this PR maybe?

  • TypeErrCtxt::normalize_fn_sig should take Unnormalized. that should simplify the diff
  • explicit_item_self_bounds is an iterator over Unnormalized<(Whatever, Span)>, it should also have the span outside of it

Things for future PRs

  • I dislike .map(|(c, s)| (c.skip_norm_wip(), s)) as a pattern, we should skip_norm_wip once we iterate over the collection
  • all of the t == cx.tcx.normalize_erasing_regions(cx.typing_env(), t) asserts should be some assert_fully_normalized and don't use Unnormalized::new_wip
  • a bunch of these normalize calls in codegen are unnecessary. we should change them to use assert_fully_normalized instead
  • a big one is struct_tail_raw whose normalize should take an Unnormalized
  • all calls to skip_norm_wip().def_id should be replaced with a def_id() function on Unnormalized<Whateverhasadefid>
  • we should add fn Unnormalized::<T>::map<U>(self, f: impl FnOnce(T) -> U) -> Unnormalized<U>
  • unsure what to do in type_of and predicates_of as these queries very intentionally ferry unnormalized types around. I guess we just have a .skip_normalization() for places where it's intentional 🤔
  • also slightly unsure about elaborate, I think it should take Unnormalized as it also just returns unnormalized things?
  • field.ty should return Unnormalized
  • normalize_with_depth_to should take Unnormalized 🤔
  • coherence::ImplHeader should its things be Unnormalized I think?
  • DropckConstraint should have Unnormalized fields? 🤔
  • the DeeplyNormalize QueryTypeOp should contain an Unnormalized<T> as input

View changes since this review

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 15, 2026

reviewed all changes, and would like to merge this soon 😁 makes it easier to work off it

@adwinwhite
Copy link
Copy Markdown
Contributor Author

Do we need to make the normalze method in borrowck's type checker consume Unnormalized in this PR? Ask because it's not in the final summary comment. 🤔
I leave it out because it would create a smaller diff if it's done with field.ty api change.

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 16, 2026

fine to do it later 👍

Comment thread compiler/rustc_type_ir/src/unnormalized.rs
@adwinwhite
Copy link
Copy Markdown
Contributor Author

explicit_item_self_bounds is an iterator over Unnormalized<(Whatever, Span)>, it should also have the span outside of it

explicit_item_self_bounds and other predicates_of variants return EarlyBinder. Their iter_instantiated(_copied) call can't be changed to return (Unnormalized<...>, Span).

Other than that, I should've resolved all review comments.

@rust-bors

This comment has been minimized.

@adwinwhite adwinwhite force-pushed the introduce-unnormalized branch from 177d0f9 to a9214f5 Compare April 17, 2026 06:48
@rustbot

This comment has been minimized.

Comment on lines +217 to +218
let mut hybrid_preds: Vec<_> =
impl_predicates.instantiate_identity(tcx).predicates.into_iter().collect();
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 17, 2026

Choose a reason for hiding this comment

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

? what is this doing

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, sorry, I reordered the code a bit. This collect isn't needed anymore.

Comment thread compiler/rustc_hir_typeck/src/expr.rs Outdated
Comment on lines +3561 to +3572
let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
let impl_trait_ref =
self.tcx.impl_trait_ref(impl_def_id).instantiate(self.tcx, impl_args);
let impl_trait_ref = self
.tcx
.impl_trait_ref(impl_def_id)
.instantiate(self.tcx, impl_args)
.skip_norm_wip();
let cause = self.misc(base_expr.span);

// Match the impl self type against the base ty. If this fails,
// we just skip this impl, since it's not particularly useful.
let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
let impl_trait_ref =
ocx.normalize(&cause, self.param_env, Unnormalized::new_wip(impl_trait_ref));
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 17, 2026

Choose a reason for hiding this comment

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

that one is just skip_Norm_wip into new_wip

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.

Comment thread compiler/rustc_lint/src/multiple_supertrait_upcastable.rs
Comment thread compiler/rustc_hir_typeck/src/expr.rs Outdated
Comment on lines +3561 to +3572
let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
let impl_trait_ref =
self.tcx.impl_trait_ref(impl_def_id).instantiate(self.tcx, impl_args);
let impl_trait_ref = self
.tcx
.impl_trait_ref(impl_def_id)
.instantiate(self.tcx, impl_args)
.skip_norm_wip();
let cause = self.misc(base_expr.span);

// Match the impl self type against the base ty. If this fails,
// we just skip this impl, since it's not particularly useful.
let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
let impl_trait_ref =
ocx.normalize(&cause, self.param_env, Unnormalized::new_wip(impl_trait_ref));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.

Comment on lines +12 to +23
/// A wrapper for value that needs normalization.
/// FIXME: This is very WIP. The plan is to replace the `skip_norm_wip` spread
/// throughout the codebase with proper normalization. This is the first step toward
/// switching to eager normalization with the next solver. See the normalization refactor plan
/// [here](https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/Eager.20normalization.2C.20ahoy.21/with/582996293)
/// We're in a weird intermediate state due to the change is big to land in a single PR.
/// A lot of active work is going on which is tracked in #155345.
/// While this is in progress, if you encounter this type, just use `Unnormalized::new_wip`
/// and `Unnormalized::skip_norm_wip` as needed.
///
/// The interner type parameter exists to constraint generic for certain impl, e.g.,
/// `Unnormalized<I, I::Clause>`.
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 17, 2026

Choose a reason for hiding this comment

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

Suggested change
/// A wrapper for value that needs normalization.
/// FIXME: This is very WIP. The plan is to replace the `skip_norm_wip` spread
/// throughout the codebase with proper normalization. This is the first step toward
/// switching to eager normalization with the next solver. See the normalization refactor plan
/// [here](https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/Eager.20normalization.2C.20ahoy.21/with/582996293)
/// We're in a weird intermediate state due to the change is big to land in a single PR.
/// A lot of active work is going on which is tracked in #155345.
/// While this is in progress, if you encounter this type, just use `Unnormalized::new_wip`
/// and `Unnormalized::skip_norm_wip` as needed.
///
/// The interner type parameter exists to constraint generic for certain impl, e.g.,
/// `Unnormalized<I, I::Clause>`.
/// A wrapper for values that need normalization.
///
/// FIXME(#155345): This is very WIP. The plan is to replace the `skip_norm_wip`
/// spread throughout the codebase with proper normalization. This is the first
/// step toward switching to eager normalization with the next solver. See the
/// normalization refactor plan [here].
///
/// We're in a weird intermediate state as the change is too big to land in a
/// single PR. While this work is in progress, just use `Unnormalized::new_wip`
/// and `Unnormalized::skip_norm_wip` as needed.
///
/// The interner type parameter exists to constraint generic for certain impl,
/// e.g., `Unnormalized<I, I::Clause>`.
///
/// [here]: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/Eager.20normalization.2C.20ahoy.21/with/582996293

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.explicit_super_predicates_of(def_id)
                .iter_identity_copied()
                .map(Unnormalized::skip_norm_wip)

the skipping above is needed since iter_identitidy_copied returns iterator over Unnoramlized<(..., span)> while the next method expects pair tuple.

Comment thread src/librustdoc/clean/mod.rs Outdated
let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, cx.tcx.types.usize);
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, *def_id);
let ct = cx.tcx.normalize_erasing_regions(typing_env, ct);
let ct = cx.tcx.normalize_erasing_regions(typing_env, Unnormalized::new(ct));
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 17, 2026

Choose a reason for hiding this comment

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

Suggested change
let ct = cx.tcx.normalize_erasing_regions(typing_env, Unnormalized::new(ct));
let ct = cx.tcx.normalize_erasing_regions(typing_env, Unnormalized::new_wip(ct));

so for the other uses in this file

View changes since the review

@@ -381,7 +382,7 @@ impl<'tcx> NonCopyConst<'tcx> {
) -> bool {
// Make sure to instantiate all types coming from `typeck` with `gen_args`.
let ty = EarlyBinder::bind(typeck.expr_ty(e)).instantiate(tcx, gen_args);
let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty.skip_normalization());
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 17, 2026

Choose a reason for hiding this comment

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

Suggested change
let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty.skip_normalization());
let ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty.skip_norm_wip());

View changes since the review

Copy link
Copy Markdown
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.

final nit pls cleanup commit history, then r=me

View changes since this review

Comment thread compiler/rustc_lint/src/multiple_supertrait_upcastable.rs
@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 17, 2026

@bors rollup=never

@adwinwhite
Copy link
Copy Markdown
Contributor Author

adwinwhite commented Apr 17, 2026

final nit pls cleanup commit history

Should I just squash or isolate the API change in one commit for this big PR?

Also unsure why I can't reply to single inline review. If you missed it,
skip_norm_wip after explicit_super_predicates_of is unavoidable.
See comment

@rust-bors

This comment has been minimized.

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 17, 2026

Should I just squash or isolate the API change in one commit for this big PR?

whatever u prefer, as long as the "address review" commits are gone :3

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Apr 17, 2026

r=me

@adwinwhite adwinwhite force-pushed the introduce-unnormalized branch from c894a60 to a73a9c7 Compare April 17, 2026 17:41
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@adwinwhite
Copy link
Copy Markdown
Contributor Author

@bors r=lcnr

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

📌 Commit a73a9c7 has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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 Apr 17, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 18, 2026

☔ The latest upstream changes (presumably #155404) made this pull request unmergeable. Please resolve the merge conflicts.

This pull request was unapproved.

@rust-bors rust-bors bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-types-nominated Nominated for discussion during a types team meeting. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. 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.

7 participants