Skip to content

Don't look for non-type-level assoc consts when checking trait object types#153738

Open
fmease wants to merge 1 commit intorust-lang:mainfrom
fmease:dyn-ace-gingerly-care
Open

Don't look for non-type-level assoc consts when checking trait object types#153738
fmease wants to merge 1 commit intorust-lang:mainfrom
fmease:dyn-ace-gingerly-care

Conversation

@fmease
Copy link
Member

@fmease fmease commented Mar 11, 2026

On main, when looking for assoc items that weren't specified in a given trait object type (via binding), we comb through all (eligible) assoc consts whether type-level or not even though you're not allowed to bind non-type-level assoc consts.

That's usually fine because traits containing (eligible) non-type-level assoc consts are dyn incompatible, so the trait object type is ~already considered ill-formed. Therefore, in PR #150843, I saved myself the extra effort, esp. since back then it was more annoying to check if the const item was declared type-level.

(More concretely: In bodies, we check for all dyn compatibility violations when lowering the trait object type, so we will bail out early with an error; in contrast, item signatures / non-bodies get wfchecked (WellFormed obligation) after lowering which includes dyn compatiblity checking (DynCompatible obligation); however to reduce the amount of diags, during lowering if there are any unspecified assoc items, we'll check them for dyn compatibility and bail out early if any of them tests negative.)

Now, we obviously don't wfcheck the defsite of (eager) type aliases which breaks the assumption that such trait object types get rejected. This led to the regression found in #153731: The ill-formed trait object type doesn't get rejected (as is expected) but we now require the single (non-type-level) assoc const to be bound in the dyn-Trait which we didn't do before. The actual error talks about dyn incompatibility due to the aforementioned extra check that's usually there to contain the number of diags.

Fixes [after beta backport] #153731.

@rustbot rustbot added 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. labels Mar 11, 2026
@fmease fmease assigned BoxyUwU and unassigned BoxyUwU Mar 11, 2026
@rust-log-analyzer

This comment has been minimized.

@fmease fmease force-pushed the dyn-ace-gingerly-care branch from 414fbd9 to c8f02eb Compare March 11, 2026 23:44
Copy link
Member Author

@fmease fmease Mar 11, 2026

Choose a reason for hiding this comment

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

The changes in the preexisting tests simply revert some of the changes from PR #150843's first commit (8d524f0).

Copy link
Member Author

Choose a reason for hiding this comment

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

I've manually looked through a bunch of test directories (like type-alias/) and I've made rustc wfcheck the defsite of eager type aliases and run the test suite just to figure out if we already have a test demonstrating the lack of def-site wfcking for (eager) type aliases. It's possible that I've simply missed them since it was only a semi-rigorous approach.

@fmease fmease marked this pull request as ready for review March 12, 2026 00:01
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 12, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2026

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 12, 2026
@fmease fmease added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Mar 12, 2026
@fmease fmease force-pushed the dyn-ace-gingerly-care branch from c8f02eb to f9251e9 Compare March 12, 2026 17:05
ViewWay added a commit to ViewWay/rust that referenced this pull request Mar 14, 2026
… checking trait object types

This is a cherry-pick of PR rust-lang#153738

Problem:
- PR rust-lang#150843 introduced a regression where eager type aliases containing
  dyn Trait (with associated consts) would incorrectly report
  'trait is not dyn compatible' errors
- This affected 254 crates depending on old bitvec versions

Root cause:
- When checking for unspecified assoc items in trait object types,
  we were filtering for all associated consts (is_const())
- But eager type aliases don't get wfchecked at definition site
- This broke the assumption that dyn-incompatible traits would be
  rejected early

Fix:
- Changed is_const() to is_type_const() to only check type-level
  associated consts, not regular associated consts
- Regular associated consts cannot be bound in trait object types
  anyway, so checking them was unnecessary

Modified files:
- compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs
- compiler/rustc_middle/src/ty/assoc.rs
- compiler/rustc_middle/src/ty/sty.rs
- compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs
- tests/ui/type-alias/lack-of-wf.rs (new test)

Fixes: rust-lang#153731
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-nominated Nominated for backporting to the compiler in the beta channel. PG-exploit-mitigations Project group: Exploit mitigations 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.

4 participants