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

@rust-log-analyzer

This comment has been minimized.

@adwinwhite adwinwhite force-pushed the introduce-unnormalized branch from 53bb180 to d9fa04c Compare April 15, 2026 09:27
Comment thread compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
{
self.normalize_with_category(value, location, ConstraintCategory::Boring)
self.normalize_with_category(
Unnormalized::new_wip(value),
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

not propagating this one out?

View changes since the review

where
T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx,
{
let value = value.inside_norm();
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
let value = value.inside_norm();
let value = value.skip_norm_wip();

would want us to propagate the Unnormalized into the type op and only discard it when actually normalizing

View changes since the review

Comment on lines +1762 to 1764
let unnormalized_ty =
tcx.type_of(static_def_id).instantiate_identity().skip_norm_wip();
let normalized_ty = self.normalize(unnormalized_ty, locations);
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

hmm, want to change TypeChecker::normalize to also take Unnormalized

View changes since the review

// restrictive than the trait's method (and the impl itself).
let impl_m_own_bounds = impl_m_predicates.instantiate_own_identity();
let impl_m_own_bounds =
impl_m_predicates.instantiate_own_identity().map(|(c, s)| (c.skip_norm_wip(), s));
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

that skip_norm_wip seems overeager given that we do actually just do ocx.normalize on that predicate right afer

View changes since the review

for (const_condition, span) in tcx
.const_conditions(impl_m.def_id)
.instantiate_own_identity()
.map(|(c, s)| (c.skip_norm_wip(), s))
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

continue;
}
if !tcx.is_dyn_compatible(trait_ref.def_id) {
if !tcx.is_dyn_compatible(trait_ref.probe(|v| v.def_id)) {
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

probe is an overloaded name in the type system 🤔 maybe .extract

I would not use this to get a def_id as I want that to have a separate function fn def_id. I don't like this as a final state here and unlike skip_norm_wip, it's harder to grep for

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.

yeah... I don't really like probe. Can you always do skip_norm_wf or skip_normalization in cases where we we know that we don't want to normalize

let (normalized_ty, obligations) =
self.structurally_normalize_ty(Ty::new_projection(tcx, trait_target_def_id, [ty]))?;
let (normalized_ty, obligations) = self.structurally_normalize_ty(
Unnormalized::new_wip(Ty::new_projection(tcx, trait_target_def_id, [ty])),
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
Unnormalized::new_wip(Ty::new_projection(tcx, trait_target_def_id, [ty])),
Unnormalized::new(Ty::new_projection(tcx, trait_target_def_id, [ty])),

very intentionally unnormalized :3

View changes since the review

&ObligationCause::dummy(),
param_env,
Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args),
Unnormalized::new_wip(Ty::new_projection_from_args(
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
Unnormalized::new_wip(Ty::new_projection_from_args(
Unnormalized::new(Ty::new_projection_from_args(

also very intentionally unnormalized

View changes since the review

cx.typing_env(),
Ty::new_projection_from_args(cx.tcx, target_id, cx.tcx.mk_args(&[GenericArg::from(indexed_ty)])),
)
&& let Ok(deref_ty) = cx.tcx.try_normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(Ty::new_projection_from_args(cx.tcx, target_id, cx.tcx.mk_args(&[GenericArg::from(indexed_ty)]))))
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

can you look through your changes to clippy and avoid breaking formatting as much in your diff? 😅 sorry

View changes since the review

// If is a Future
&& preds
.iter_instantiated_copied(cx.tcx, args)
&& preds.iter_instantiated_copied(cx.tcx, args).map(Unnormalized::skip_normalization)
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

what are the places that still use skip_normalization instead of skip_norm_wip?

View changes since the review

f(&self.value)
}

pub fn map_inner<F, U>(self, f: F) -> Unnormalized<I, U>
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

just call this map

View changes since the review

Comment on lines +49 to +53
/// FIXME: Should I just use `skip_normalization`?
/// Kinda weird to call that insides normalization methods.
pub fn inside_norm(self) -> T {
self.value
}
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

I would call that one skip_normalization and then it should be clear from context why we are 🤔

could imagine being more specific about why we're intentianally not normalizing, but I think for now it's fine for that to be (poorly) implied by the context

View changes since the review

pub fn instantiate_identity(self) -> T {
self.value
pub fn instantiate_identity(self) -> Unnormalized<I, T> {
Unnormalized::new(self.value)
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
Unnormalized::new(self.value)
// FIXME(#155345): In case the bound value was already normalized, this
// is unnecessary. We may want to track explicitly whether `EarlyBinder`
// contains something that has been normalized already.
//
// This is annoying, as e.g. `type_of` for opaque types is normalized,
// while `type_of` for free type aliases is not.
Unnormalized::new(self.value)

View changes since the review

Comment on lines +200 to +201
.map(Unnormalized::skip_norm_wip)
.filter_map(ty::Clause::as_trait_clause)
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
.map(Unnormalized::skip_norm_wip)
.filter_map(ty::Clause::as_trait_clause)
.map(Unnormalized::as_trait_clause)

View changes since the review

queue.extend(
tcx.explicit_super_predicates_of(trait_pred.def_id())
.iter_identity_copied()
.map(Unnormalized::skip_norm_wip)
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

explicit_super_predicates_of still does Unnormalized<(Clause, Span)>?

View changes since the review

let trait_ref = trait_ref.skip_norm_wip();
(
trait_ref.to_host_effect_clause(
Unnormalized::new_wip(trait_ref.to_host_effect_clause(
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

also add an fn to_host_effect_clause to Unnormalized

View changes since the review

// Don't check non-defaulted params, dependent defaults (including lifetimes)
// or preds with multiple params.
if instantiated_pred.has_non_region_param()
if instantiated_pred.probe(|v| v.has_non_region_param())
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

why probe over skip_norm_wip?

View changes since the review

.try_normalize_erasing_regions(
ty::TypingEnv::non_body_analysis(tcx, def_a.did()),
unnormalized_ty,
unnormalized_ty.clone(),
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

why .clone? Unnormalized should also be copy 🤔

View changes since the review

.tcx
.explicit_super_predicates_of(def_id)
.iter_identity_copied()
.map(Unnormalized::skip_norm_wip)
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
.map(Unnormalized::skip_norm_wip)

View changes since the review


if let Err(reported) = tcx.type_of(start_from_impl).instantiate_identity().error_reported() {
if let Err(reported) =
tcx.type_of(start_from_impl).instantiate_identity().skip_norm_wip().error_reported()
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

Suggested change
tcx.type_of(start_from_impl).instantiate_identity().skip_norm_wip().error_reported()
tcx.type_of(start_from_impl).instantiate_identity().skip_normalization().error_reported()

intended, don't care about norm

View changes since the review

Comment on lines +55 to +60
pub fn probe<F, U>(&self, f: F) -> U
where
F: FnOnce(&T) -> U,
{
f(&self.value)
}
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 15, 2026

Choose a reason for hiding this comment

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

I would entirely remove this method/is it needed to get something that's copy out of an Unnormalized which is not? 🤔 maybe add as_ref() instead then, as in value.as_ref().skip_normalized().whatever

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.

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

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-review Status: Awaiting review from the assignee but also interested parties. 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