Skip to content

Require coherence of supertrait associated item bounds for dyn-compability - #158915

Open
theemathas wants to merge 2 commits into
rust-lang:mainfrom
theemathas:coherent-dyn-compat
Open

Require coherence of supertrait associated item bounds for dyn-compability#158915
theemathas wants to merge 2 commits into
rust-lang:mainfrom
theemathas:coherent-dyn-compat

Conversation

@theemathas

Copy link
Copy Markdown
Contributor

This PR is an alternative to #157710. However, unlike that PR, this PR does not address #150936.

Fixes #154662

In a dyn type, if multiple bounds are specified via supertraits for the same associated item, then we previously accepted them if the relevant trait's generics are different, even if the bounds conflict.

This was unsound, since those generics could end up being instantiated with identical concrete types, causing the dyn type to have two different "values" for the same bound.

Thus, if a trait has multiple supertrait bounds for the same associated item, we check whether those bounds are coherent, similarly to how we check for overlap between impulse (i.e., we check if the generics could be instantiated to be the same while the "values" of the bounds are different). If the bounds are incoherent, then we consider the trait to be dyn-incompatible.

r? @fmease

…ics.

See rust-lang#154662

These tests will be fixed in a subsequent commit.
@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 7, 2026
@theemathas

Copy link
Copy Markdown
Contributor Author

I'm unsure of what to do with the now-failing tests.

I'm also rather unsure of my code in general. Please be careful with the review to make sure I didn't do anything stupid. Thank you.

@theemathas theemathas added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Jul 7, 2026
…ility

Fixes rust-lang#154662

In a `dyn` type, if multiple bounds are specified via supertraits for
the same associated item, then we previously accepted them if
the relevant trait's generics are different,
even if the bounds conflict.

This was unsound, since those generics could end up being instantiated
with identical concrete types, causing the `dyn` type to have
two different "values" for the same bound.

Thus, if a trait has multiple supertrait bounds for the same
associated item, we check whether those bounds are coherent,
similarly to how we check for overlap between impulse (i.e., we check
if the generics could be instantiated to be the same while the "values"
of the bounds are different). If the bounds are incoherent, then we
consider the trait to be dyn-incompatible.
@theemathas
theemathas force-pushed the coherent-dyn-compat branch from 4d67fa1 to 256670e Compare July 7, 2026 15:07
@theemathas

Copy link
Copy Markdown
Contributor Author

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jul 21, 2026
@rustbot rustbot assigned lcnr and unassigned fmease Jul 21, 2026
Comment on lines +1057 to +1062
// Note that we discard any obligations we get here.
// We don't care about proving them.
//
// If this call returns an Err, then the two sets of generic args
// can't possibly be instantiated with the same concrete types.
// So, we return true from the function

@lcnr lcnr Jul 29, 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.

you could do the proper coherence thing and actually use an ObligationCtxt and try_evaluate

makes slightly more compile

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.

Sorry, but I don't understand what you mean. What is an ObligationCtxt, and how does using it cause more code to compile?

.instantiate_identity(tcx)
.predicates
.into_iter()
.map(Unnormalized::skip_normalization);

@lcnr lcnr Jul 29, 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
.map(Unnormalized::skip_normalization);
.map(Unnormalized::skip_norm_wip);

View changes since the review

) -> bool {
// We syntactically compare the two terms. If they're equal, then
// they do not conflict with each other.
// FIXME: This is an overly strict definition of equality. Could this be done better?

@lcnr lcnr Jul 29, 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.

if we check for equality in the param_env of the trait and prove nested obligations + regions, this should be correct?

as in, what we prove here is

for<trait_params, trait-where-clauses> assoc1 == assoc2
  => assoc1_term == assoc2_term

and we under-approximate by

for<trait_params, trait-where-clauses> assoc1 != assoc2
OR
for<trait_params, trait-where-clauses> assoc1term == assoc2term

which we impl as

!exists<trait_params, trait-where-clauses> assoc1 == assoc2
OR
for<trait_params, trait-where-clauses> assoc1term == assoc2term

so to make this check more permissive, you could check the where-clauses of the trait in the exists check :>

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.

What's a nested obligation? What's a nested region? What's "the param_env of the trait"? What does it mean to "check" the where clauses of a trait?

@lcnr

lcnr commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

after that, we should do a crater run 😁

@lcnr

lcnr commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

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

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

needs-crater This change needs a crater run to check for possible breakage in the ecosystem. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. 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. T-types Relevant to the types 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.

Conflicting supertrait associated type in dyn causes unsoundness and ICE

4 participants