-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use hir::ItemLocalId as keys in TypeckTables. #43740
Use hir::ItemLocalId as keys in TypeckTables. #43740
Conversation
LGTM. r? @arielb1 |
src/librustc/middle/dead.rs
Outdated
@@ -119,6 +119,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { | |||
|
|||
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, def: Def, | |||
pats: &[codemap::Spanned<hir::FieldPat>]) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
src/librustc/middle/reachable.rs
Outdated
@@ -375,7 +375,7 @@ fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> | |||
}); | |||
let mut reachable_context = ReachableContext { | |||
tcx, | |||
tables: &ty::TypeckTables::empty(), | |||
tables: &ty::TypeckTables::empty(DefId::invalid()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a FIXME? Shouldn't tables
be None
when outside of an item?
src/librustc/ty/context.rs
Outdated
TypeckTables { | ||
type_dependent_defs: NodeMap(), | ||
local_id_root, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing with local_id_root
being sometimes invalid makes me uncomfortable. Please insert an Option
somewhere (I think making local_id_root
an option would work well).
src/librustc/ty/context.rs
Outdated
@@ -384,6 +390,30 @@ impl<'tcx> TypeckTables<'tcx> { | |||
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> ty::UpvarCapture<'tcx> { | |||
self.upvar_capture_map[&upvar_id] | |||
} | |||
|
|||
/// Validate that a NodeId can safely be converted to an ItemLocalId for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is this function called? It seems to be called on some random HIR id accesses.
src/librustc/hir/map/definitions.rs
Outdated
@@ -466,7 +466,11 @@ impl Definitions { | |||
} | |||
|
|||
pub fn find_node_for_hir_id(&self, hir_id: hir::HirId) -> ast::NodeId { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you rather do some sort of merging of this function with validate_hir_id
? Unmarked linear searches are dangerous.
src/librustc/ty/context.rs
Outdated
@@ -207,52 +207,55 @@ pub struct CommonTypes<'tcx> { | |||
|
|||
#[derive(RustcEncodable, RustcDecodable)] | |||
pub struct TypeckTables<'tcx> { | |||
/// The HirId::owner all ItemLocalIds in this table are relative to. | |||
pub local_id_root: DefId, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in several places initialized with an invalid_def_id
. Why can't this be an Option
?
I don't like the way pub struct LocalTableInContext<'a, V> {
parent_def_id: Option<DefId>,
data: &'a LocalItemMap<V>
} Which does validation on the accessor methods. Or something. |
OK, I'll change the various visitors to use an I'll also look into a way of handling |
☔ The latest upstream changes (presumably #43582) made this pull request unmergeable. Please resolve the merge conflicts. |
49abb36
to
75022ed
Compare
☔ The latest upstream changes (presumably #43743) made this pull request unmergeable. Please resolve the merge conflicts. |
91207de
to
c078583
Compare
… binary one. Unfortunately, the NodeId->HirId array is not sorted. Since this search is only done right before calling bug!(), let's not waste time allocating a faster lookup.
c078583
to
a69eaf6
Compare
@arielb1 I think I addressed your comments. |
(Note: I've added the last three commits. The others were not touched except for rebasing). |
src/librustc/hir/def_id.rs
Outdated
@@ -201,4 +201,11 @@ impl DefId { | |||
pub fn is_local(&self) -> bool { | |||
self.krate == LOCAL_CRATE | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused function plz kill
src/librustc/middle/dead.rs
Outdated
@@ -119,7 +119,9 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { | |||
|
|||
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, def: Def, | |||
pats: &[codemap::Spanned<hir::FieldPat>]) { | |||
let variant = match self.tables.node_id_to_type(lhs.id).sty { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: stray newlines in program
src/librustc/ty/context.rs
Outdated
} | ||
|
||
impl<'a, V> LocalTableInContextMut<'a, V> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: stray newline in program
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually like newlines in places like this. Makes the impl
line easier to read. But I'll remove it for consistency.
src/librustc_privacy/lib.rs
Outdated
@@ -1606,21 +1656,26 @@ fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, | |||
|
|||
let krate = tcx.hir.krate(); | |||
|
|||
let empty_tables = ty::TypeckTables::empty(None); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: stray newline in program (I think)
Nice! r=me modulo nits. |
b30f28a
to
6fd7d85
Compare
@bors r=arielb1 Amended the nit fixes to the last commit. Thanks for the reviews! |
📌 Commit 6fd7d85 has been approved by |
⌛ Testing commit 6fd7d85 with merge 52523ded9a2d39d7a76f41a9d373610cdeecf501... |
💔 Test failed - status-travis |
/// stored/returned. | ||
fn validate_hir_id_for_typeck_tables(local_id_root: Option<DefId>, | ||
hir_id: hir::HirId, | ||
mut_access: bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[00:10:17] error: unused variable: `local_id_root`
[00:10:17] --> /checkout/src/librustc/ty/context.rs:226:38
[00:10:17] |
[00:10:17] 226 | fn validate_hir_id_for_typeck_tables(local_id_root: Option<DefId>,
[00:10:17] | ^^^^^^^^^^^^^
[00:10:17] |
[00:10:17]
[00:10:17] error: unused variable: `hir_id`
[00:10:17] --> /checkout/src/librustc/ty/context.rs:227:38
[00:10:17] |
[00:10:17] 227 | hir_id: hir::HirId,
[00:10:17] | ^^^^^^
[00:10:17]
[00:10:17] error: unused variable: `mut_access`
[00:10:17] --> /checkout/src/librustc/ty/context.rs:228:38
[00:10:17] |
[00:10:17] 228 | mut_access: bool) {
[00:10:17] | ^^^^^^^^^^
[00:10:17]
[00:10:18] error: aborting due to 3 previous errors
[00:10:18]
[00:10:18] error: Could not compile `rustc`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @kennytm! That must be because of the cfg(debug_assertions)
switch...
@bors r=arielb1 |
📌 Commit 3b92b97 has been approved by |
…rielb1 Use hir::ItemLocalId as keys in TypeckTables. This PR makes `TypeckTables` use `ItemLocalId` instead of `NodeId` as key. This is needed for incremental compilation -- for stable hashing and for being able to persist and reload these tables. The PR implements the most important part of #40303. Some notes on the implementation: * The PR adds the `HirId` to HIR nodes where needed (`Expr`, `Local`, `Block`, `Pat`) which obviates the need to store a `NodeId -> HirId` mapping in crate metadata. Thanks @eddyb for the suggestion! In the future the `HirId` should completely replace the `NodeId` in HIR nodes. * Before something is read or stored in one of the various `TypeckTables` subtables, the entry's key is validated via the new `TypeckTables::validate_hir_id()` method. This makes sure that we are not mixing information from different items in a single table. That last part could be made a bit nicer by either (a) new-typing the table-key and making `validate_hir_id()` the only way to convert a `HirId` to the new-typed key, or (b) just encapsulate sub-table access a little better. This PR, however, contents itself with not making things significantly worse. Also, there's quite a bit of switching around between `NodeId`, `HirId`, and `DefIndex`. These conversions are cheap except for `HirId -> NodeId`, so if the valued reviewer finds such an instance in a performance critical place, please let me know. Ideally we convert more and more code from `NodeId` to `HirId` in the future so that there are no more `NodeId`s after HIR lowering anywhere. Then the amount of switching should be minimal again. r? @eddyb, maybe?
☀️ Test successful - status-appveyor, status-travis |
This PR makes
TypeckTables
useItemLocalId
instead ofNodeId
as key. This is needed for incremental compilation -- for stable hashing and for being able to persist and reload these tables. The PR implements the most important part of #40303.Some notes on the implementation:
HirId
to HIR nodes where needed (Expr
,Local
,Block
,Pat
) which obviates the need to store aNodeId -> HirId
mapping in crate metadata. Thanks @eddyb for the suggestion! In the future theHirId
should completely replace theNodeId
in HIR nodes.TypeckTables
subtables, the entry's key is validated via the newTypeckTables::validate_hir_id()
method. This makes sure that we are not mixing information from different items in a single table.That last part could be made a bit nicer by either (a) new-typing the table-key and making
validate_hir_id()
the only way to convert aHirId
to the new-typed key, or (b) just encapsulate sub-table access a little better. This PR, however, contents itself with not making things significantly worse.Also, there's quite a bit of switching around between
NodeId
,HirId
, andDefIndex
. These conversions are cheap except forHirId -> NodeId
, so if the valued reviewer finds such an instance in a performance critical place, please let me know.Ideally we convert more and more code from
NodeId
toHirId
in the future so that there are no moreNodeId
s after HIR lowering anywhere. Then the amount of switching should be minimal again.r? @eddyb, maybe?