Skip to content

fix: defer const normalize in coherence mode - #158205

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
cordx56:fix/const-coherence
Jul 27, 2026
Merged

fix: defer const normalize in coherence mode#158205
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
cordx56:fix/const-coherence

Conversation

@cordx56

@cordx56 cordx56 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

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
Contributor 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
Contributor 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
Contributor 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
Contributor 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
Contributor 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
Contributor Author

@rustbot review

I changed the code to resolve conflict.

@lcnr

lcnr commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7e186a8 has been approved by lcnr

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jul 27, 2026
@rust-bors rust-bors Bot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 27, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #160009 (`rust-analyzer` subtree update)
 - #159833 (ci: Make the `x86_64-gnu-parallel-frontend` job non-optional)
 - #158873 (applying `/Brepro` in bootstrap on MSVC and adding a run-make test.)
 - #159777 (Enforce that we don't generate code for comptime fns)
 - #159997 (On many bindings with move error, limit the number of `Span`s)
 - #158205 (fix: defer const normalize in coherence mode)
 - #159312 (Windows implementation for stdio set/take/replace)
 - #159772 (Fix lowering of resolved const inference variables)
 - #159785 (Share _Unwind_Exception definition between native and wasm)
 - #159968 (Fix the const impl suggestion)
 - #159974 (Update thinvec to 0.2.19)
 - #160013 (tests/ui: Ignore one query cycle test in parallel frontend mode)
 - #160016 (compiletest: do not talk about JSON when the user never sees any)
 - #160018 (test suite: add ARM case to ABI-required target feature check for -Ctarget-cpu)
@rust-bors
rust-bors Bot merged commit 3aec251 into rust-lang:main Jul 27, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 27, 2026
rust-timer added a commit that referenced this pull request Jul 27, 2026
Rollup merge of #158205 - cordx56:fix/const-coherence, r=lcnr

fix: defer const normalize in coherence mode

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.
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 3, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#160009 (`rust-analyzer` subtree update)
 - rust-lang/rust#159833 (ci: Make the `x86_64-gnu-parallel-frontend` job non-optional)
 - rust-lang/rust#158873 (applying `/Brepro` in bootstrap on MSVC and adding a run-make test.)
 - rust-lang/rust#159777 (Enforce that we don't generate code for comptime fns)
 - rust-lang/rust#159997 (On many bindings with move error, limit the number of `Span`s)
 - rust-lang/rust#158205 (fix: defer const normalize in coherence mode)
 - rust-lang/rust#159312 (Windows implementation for stdio set/take/replace)
 - rust-lang/rust#159772 (Fix lowering of resolved const inference variables)
 - rust-lang/rust#159785 (Share _Unwind_Exception definition between native and wasm)
 - rust-lang/rust#159968 (Fix the const impl suggestion)
 - rust-lang/rust#159974 (Update thinvec to 0.2.19)
 - rust-lang/rust#160013 (tests/ui: Ignore one query cycle test in parallel frontend mode)
 - rust-lang/rust#160016 (compiletest: do not talk about JSON when the user never sees any)
 - rust-lang/rust#160018 (test suite: add ARM case to ABI-required target feature check for -Ctarget-cpu)
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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