Skip to content

Commit d0ece44

Browse files
committed
Auto merge of #102040 - TaKO8Ki:separate-definitions-and-hir-owners, r=cjgillot
Separate definitions and HIR owners in the type system Fixes #83158 r? `@cjgillot`
2 parents 3f83906 + 8fe9360 commit d0ece44

File tree

114 files changed

+659
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+659
-518
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
237237
// Wrap the expression in an AnonConst.
238238
let parent_def_id = self.current_hir_id_owner;
239239
let node_id = self.next_node_id();
240-
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
240+
self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst);
241241
let anon_const = AnonConst { id: node_id, value: P(expr) };
242242
hir::InlineAsmOperand::SymFn {
243243
anon_const: self.lower_anon_const(&anon_const),

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
359359
let node_id = self.next_node_id();
360360

361361
// Add a definition for the in-band const def.
362-
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
362+
self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst);
363363

364364
let anon_const = AnonConst { id: node_id, value: arg };
365365
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));

compiler/rustc_ast_lowering/src/index.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2424
/// The parent of this node
2525
parent_node: hir::ItemLocalId,
2626

27-
owner: LocalDefId,
27+
owner: OwnerId,
2828

2929
definitions: &'a definitions::Definitions,
3030
}
@@ -81,9 +81,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
8181
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
8282
self.source_map.span_to_diagnostic_string(span),
8383
node,
84-
self.definitions.def_path(self.owner).to_string_no_crate_verbose(),
84+
self.definitions.def_path(self.owner.def_id).to_string_no_crate_verbose(),
8585
self.owner,
86-
self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
86+
self.definitions.def_path(hir_id.owner.def_id).to_string_no_crate_verbose(),
8787
hir_id.owner,
8888
)
8989
}
@@ -112,19 +112,19 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
112112
113113
fn visit_nested_item(&mut self, item: ItemId) {
114114
debug!("visit_nested_item: {:?}", item);
115-
self.insert_nested(item.def_id);
115+
self.insert_nested(item.def_id.def_id);
116116
}
117117

118118
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
119-
self.insert_nested(item_id.def_id);
119+
self.insert_nested(item_id.def_id.def_id);
120120
}
121121

122122
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
123-
self.insert_nested(item_id.def_id);
123+
self.insert_nested(item_id.def_id.def_id);
124124
}
125125

126126
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
127-
self.insert_nested(foreign_id.def_id);
127+
self.insert_nested(foreign_id.def_id.def_id);
128128
}
129129

130130
fn visit_nested_body(&mut self, id: BodyId) {

compiler/rustc_ast_lowering/src/item.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
6868
bodies: Vec::new(),
6969
attrs: SortedMap::default(),
7070
children: FxHashMap::default(),
71-
current_hir_id_owner: CRATE_DEF_ID,
71+
current_hir_id_owner: hir::CRATE_OWNER_ID,
7272
item_local_id_counter: hir::ItemLocalId::new(0),
7373
node_id_to_local_id: Default::default(),
7474
local_id_to_def_id: SortedMap::new(),
@@ -177,7 +177,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
177177
}
178178

179179
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
180-
let mut node_ids = smallvec![hir::ItemId { def_id: self.local_def_id(i.id) }];
180+
let mut node_ids =
181+
smallvec![hir::ItemId { def_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }];
181182
if let ItemKind::Use(ref use_tree) = &i.kind {
182183
self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids);
183184
}
@@ -193,7 +194,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
193194
match tree.kind {
194195
UseTreeKind::Nested(ref nested_vec) => {
195196
for &(ref nested, id) in nested_vec {
196-
vec.push(hir::ItemId { def_id: self.local_def_id(id) });
197+
vec.push(hir::ItemId {
198+
def_id: hir::OwnerId { def_id: self.local_def_id(id) },
199+
});
197200
self.lower_item_id_use_tree(nested, id, vec);
198201
}
199202
}
@@ -202,7 +205,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
202205
for (_, &id) in
203206
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
204207
{
205-
vec.push(hir::ItemId { def_id: self.local_def_id(id) });
208+
vec.push(hir::ItemId {
209+
def_id: hir::OwnerId { def_id: self.local_def_id(id) },
210+
});
206211
}
207212
}
208213
}
@@ -553,7 +558,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
553558
}
554559

