Skip to content

fix: defer const normalize in coherence mode#158205

Open
cordx56 wants to merge 4 commits into
rust-lang:mainfrom
cordx56:fix/const-coherence
Open

fix: defer const normalize in coherence mode#158205
cordx56 wants to merge 4 commits into
rust-lang:mainfrom
cordx56:fix/const-coherence

Conversation

@cordx56

@cordx56 cordx56 commented Jun 21, 2026

Copy link
Copy Markdown

I'm not confident that this fix is the best and desired in the long term point of view, but I'm confident that this does not do anything unsound.

Fixes #157937, by deferring normalization of associated const, except for anon, in coherence mode.
This fix prevents reporting impls, including associated const, are not overlapping.

@rustbot

rustbot commented Jun 21, 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 Jun 21, 2026
@rustbot

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @petrochenkov (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@lcnr

lcnr commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

r? lcnr

@rustbot rustbot assigned lcnr and unassigned petrochenkov Jun 21, 2026
@rust-bors

This comment has been minimized.

@lcnr lcnr 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 Jul 6, 2026
@cordx56
cordx56 force-pushed the fix/const-coherence branch from 2054520 to 134d2ce Compare July 8, 2026 08:30
@rustbot

This comment has been minimized.

@cordx56

cordx56 commented Jul 8, 2026

Copy link
Copy Markdown
Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 8, 2026

@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.

sorry for the late review 🙇

avoided this PR a bit as thinking about gce is annoying

View changes since this review

// ripped out soon so this shouldn't matter soon.
StructurallyRelateAliases::No if !tcx.features().generic_const_exprs() => {
StructurallyRelateAliases::No
if !tcx.features().generic_const_exprs() || defer_in_coherence =>

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.

hmm, can you change this to instead be self.infcx.next_trait_solver() || !tcx.features().generic_const_exprs()

We don't support gce with the new solver but currently have the new solver in coherence if gce is enabled, so this should have the same results as your PR while being easier (for me) to reason about

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see, and thank you for reviewing!

I'm running tests and will push a revised version!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Wait, the ui test failed if I replaced my change with self.infcx.next_trait_solver() || !tcx.features().generic_const_exprs().

The case is tests/ui/const-generics/issues/issue-89304.rs that uses anon const kind like:

impl<const T: usize> From<GenericStruct<T>> for GenericStruct<{T + 1}> { ... }
impl<const T: usize> From<GenericStruct<{T + 1}>> for GenericStruct<T> { ... }

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.

that test should fail 🤣

From<W<A>> for W<{ expr involving A }> and From<W<{ expr involving A }>> for W<A> may overlap unless we do reasoning about the generic expression.

The compiler currently does not reason about the fact that T + 1 is larger than T, so that (T, T + 1) and (U + 1, U) can never be equal.

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.

As in, while this example could be accepted in theory, the reason we do so right now is due to a bug and not because we correctly reason about generic expressions

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Okay, so... //@ check-pass at line 1 on tests/ui/const-generics/issues/issue-89304.rs is actually caused by a bug and currently the compiler does not check this overlap accurately, right?

How should we handle the case like this? I don't know the process to decide these regressions in the Rust dev.
Should this be discussed in some other place? I've joined rust-lang Zulip.

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.

exactly

for GCE it'd be fine to just delete the test I think

more generally, often it's one or a combination of

  • reopen the original issue linked from the test
  • open a new issue if the failure of the test changed
  • mark the test as //@ known-bug
  • update the comment in the test file to the new (intended) behavior
  • cry

generic_const_exprs will be replaced by generic_const_args soon. We'd otherwise update the comment and expected outcome of this test and keep it around, but I think here deleting is easier 🤷

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see, so I will delete the test, commit it, and push it. Thanks for reviewing!

@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jul 20, 2026
@rust-bors

This comment has been minimized.

@cordx56
cordx56 force-pushed the fix/const-coherence branch from 20b9a4e to 7e186a8 Compare July 20, 2026 15:21
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

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.

@cordx56

cordx56 commented Jul 20, 2026

Copy link
Copy Markdown
Author

@rustbot review

I changed the code to resolve conflict.

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

Labels

A-tidy Area: The tidy tool S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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]: failed to resolve instance for <A<[i8; 1]> as From<A<[i8; 1]>>>::from

4 participants