Open cjgillot opened 2 years ago
- [ ] Stop implementing
PartialOrd
andOrd
onDefId
...
Can these items be done one at a time?
Of course!
Will give this a go and see what happens. @rustbot claim
I started experimenting with LocalDefId
. I wonder if the DefId
work might end up being slightly less complex. 🤔
I wonder if the DefId work might end up being slightly less complex. 🤔
Update: No. 😄
Looks like removing Ord
, PartialOrd
for ExpnId
and LocalExpnId
might require changes to the macro rustc_index::newtype_index
because of this line:
Track accesses to command-line options; Track accesses to debugging command-line options.
Most of them are already "tracked" by discarding the entire incr comp cache if they change. The ones that aren't tracked are explicitly not tracked. See the UNTRACKED
mentions in https://github.com/rust-lang/rust/blob/d22dd65835190278f315e06442614142653ec98f/compiler/rustc_session/src/options.rs A couple of them should be marked as TRACKED
though I think.
Here's where we'd be if we merge https://github.com/rust-lang/rust/pull/90408:
- [ ] Stop implementing
PartialOrd
,Ord
forHirId
- [ ] Stop implementing
PartialOrd
,Ord
forDefId
- [x] Stop implementing
PartialOrd
,Ord
forLocalDefId
- [ ] Stop implementing
Idx
forLocalDefId
- [ ] Stop implementing
PartialOrd
,Ord
forExpnId
- [ ] Stop implementing
PartialOrd
,Ord
forLocalExpnId
- [ ] Stop implementing
Idx
forLocalExpnId
@cjgillot It looks like rustc_span::hygiene::ExpnId
does not implement Ord
or PartialOrd
. rustc_span::hygiene::LocalExpnId
does implement ordering.
(So does rustc_middle::thir::ExprId
. Is that related to this issue?)
I think it would be useful to enforce that UNTRACKED
options are not accessed within a query (we could add a helper function to suppress the check, to allow for things like debug printing inside a query).
Correction to list of known issues: LocalExpnId
does implement Ord
.
Known issues:
- [ ] Stop implementing
PartialOrd
andOrd
onDefId
;- [ ] Stop implementing
PartialOrd
,Ord
andIdx
forLocalDefId
;
- [ ] Stop implementing
PartialOrd
,Ord
andIdx
forLocalExpnId
;- [ ] Enforce that UNTRACKED options are not accessed within a query.
I think it would be useful to enforce that
UNTRACKED
options are not accessed within a query
@Aaron1011 Did you have someplace specific in mind for where to add this check?
The fields could be made private and instead a getter for each option could be exposed that run dep_graph.assert_ignored()
. And maybe also add an _untracked
variant of each getter for cases where accessing it inside a query is fine, like for debug dumps.
The fields could be made private...
That is, fields of rustc_session::options::Options
. For example: https://github.com/rust-lang/rust/blob/3b263ceb5cb89b6d53b5a03b47ec447c3a7f7765/compiler/rustc_session/src/options.rs#L165 https://github.com/rust-lang/rust/blob/3b263ceb5cb89b6d53b5a03b47ec447c3a7f7765/compiler/rustc_session/src/options.rs#L1176
@cjgillot It looks like
rustc_span::hygiene::ExpnId
does not implementOrd
orPartialOrd
.
All the better.
rustc_span::hygiene::LocalExpnId
does implement ordering.
So we should remove it. I don't think it's required anywhere.
(So does
rustc_middle::thir::ExprId
. Is that related to this issue?)
There is no issue here. ExprId
is local to a function body, reordering statements within a function body changes meaning, unlike reordering functions.
Current status:
PartialOrd
, Ord
from LocalDefId
(https://github.com/rust-lang/rust/pull/90408)PartialOrd
, Ord
from LocalExpnId
(https://github.com/rust-lang/rust/pull/94614)PartialOrd
, Ord
from HirId
(https://github.com/rust-lang/rust/pull/92233)PartialOrd
, Ord
from DefId
(https://github.com/rust-lang/rust/pull/90749)Items still under consideration:
Idx
from LocalDefId
(https://github.com/rust-lang/rust/pull/92359)Idx
from LocalExpnId
The Ord for Span also seems problematic. Stable hash for span is based on file, line and column, so it is stable within a single file. At the same time Ord provides an order between different files, which is turn is based on the order files happen to be loaded into the source map.
For example going from pub mod a; pub mod b
, to pub mod b; pub mod a
, will result in the same hashes for spans from module a
and module b
but they will have a different order.
Ord for Span also compares SyntaxContext which doesn't seem to be stable either:
And the PartialOrd
/ Ord
is derived and compares only the id:
@rustbot label -E-help-wanted
The Ord for Span also seems problematic. Stable hash for span is based on file, line and column, so it is stable within a single file. At the same time Ord provides an order between different files, which is turn is based on the order files happen to be loaded into the source map.
For example going from
pub mod a; pub mod b
, topub mod b; pub mod a
, will result in the same hashes for spans from modulea
and moduleb
but they will have a different order.Ord for Span also compares SyntaxContext which doesn't seem to be stable either.
Thanks @tmiasko! This is super interesting. My sense is that the ordering on Span
s is used a lot, often as a kind of fallback. I wonder, though, if removing this ordering is even possible! Don't we need to order certain things like lints based on the order of their appearance in source code? Maybe we could use an implementation that orders Span
s within a given file, but not between files?
@cjgillot: what do you think of Ord
for Span
?
When iterating over a collection, be it a Vec, a HashMap or an IndexMap, the order of items influences the value of the resulting hash:
[a, b]
and[b, a]
have different hashes.Meanwhile, there is some information we do not want to track. This is the case of the value of
LocalDefId
. Inserting a function in a file will change other functionsLocalDefId
, but it will not change theirDefPathHash
.My concern is about controlling this information flow. In order to do that
ToStableHashKey
trait replaces the iteration order of the collection (which for hash maps is based on the key and the allocated memory capacity and should be irrelevant to the compilation), by the order of a stable hash key (theDefPathHash
when the key isLocalDefId
). By sorting the vectors by stable key, we manage the information flow.Using
IndexMap
, the iteration order is the insertion order. Normally, this insertion order should only depend on tracked information obtained by depending on another query. For instance, a HIR visitor will create a query dependency onhir_owner_nodes
, which hashes the in-code declaration order of functions. However, and this is my concern, the order ofLocalDefId
is freely available without using a query and is purposely untracked.In order to make
IndexMap
s safe for incr. comp., we need to ensure untracked information is inaccessible.Known issues:
PartialOrd
andOrd
onDefId
;PartialOrd
,Ord
andIdx
forLocalDefId
;PartialOrd
,Ord
andIdx
forLocalExpnId
;PartialOrd
andOrd
forSyntaxContext
;Originally posted by @cjgillot in https://github.com/rust-lang/rust/pull/90253#issuecomment-952203213