Skip to content

DeeplyNormalize and normalize_with_depth_to take Unnormalized<T> as input#158668

Open
Shourya742 wants to merge 4 commits into
rust-lang:mainfrom
Shourya742:2026-07-01-use-unnormalized
Open

DeeplyNormalize and normalize_with_depth_to take Unnormalized<T> as input#158668
Shourya742 wants to merge 4 commits into
rust-lang:mainfrom
Shourya742:2026-07-01-use-unnormalized

Conversation

@Shourya742

Copy link
Copy Markdown
Member

part of #155345

r? @lcnr

@rustbot

rustbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

changes to the core type system

cc @lcnr

@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 Jul 1, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the 2026-07-01-use-unnormalized branch 2 times, most recently from 2f2feff to 8977db3 Compare July 2, 2026 03:12
ConstraintCategory::Boring,
self.infcx.param_env.and(type_op::normalize::DeeplyNormalize { value }),
);
result.unwrap_or(value)

@lcnr lcnr Jul 2, 2026

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.

can you instead change the signature of this function to propagate the ErrorGuaranteed and handle it in its one call site?

View changes since the review

self.constraints.type_tests.push(type_test);
}

fn normalize_and_add_type_outlives_constraints(

@lcnr lcnr Jul 2, 2026

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.

Suggested change
// FIXME(trait-system-refactor-initiative#260): This function should be
// removed.
fn normalize_and_add_type_outlives_constraints(

View changes since the review

@lcnr lcnr Jul 3, 2026

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.

I feel like "needs to" has a slightly bad vibe and I care enough that I would like you to change this to "should be"

It's hard to explain. To me "needs to" has a stronger expectation that this is something that somebody has to do while "should" is a lot more things would be better if this happens.

In a sense, needs to feels like it introduces an obligation for someone to resolve this in the near future while should is mainly just a note that this is something worth dealing with

Comment on lines 356 to 362
}) = self.infcx.fully_perform(
DeeplyNormalize { value: ty::Unnormalized::new_wip(outlives) },
span,
)
else {
self.infcx.dcx().delayed_bug(format!("could not normalize {outlives:?}"));
return;

@lcnr lcnr Jul 2, 2026

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.

can you instead change this to a match and in the error branch do let _: ErrorGuarateed = guar; instead of a delayed_bug

View changes since the review

|ty::ParamEnvAnd { param_env, value }| ty::ParamEnvAnd {
param_env,
value: Normalize { value: value.value },
value: Normalize { value: value.value.skip_normalization() },

@lcnr lcnr Jul 2, 2026

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.

idk how much effort, so could leave for future work, but Normalize should also take Unnormalized. Or actually, we should merge NormalizeandDeeplyNormalize`. They are now the same thing afaict?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Collapsed them here: d8f43f7

Comment thread compiler/rustc_trait_selection/src/traits/select/confirmation.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/select/mod.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/select/mod.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/normalize.rs Outdated
Comment thread compiler/rustc_trait_selection/src/traits/project.rs Outdated
Comment on lines 583 to 592
push_const_arg_has_type_obligation(
tcx,
obligations,
&cause,
depth + 1,
param_env,
term,
def_id,
args,
);

@lcnr lcnr Jul 2, 2026

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.

and move this below the call to normalizes_with_depth_to? cc @BoxyUwU @khyperia for whether ConstArgHasType expects (or allows) the const to be normalized

View changes since the review

@khyperia khyperia Jul 3, 2026

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.

afaik this call to push_const_arg_has_type_obligation is checking "hey, make sure unwrapping this single alias layer didn't change the type":

type const FurtherAlias: bool = 5_usize;
type const Alias: usize = FurtherAlias;
... [(); Alias] ...

when on a projection goal for Alias in [(); Alias], this should check that resolving Alias to FurtherAlias (which is what const_of_item does) has the resulting value still retain the original type of Alias, which is usize.

In other words, it checks :

  • typeof(const_of_item(Alias)) == typeof(Alias) =>
  • typeof(FurtherAlias) == typeof(Alias) =>
  • bool == usize, ❌ compiler error.

If we eager norm before doing that, we resolve FurtherAlias to 5_usize, and then check "hey, does 5_usize still have the original type of Alias, which is usize", we go "oh yeah yep we gucci", missing the fact that we hit a bool in the middle.

In other words,

  • typeof(normalize(const_of_item(Alias))) == typeof(Alias) =>
  • typeof(5_usize) == typeof(Alias) =>
  • usize == usize ✔️ we good (we should not be good!!).

see also, this PR #154853

That's my naiive understanding based on a quick reading of the code though, I would defer to boxy here

@khyperia khyperia Jul 3, 2026

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.

(type aliases don't have this issue, because they're all just kind *, so resolving a type alias never changes its kind. As a hypothetical, I think we'd have to do the same thing for types if we ever supported something like this:)

type List: * -> * = Vec;
// ... let _: List<u32>; ...
type BadAlias: * = List;
// ... let _: BadAlias; ...

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.

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.

after discussion, seems it's fine that we only check push_const_arg_has_type_obligation on the fully normalized const (i.e. it doesn't really matter either way).

From a type theory perspective, this check is unnecessary, and only exists to prevent CTFE from seeing non-wfchecked things. So, if the type of the value of the const happens to be "accidentally correct", that's fine, CTFE won't explode. The fact it's not WF will report an error in regular wfcheck.

@lcnr lcnr left a comment

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.

can you go through the calls to new_wip you've added whether we have a call to skip_norm_wip earlier in that function and whether you can restructure the code to remove both of them?

View changes since this review

@rust-cloud-vms rust-cloud-vms Bot force-pushed the 2026-07-01-use-unnormalized branch from 8977db3 to d8f43f7 Compare July 3, 2026 06:53
@rust-log-analyzer

This comment has been minimized.

@Shourya742 Shourya742 requested a review from lcnr July 3, 2026 08:15
@lcnr lcnr changed the title DeeplyNormalize QueryTpeOp and normalize_with_depth_to takes unnormalized<T> as input DeeplyNormalize and normalize_with_depth_to takes Unnormalized<T> as input Jul 3, 2026
@lcnr lcnr changed the title DeeplyNormalize and normalize_with_depth_to takes Unnormalized<T> as input DeeplyNormalize and normalize_with_depth_to take Unnormalized<T> as input Jul 3, 2026
param_env.and(type_op::normalize::Normalize { value }),
);
result.unwrap_or(value)
result.unwrap_or(value.skip_normalization())

@lcnr lcnr Jul 3, 2026

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.

This is also dubious and something we shouldn't support :>

can keep it as wip for now

Suggested change
result.unwrap_or(value.skip_normalization())
result.unwrap_or(value.skip_norm_wip())

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants