rust/src/test/rustdoc/inline_cross
Aaron Hill 06546d4b40
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-13 13:45:12 -05:00
..
auxiliary Record proc macro harness order for use during metadata deserialization 2020-02-15 15:48:36 -05:00
add-docs.rs Enable smart punctuation 2021-02-07 19:57:53 -08:00
assoc-items.rs Update tests 2019-07-16 23:24:26 +02:00
cross-glob.rs Remove licenses 2018-12-25 21:08:33 -07:00
default-trait-method.rs rustdoc: Remove default keyword from re-exported trait methods 2019-04-14 23:55:14 +01:00
hidden-use.rs Remove licenses 2018-12-25 21:08:33 -07:00
impl-inline-without-trait.rs Update tests 2019-07-16 23:24:26 +02:00
impl_trait.rs Avoid sorting predicates by `DefId` 2021-03-13 13:45:12 -05:00
inline_hidden.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-28480.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-31948-1.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-31948-2.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-31948.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-32881.rs Remove double trailing newlines 2019-04-22 16:57:01 +01:00
issue-33113.rs Remove licenses 2018-12-25 21:08:33 -07:00
macro-vis.rs Remove licenses 2018-12-25 21:08:33 -07:00
macros.rs rustdoc: wrap stability tags in colored spans 2019-01-31 15:15:59 -05:00
proc_macro.rs Record proc macro harness order for use during metadata deserialization 2020-02-15 15:48:36 -05:00
renamed-via-module.rs Remove licenses 2018-12-25 21:08:33 -07:00
trait-vis.rs Remove licenses 2018-12-25 21:08:33 -07:00
use_crate.rs Add more --extern tests. 2019-11-07 05:51:17 -08:00