Skip to content

report unresolved dyn associated const evaluation during CTFE - #164840

Open
amirHdev wants to merge 3 commits into
rust-lang:mainfrom
amirHdev:dyn-associated-const-eval
Open

amirHdev wants to merge 3 commits into
rust-lang:mainfrom
amirHdev:dyn-associated-const-eval

Conversation

@amirHdev

@amirHdev amirHdev commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

with the next solver an associated const can stay unresolved when a trait-object impl overlaps a blanket impl and can let conflicting const assertions compile or cause an ICE when the value is used
this change reports a regular error during CTFE
related to rust-lang/trait-system-refactor-initiative#311

r? oli-obk maybe

@rustbot rustbot added 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 Oct 24, 2026
@rust-log-analyzer

This comment has been minimized.

@amirHdev
amirHdev force-pushed the dyn-associated-const-eval branch from df4d0a0 to 74a9623 Compare October 24, 2026 10:57
@rust-log-analyzer

This comment has been minimized.

@amirHdev
amirHdev force-pushed the dyn-associated-const-eval branch from 74a9623 to f01b054 Compare October 24, 2026 11:14
@amirHdev
amirHdev marked this pull request as ready for review October 28, 2026 17:18
@rustbot

rustbot commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

@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 Oct 28, 2026
Comment on lines +461 to +467
let unresolved_dyn_assoc = tcx.next_trait_solver_globally()
&& in_codegen
&& key.value.promoted.is_none()
&& matches!(tcx.def_kind(key.value.instance.def_id()), DefKind::AssocConst)
&& key.value.instance.args.iter().any(|arg| {
matches!(arg.kind(), ty::GenericArgKind::Type(ty) if matches!(ty.kind(), ty::Dynamic(..)))
});

@RalfJung RalfJung Oct 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks extremely fragile. This function here should not have any special interactions with the trait solver, let alone a random arbitrary error message from the other end of the compiler. Such special cases are not maintainable in the long term.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like there is a bug somewhere else, where TooGeneric is emitted for code that's not actually generic? That should be fixed at the source, not with work-arounds far downstream.

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 removed the workaround and moved the error handling closer to where normalization fails
there is just one thing I'm not sure about!
codegen can still have a generic ParamEnv and could a normalization failure here still mean TooGeneric or is it always safe to treat it as an actual error? @RalfJung

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

codegen is never in a generic ParamEnv I think?

@oli-obk oli-obk 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 Oct 29, 2026
@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

Comment on lines +357 to +365
if match self.typing_env.typing_mode() {
ty::TypingMode::Codegen => true,
ty::TypingMode::Coherence
| ty::TypingMode::Typeck { .. }
| ty::TypingMode::PostTypeckThroughBorrowck { .. }
| ty::TypingMode::PostBorrowck { .. }
| ty::TypingMode::Reflection
| ty::TypingMode::PostAnalysis
| ty::TypingMode::ErasedNotCoherence(_) => false,

@RalfJung RalfJung Nov 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That still looks very fragile and hard-to-maintain. At the very least there needs to be an extensive comment explaining exactly why this logic is the way ot is. But really, ad-hoc matching on error enums from other subsystems is a big red flag.

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.

one thing I'm still wondering about is if normalization fails in PostAnalysis could it succeed when retried in Codegen? with this change we'd report the error right away instead of retrying

just wanted to check if that's something we need to worry about :)

InvalidProgramInfo::AlreadyReported(_) | InvalidProgramInfo::Layout(..),
InvalidProgramInfo::AlreadyReported(_)
| InvalidProgramInfo::Layout(..)
| InvalidProgramInfo::NormalizationFailure(..),

@RalfJung RalfJung Nov 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please also add a Miri test that triggers this new error.

View changes since the review

@oli-obk

oli-obk commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@bors squash msg="Report normalization failures during CTFE"

@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

🔨 3 commits were squashed into dbcc385.

@rust-bors
rust-bors Bot force-pushed the dyn-associated-const-eval branch from a7c1d12 to dbcc385 Compare November 26, 2026 10:47
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

@oli-obk

oli-obk commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Thanks @RalfJung for the reviews. The final version lgtm

@bors r+ rollup

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit dbcc385 has been tentatively approved by oli-obk

It will be put into the queue for this repository once PR CI succeeds.

@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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
…, r=oli-obk

report unresolved dyn associated const evaluation during CTFE

with the next solver an associated const can stay unresolved when a trait-object impl overlaps a blanket impl and can let conflicting const assertions compile or cause an ICE when the value is used
this change reports a regular error during CTFE
related to rust-lang/trait-system-refactor-initiative#311

r? oli-obk maybe
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #166615 (comment)

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 26, 2026
@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#166615), which was unapproved.

View changes since this unapproval

Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
@amirHdev
amirHdev force-pushed the dyn-associated-const-eval branch from fac9bf9 to b153029 Compare November 27, 2026 08:52
@amirHdev

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-armhf-gnu

@rust-bors

rust-bors Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@amirHdev: 🔑 Insufficient privileges: not in try users

@amirHdev

Copy link
Copy Markdown
Contributor Author

Could you please take another look ? @oli-obk

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors try jobs=test-armhf-gnu

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
report unresolved dyn associated const evaluation during CTFE


try-job: test-armhf-gnu
@rust-bors

rust-bors Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9eff1fd (9eff1fd59ec0fd5f41778f5b8093e68300ae8935)
Base parent: b8794ab (b8794ab3b96cbe1cd0698ecec35b7ffcedc9e995)

@amirHdev

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Nov 28, 2026
@RalfJung

Copy link
Copy Markdown
Member

Yes this looks very nice now, thanks :)

@RalfJung RalfJung Nov 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it worth a comment explaining why we need this?
To me that's not clear at all.^^ I'm not even fully sure what this does...

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.

actually this avoids the dylib loading failure we hit during the rollup
I added a inline comment

This branch has not been deployed

No deployments
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. 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.

6 participants