rust/src
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
..
bootstrap Rollup merge of #82924 - sunfishcode:wasi-command, r=alexcrichton 2021-03-10 17:55:41 +01:00
build_helper
ci Auto merge of #82747 - JohnTitor:pin-es-check-version, r=Mark-Simulacrum 2021-03-04 19:24:21 +00:00
doc Rollup merge of #82979 - GuillaumeGomez:run-button-pos, r=Nemo157 2021-03-12 08:55:18 +09:00
etc Rollup merge of #82557 - rylev:natvis-improvements, r=varkor 2021-03-08 13:13:20 +01:00
librustdoc Rollup merge of #83003 - notriddle:rustdoc-index-v3, r=GuillaumeGomez 2021-03-12 08:55:20 +09:00
llvm-project@62a1ddde22 Update llvm-project submodule 2021-03-11 22:04:10 +01:00
rustdoc-json-types x.py fmt 2021-03-06 15:50:29 -05:00
test Avoid sorting predicates by `DefId` 2021-03-13 13:45:12 -05:00
tools Auto merge of #83024 - Xanewok:update-rls, r=Dylan-DPC 2021-03-12 16:57:11 +00:00
README.md update message 2021-02-14 10:08:37 +05:30
stage0.txt Update the bootstrap compiler 2021-02-20 17:19:30 -05:00
version bump nightly version to 1.52.0 2021-02-05 16:25:08 +01:00

README.md

This directory contains the source code of the rust project, including:

  • The test suite
  • The bootstrapping build system
  • Various submodules for tools, like rustdoc, rls, etc.

For more information on how various parts of the compiler work, see the rustc dev guide.