Repository navigation
Require coherence of supertrait associated item bounds for dyn-compability - #158915
theemathas wants to merge 2 commits into
Conversation
|
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. |
4d67fa1 to
256670e
Compare
|
r? types |
|
after that, we should do a crater run 😁 |
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
256670e to
4adf718
Compare
This comment has been minimized.
This comment has been minimized.
4adf718 to
97592bc
Compare
|
I decided to ask an LLM for assistance with understanding how the trait solver API works, and with double-checking my code for any glaring issues. I still wrote all code manually. In particular, the LLM did the following:
I claim full responsibility of all code in this PR. |
| // Now that we've constrained the two projections to be on the same thing, | ||
| // we check whether the two terms are necessarily equal to each other. | ||
| // If they are, then the two projections are coherent. | ||
| plug_infer_with_placeholders(&infcx, ty::UniverseIndex::ROOT, (proj_1, proj_2)); | ||
| let ocx = ObligationCtxt::new(&infcx); | ||
| ocx.eq(&ObligationCause::dummy(), param_env, proj_1.term, proj_2.term).is_ok() | ||
| && ocx.evaluate_obligations_error_on_ambiguity().no_errors() | ||
| && ocx.resolve_regions(CRATE_DEF_ID, param_env, []).is_empty() | ||
| } |
There was a problem hiding this comment.
The LLM, in reviewing my code, told me that I have to instantiate the param_env with trait_args here. However, I should not do this instantiation in the can_equate_generics computation. Is the LLM right here? I don't understand at all what this does.
There was a problem hiding this comment.
This test is currently the only pre-existing test that this PR breaks. What should be done about this test?
|
Is this PR ready for a crater run? @rustbot ready |
97592bc to
8546c21
Compare
|
I just realized that the current implementation has a weird consequence: It is possible for a dyn-compatible trait to be a subtrait of a dyn-incompatible trait. For example: trait Super<T> {
type Assoc;
}
trait Sub<T, U>: Super<T, Assoc = i32> + Super<U, Assoc = i64> {}
trait Sub2: Sub<i32, i64> {}In the above code, |
This comment has been minimized.
This comment has been minimized.
Require coherence of supertrait associated item bounds for dyn-compability
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
This comment has been minimized.
This comment has been minimized.
|
Crater is clean. |
5a1c765 to
2c711a8
Compare
This comment has been minimized.
This comment has been minimized.
2c711a8 to
20cb1f3
Compare
This comment has been minimized.
This comment has been minimized.
|
Squashed into 2 commits, and rebased. Ready for review, and hopefully ready for FCP. |
20cb1f3 to
03399a0
Compare
This comment has been minimized.
This comment has been minimized.
03399a0 to
8ece667
Compare
…ics. See rust-lang#154662 These tests will be fixed in a subsequent commit.
…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 impls (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.
8ece667 to
82dd806
Compare
|
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. |
|
I moved the new tests into its own directory. I had an LLM look at this PR again. It came up with 4 new test cases ( The LLM also noted that my initial idea of switching typing modes between the two stages was already correct. So I did that, which caused |
| // even though a supertrait is dyn-incompatible. | ||
| // This can occur when the subtrait instantiates generics to be | ||
| // concrete enough that we can prove that the associated type bounds | ||
| // do not conflict. |
There was a problem hiding this comment.
I think that's novel. Can we get bugs from doing trait upcasting or what not?
| // Make sure that the dyn-compatibility check for conflicting bounds | ||
| // only use bounds from supertraits, and not from type parameters. |
|
|
||
| // Determining that `Sub` is dyn-compatible requires normalizing `<Self as Constant>::Out`. | ||
| // This depends on impl blocks elsewhere. Make sure we can do this. | ||
| // Due to the `PointeeSized`, this normalization doesn't need any trait bounds on `Self`. |
There was a problem hiding this comment.
instead use #[rustc_no_implicit_default_bounds]
|
|
||
| /// Computes [`DynCompatibilityViolation::IncoherentSupertraitAssocs`] | ||
| #[instrument(level = "debug", skip(tcx))] | ||
| fn incoherent_supertrait_assocs( |
There was a problem hiding this comment.
instead of assocs, I'd prefer assoc_items as naming
| }) | ||
| } | ||
|
|
||
| #[instrument(level = "debug", skip(tcx), ret)] |
There was a problem hiding this comment.
also worth mentioning that this is similar to the explicit negative coherence checking, i.e.
| // as they're projecting different things. | ||
| let can_equate_generics = | ||
| proj_1.projection_term.args.iter().zip_eq(proj_2.projection_term.args).all( | ||
| |(arg_1, arg_2)| ocx.eq(&ObligationCause::dummy(), param_env, arg_1, arg_2).is_ok(), |
There was a problem hiding this comment.
we should equate them in an empty env here
| // FIXME: This doesn't instantiate generic params with placeholders in any bounds | ||
| // that may be helpful in proving that the two terms are equal with each other. |
There was a problem hiding this comment.
see what we do for explicit negative coherence
rust/compiler/rustc_trait_selection/src/traits/coherence.rs
Lines 417 to 441 in 0259d5e
should do the same here
| // For this second step, we use typecheck mode, as we're distinguishing | ||
| // "maybe they're the same" vs "they're definitely the same". | ||
| let infcx = infcx.fork_with_typing_mode(TypingMode::non_body_analysis()); |
There was a problem hiding this comment.
also see
rust/compiler/rustc_trait_selection/src/traits/coherence.rs
Lines 570 to 576 in 0259d5e
|
@rustbot author |
View all comments
This PR is an alternative to #157710. However, unlike that PR, this PR does not address #150936.
Fixes #154662
In a
dyntype, 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
dyntype 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 impls (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