555560
let item = hir::Item {
556-
def_id: new_id,
561+
def_id: hir::OwnerId { def_id: new_id },
557562
ident: this.lower_ident(ident),
558563
kind,
559564
vis_span,
@@ -627,7 +632,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
627632
}
628633

629634
let item = hir::Item {
630-
def_id: new_hir_id,
635+
def_id: hir::OwnerId { def_id: new_hir_id },
631636
ident: this.lower_ident(ident),
632637
kind,
633638
vis_span,
@@ -689,7 +694,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
689694

690695
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
691696
hir::ForeignItemRef {
692-
id: hir::ForeignItemId { def_id: self.local_def_id(i.id) },
697+
id: hir::ForeignItemId { def_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
693698
ident: self.lower_ident(i.ident),
694699
span: self.lower_span(i.span),
695700
}
@@ -851,7 +856,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
851856
}
852857
AssocItemKind::MacCall(..) => unimplemented!(),
853858
};
854-
let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
859+
let id = hir::TraitItemId { def_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
855860
hir::TraitItemRef {
856861
id,
857862
ident: self.lower_ident(i.ident),
@@ -931,7 +936,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
931936

932937
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
933938
hir::ImplItemRef {
934-
id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
939+
id: hir::ImplItemId { def_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
935940
ident: self.lower_ident(i.ident),
936941
span: self.lower_span(i.span),
937942
kind: match &i.kind {

compiler/rustc_ast_lowering/src/lib.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct LoweringContext<'a, 'hir> {
126126
is_in_trait_impl: bool,
127127
is_in_dyn_type: bool,
128128

129-
current_hir_id_owner: LocalDefId,
129+
current_hir_id_owner: hir::OwnerId,
130130
item_local_id_counter: hir::ItemLocalId,
131131
local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
132132
trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
@@ -572,7 +572,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
572572
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
573573
let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
574574
let current_trait_map = std::mem::take(&mut self.trait_map);
575-
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
575+
let current_owner =
576+
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
576577
let current_local_counter =
577578
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
578579
let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
@@ -587,7 +588,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
587588
debug_assert_eq!(_old, None);
588589

589590
let item = f(self);
590-
debug_assert_eq!(def_id, item.def_id());
591+
debug_assert_eq!(def_id, item.def_id().def_id);
591592
// `f` should have consumed all the elements in these vectors when constructing `item`.
592593
debug_assert!(self.impl_trait_defs.is_empty());
593594
debug_assert!(self.impl_trait_bounds.is_empty());
@@ -786,7 +787,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
786787
/// Mark a span as relative to the current owning item.
787788
fn lower_span(&self, span: Span) -> Span {
788789
if self.tcx.sess.opts.unstable_opts.incremental_relative_spans {
789-
span.with_parent(Some(self.current_hir_id_owner))
790+
span.with_parent(Some(self.current_hir_id_owner.def_id))
790791
} else {
791792
// Do not make spans relative when not using incremental compilation.
792793
span
@@ -812,7 +813,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
812813
LifetimeRes::Fresh { param, .. } => {
813814
// Late resolution delegates to us the creation of the `LocalDefId`.
814815
let _def_id = self.create_def(
815-
self.current_hir_id_owner,
816+
self.current_hir_id_owner.def_id,
816817
param,
817818
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
818819
);
@@ -1062,7 +1063,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10621063

10631064
let parent_def_id = self.current_hir_id_owner;
10641065
let impl_trait_node_id = self.next_node_id();
1065-
self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait);
1066+
self.create_def(
1067+
parent_def_id.def_id,
1068+
impl_trait_node_id,
1069+
DefPathData::ImplTrait,
1070+
);
10661071

10671072
self.with_dyn_type_scope(false, |this| {
10681073
let node_id = this.next_node_id();
@@ -1154,7 +1159,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11541159
let node_id = self.next_node_id();
11551160

11561161
// Add a definition for the in-band const def.
1157-
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
1162+
self.create_def(
1163+
parent_def_id.def_id,
1164+
node_id,
1165+
DefPathData::AnonConst,
1166+
);
11581167

11591168
let span = self.lower_span(ty.span);
11601169
let path_expr = Expr {
@@ -1551,7 +1560,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15511560
debug!(?lifetimes);
15521561

15531562
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1554-
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes, in_trait)
1563+
hir::TyKind::OpaqueDef(
1564+
hir::ItemId { def_id: hir::OwnerId { def_id: opaque_ty_def_id } },
1565+
lifetimes,
1566+
in_trait,
1567+
)
15551568
}
15561569

15571570
/// Registers a new opaque type with the proper `NodeId`s and
@@ -1567,7 +1580,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15671580
// Generate an `type Foo = impl Trait;` declaration.
15681581
trace!("registering opaque type with id {:#?}", opaque_ty_id);
15691582
let opaque_ty_item = hir::Item {
1570-
def_id: opaque_ty_id,
1583+
def_id: hir::OwnerId { def_id: opaque_ty_id },
15711584
ident: Ident::empty(),
15721585
kind: opaque_ty_item_kind,
15731586
vis_span: self.lower_span(span.shrink_to_lo()),
@@ -2018,7 +2031,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20182031
// async fn, so the *type parameters* are inherited. It's
20192032
// only the lifetime parameters that we must supply.
20202033
let opaque_ty_ref = hir::TyKind::OpaqueDef(
2021-
hir::ItemId { def_id: opaque_ty_def_id },
2034+
hir::ItemId { def_id: hir::OwnerId { def_id: opaque_ty_def_id } },
20222035
generic_args,
20232036
in_trait,
20242037
);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
932932
let opt_suggestions = self
933933
.infcx
934934
.tcx
935-
.typeck(path_segment.hir_id.owner)
935+
.typeck(path_segment.hir_id.owner.def_id)
936936
.type_dependent_def_id(*hir_id)
937937
.and_then(|def_id| self.infcx.tcx.impl_of_method(def_id))
938938
.map(|def_id| self.infcx.tcx.associated_items(def_id))
@@ -1032,7 +1032,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10321032
if look_at_return && hir.get_return_block(closure_id).is_some() {
10331033
// ...otherwise we are probably in the tail expression of the function, point at the
10341034
// return type.
1035-
match hir.get_by_def_id(hir.get_parent_item(fn_call_id)) {
1035+
match hir.get_by_def_id(hir.get_parent_item(fn_call_id).def_id) {
10361036
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
10371037
| hir::Node::TraitItem(hir::TraitItem {
10381038
ident,

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
281281
let tcx = self.infcx.tcx;
282282
match tcx.hir().get_if_local(def_id) {
283283
Some(Node::ImplItem(impl_item)) => {
284-
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) {
284+
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
285+
{
285286
Some(Node::Item(Item {
286287
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
287288
..
@@ -291,7 +292,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
291292
}
292293
Some(Node::TraitItem(trait_item)) => {
293294
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
294-
match tcx.hir().find_by_def_id(trait_did) {
295+
match tcx.hir().find_by_def_id(trait_did.def_id) {
295296
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
296297
// The method being called is defined in the `trait`, but the `'static`
297298
// obligation comes from the `impl`. Find that `impl` so that we can point

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
707707
hir::AsyncGeneratorKind::Block => " of async block",
708708
hir::AsyncGeneratorKind::Closure => " of async closure",
709709
hir::AsyncGeneratorKind::Fn => {
710-
let parent_item = hir.get_by_def_id(hir.get_parent_item(mir_hir_id));
710+
let parent_item =
711+
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
711712
let output = &parent_item
712713
.fn_decl()
713714
.expect("generator lowered from async fn should be in fn")

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn mir_borrowck<'tcx>(
134134

135135
let opt_closure_req = tcx
136136
.infer_ctxt()
137-
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner))
137+
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
138138
.enter(|infcx| {
139139
let input_body: &Body<'_> = &input_body.borrow();
140140
let promoted: &IndexVec<_, _> = &promoted.borrow();

compiler/rustc_driver/src/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
329329
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
330330
self.tcx
331331
.hir()
332-
.maybe_body_owned_by(expr.hir_id.owner)
332+
.maybe_body_owned_by(expr.hir_id.owner.def_id)
333333
.map(|body_id| self.tcx.typeck_body(body_id))
334334
});
335335

0 commit comments

Comments
 (0)