2024-02-22 06:10:29 -06:00
|
|
|
//@ revisions: rpass1 rpass2
|
Avoid sorting predicates by `DefId`
Fixes issue #82920
Even if an item does not change between compilation sessions, it may end
up with a different `DefId`, since inserting/deleting an item affects
the `DefId`s of all subsequent items. Therefore, we use a `DefPathHash`
in the incremental compilation system, which is stable in the face of
changes to unrelated items.
In particular, the query system will consider the inputs to a query to
be unchanged if any `DefId`s in the inputs have their `DefPathHash`es
unchanged. Queries are pure functions, so the query result should be
unchanged if the query inputs are unchanged.
Unfortunately, it's possible to inadvertantly make a query result
incorrectly change across compilations, by relying on the specific value
of a `DefId`. Specifically, if the query result is a slice that gets
sorted by `DefId`, the precise order will depend on how the `DefId`s got
assigned in a particular compilation session. If some definitions end up
with different `DefId`s (but the same `DefPathHash`es) in a subsequent
compilation session, we will end up re-computing a *different* value for
the query, even though the query system expects the result to unchanged
due to the unchanged inputs.
It turns out that we have been sorting the predicates computed during
`astconv` by their `DefId`. These predicates make their way into the
`super_predicates_that_define_assoc_type`, which ends up getting used to
compute the vtables of trait objects. This, re-ordering these predicates
between compilation sessions can lead to undefined behavior at runtime -
the query system will re-use code built with a *differently ordered*
vtable, resulting in the wrong method being invoked at runtime.
This PR avoids sorting by `DefId` in `astconv`, fixing the
miscompilation. However, it's possible that other instances of this
issue exist - they could also be easily introduced in the future.
To fully fix this issue, we should
1. Turn on `-Z incremental-verify-ich` by default. This will cause the
compiler to ICE whenver an 'unchanged' query result changes between
compilation sessions, instead of causing a miscompilation.
2. Remove the `Ord` impls for `CrateNum` and `DefId`. This will make it
difficult to introduce ICEs in the first place.
2021-03-12 16:53:02 -06:00
|
|
|
|
|
|
|
trait MyTrait: One + Two {}
|
|
|
|
impl<T> One for T {
|
|
|
|
fn method_one(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T> Two for T {
|
|
|
|
fn method_two(&self) -> usize {
|
|
|
|
2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T: One + Two> MyTrait for T {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a: &dyn MyTrait = &true;
|
|
|
|
assert_eq!(a.method_one(), 1);
|
|
|
|
assert_eq!(a.method_two(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-order traits 'One' and 'Two' between compilation
|
|
|
|
// sessions
|
|
|
|
|
|
|
|
#[cfg(rpass1)]
|
|
|
|
trait One { fn method_one(&self) -> usize; }
|
|
|
|
|
|
|
|
trait Two { fn method_two(&self) -> usize; }
|
|
|
|
|
|
|
|
#[cfg(rpass2)]
|
|
|
|
trait One { fn method_one(&self) -> usize; }
|