Skip to content

Commit 84687a6

Browse files
committed
Seperate HIR owner
1 parent fbf1b1a commit 84687a6

File tree

47 files changed

+376
-234
lines changed

Some content is hidden

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

47 files changed

+376
-234
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
341341

342342
// Add a definition for the in-band const def.
343343
self.resolver.create_def(
344-
parent_def_id,
344+
parent_def_id.def_id,
345345
node_id,
346346
DefPathData::AnonConst,
347347
ExpnId::root(),

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
5353
ItemKind::Mod(..) => {
5454
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
5555
let old_current_module =
56-
mem::replace(&mut this.lctx.current_module, def_id);
56+
mem::replace(&mut this.lctx.current_module, def_id.def_id);
5757
visit::walk_item(this, item);
5858
this.lctx.current_module = old_current_module;
5959
}
@@ -404,7 +404,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
404404
let lowered_trait_def_id = self.lower_node_id(id).expect_owner();
405405
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
406406
ast_generics,
407-
lowered_trait_def_id,
407+
lowered_trait_def_id.def_id,
408408
AnonymousLifetimeMode::CreateParameter,
409409
|this, _| {
410410
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
@@ -416,7 +416,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
416416
this.trait_impls
417417
.entry(def_id)
418418
.or_default()
419-
.push(lowered_trait_def_id);
419+
.push(lowered_trait_def_id.def_id);
420420
}
421421
}
422422

@@ -714,7 +714,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
714714
let fdec = &sig.decl;
715715
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
716716
generics,
717-
def_id,
717+
def_id.def_id,
718718
AnonymousLifetimeMode::PassThrough,
719719
|this, _| {
720720
(
@@ -832,8 +832,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
832832
}
833833
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => {
834834
let names = self.lower_fn_params_to_names(&sig.decl);
835-
let (generics, sig) =
836-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
835+
let (generics, sig) = self.lower_method_sig(
836+
generics,
837+
sig,
838+
trait_item_def_id.def_id,
839+
false,
840+
None,
841+
i.id,
842+
);
837843
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
838844
}
839845
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
@@ -843,7 +849,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
843849
let (generics, sig) = self.lower_method_sig(
844850
generics,
845851
sig,
846-
trait_item_def_id,
852+
trait_item_def_id.def_id,
847853
false,
848854
asyncness.opt_return_id(),
849855
i.id,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5151
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
5252
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5353
use rustc_hir::intravisit;
54-
use rustc_hir::{ConstArg, GenericArg, ParamName};
54+
use rustc_hir::{ConstArg, GenericArg, HirOwner, ParamName};
5555
use rustc_index::vec::{Idx, IndexVec};
5656
use rustc_session::lint::builtin::{BARE_TRAIT_OBJECTS, MISSING_ABI};
5757
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
@@ -166,7 +166,7 @@ struct LoweringContext<'a, 'hir: 'a> {
166166

167167
type_def_lifetime_params: DefIdMap<usize>,
168168

169-
current_hir_id_owner: (LocalDefId, u32),
169+
current_hir_id_owner: (HirOwner, u32),
170170
item_local_id_counters: NodeMap<u32>,
171171
node_id_to_hir_id: IndexVec<NodeId, Option<hir::HirId>>,
172172

@@ -223,7 +223,7 @@ enum ImplTraitContext<'b, 'a> {
223223
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
224224
///
225225
/// Newly generated parameters should be inserted into the given `Vec`.
226-
Universal(&'b mut Vec<hir::GenericParam<'a>>, LocalDefId),
226+
Universal(&'b mut Vec<hir::GenericParam<'a>>, HirOwner),
227227

228228
/// Treat `impl Trait` as shorthand for a new opaque type.
229229
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
@@ -322,7 +322,7 @@ pub fn lower_crate<'a, 'hir>(
322322
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
323323
type_def_lifetime_params: Default::default(),
324324
current_module: CRATE_DEF_ID,
325-
current_hir_id_owner: (CRATE_DEF_ID, 0),
325+
current_hir_id_owner: (HirOwner { def_id: CRATE_DEF_ID }, 0),
326326
item_local_id_counters: Default::default(),
327327
node_id_to_hir_id: IndexVec::new(),
328328
generator_kind: None,
@@ -594,7 +594,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
594594
.item_local_id_counters
595595
.insert(owner, HIR_ID_COUNTER_LOCKED)
596596
.unwrap_or_else(|| panic!("no `item_local_id_counters` entry for {:?}", owner));
597-
let def_id = self.resolver.local_def_id(owner);
597+
let def_id = HirOwner { def_id: self.resolver.local_def_id(owner) };
598598
let old_owner = std::mem::replace(&mut self.current_hir_id_owner, (def_id, counter));
599599
let ret = f(self);
600600
let (new_def_id, new_counter) =
@@ -637,10 +637,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
637637
debug_assert!(local_id != HIR_ID_COUNTER_LOCKED);
638638

639639
*local_id_counter += 1;
640-
let owner = this.resolver.opt_local_def_id(owner).expect(
641-
"you forgot to call `create_def` or are lowering node-IDs \
640+
let owner = HirOwner {
641+
def_id: this.resolver.opt_local_def_id(owner).expect(
642+
"you forgot to call `create_def` or are lowering node-IDs \
642643
that do not belong to the current owner",
643-
);
644+
),
645+
};
644646

645647
hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) }
646648
})
@@ -1133,7 +1135,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11331135

11341136
let impl_trait_node_id = self.resolver.next_node_id();
11351137
self.resolver.create_def(
1136-
parent_def_id,
1138+
parent_def_id.def_id,
11371139
impl_trait_node_id,
11381140
DefPathData::ImplTrait,
11391141
ExpnId::root(),
@@ -1201,7 +1203,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12011203

12021204
// Add a definition for the in-band const def.
12031205
self.resolver.create_def(
1204-
parent_def_id,
1206+
parent_def_id.def_id,
12051207
node_id,
12061208
DefPathData::AnonConst,
12071209
ExpnId::root(),
@@ -1512,18 +1514,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15121514
};
15131515

15141516
trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id);
1515-
lctx.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
1517+
lctx.generate_opaque_type(
1518+
HirOwner { def_id: opaque_ty_def_id },
1519+
opaque_ty_item,
1520+
span,
1521+
opaque_ty_span,
1522+
);
15161523

15171524
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1518-
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes)
1525+
hir::TyKind::OpaqueDef(
1526+
hir::ItemId { def_id: HirOwner { def_id: opaque_ty_def_id } },
1527+
lifetimes,
1528+
)
15191529
})
15201530
}
15211531

15221532
/// Registers a new opaque type with the proper `NodeId`s and
15231533
/// returns the lowered node-ID for the opaque type.
15241534
fn generate_opaque_type(
15251535
&mut self,
1526-
opaque_ty_id: LocalDefId,
1536+
opaque_ty_id: HirOwner,
15271537
opaque_ty_item: hir::OpaqueTy<'hir>,
15281538
span: Span,
15291539
opaque_ty_span: Span,
@@ -2015,7 +2025,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20152025
};
20162026

20172027
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
2018-
this.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
2028+
this.generate_opaque_type(
2029+
HirOwner { def_id: opaque_ty_def_id },
2030+
opaque_ty_item,
2031+
span,
2032+
opaque_ty_span,
2033+
);
20192034

20202035
lifetime_params
20212036
});
@@ -2060,8 +2075,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20602075
// Foo = impl Trait` is, internally, created as a child of the
20612076
// async fn, so the *type parameters* are inherited. It's
20622077
// only the lifetime parameters that we must supply.
2063-
let opaque_ty_ref =
2064-
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, generic_args);
2078+
let opaque_ty_ref = hir::TyKind::OpaqueDef(
2079+
hir::ItemId { def_id: HirOwner { def_id: opaque_ty_def_id } },
2080+
generic_args,
2081+
);
20652082
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
20662083
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
20672084
}

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ignore-tidy-filelength
22
use crate::def::{CtorKind, DefKind, Res};
33
use crate::def_id::DefId;
4-
crate use crate::hir_id::HirId;
4+
crate use crate::hir_id::{HirId, HirOwner};
55
use crate::{itemlikevisit, LangItem};
66

77
use rustc_ast::util::parser::ExprPrecedence;
@@ -753,7 +753,7 @@ impl Crate<'_> {
753753
pub struct MacroDef<'hir> {
754754
pub ident: Ident,
755755
pub vis: Visibility<'hir>,
756-
pub def_id: LocalDefId,
756+
pub def_id: HirOwner,
757757
pub span: Span,
758758
pub ast: ast::MacroDef,
759759
}
@@ -1994,7 +1994,7 @@ pub struct FnSig<'hir> {
19941994
// so it can fetched later.
19951995
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
19961996
pub struct TraitItemId {
1997-
pub def_id: LocalDefId,
1997+
pub def_id: HirOwner,
19981998
}
19991999

20002000
impl TraitItemId {
@@ -2012,7 +2012,7 @@ impl TraitItemId {
20122012
#[derive(Debug)]
20132013
pub struct TraitItem<'hir> {
20142014
pub ident: Ident,
2015-
pub def_id: LocalDefId,
2015+
pub def_id: HirOwner,
20162016
pub generics: Generics<'hir>,
20172017
pub kind: TraitItemKind<'hir>,
20182018
pub span: Span,
@@ -2055,9 +2055,10 @@ pub enum TraitItemKind<'hir> {
20552055
// The bodies for items are stored "out of line", in a separate
20562056
// hashmap in the `Crate`. Here we just record the hir-id of the item
20572057
// so it can fetched later.
2058+
/// TODO review
20582059
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
20592060
pub struct ImplItemId {
2060-
pub def_id: LocalDefId,
2061+
pub def_id: HirOwner,
20612062
}
20622063

20632064
impl ImplItemId {
@@ -2072,7 +2073,7 @@ impl ImplItemId {
20722073
#[derive(Debug)]
20732074
pub struct ImplItem<'hir> {
20742075
pub ident: Ident,
2075-
pub def_id: LocalDefId,
2076+
pub def_id: HirOwner,
20762077
pub vis: Visibility<'hir>,
20772078
pub defaultness: Defaultness,
20782079
pub generics: Generics<'hir>,
@@ -2653,9 +2654,10 @@ impl VariantData<'hir> {
26532654
// The bodies for items are stored "out of line", in a separate
26542655
// hashmap in the `Crate`. Here we just record the hir-id of the item
26552656
// so it can fetched later.
2657+
/// TODO review
26562658
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug, Hash)]
26572659
pub struct ItemId {
2658-
pub def_id: LocalDefId,
2660+
pub def_id: HirOwner,
26592661
}
26602662

26612663
impl ItemId {
@@ -2672,7 +2674,7 @@ impl ItemId {
26722674
#[derive(Debug)]
26732675
pub struct Item<'hir> {
26742676
pub ident: Ident,
2675-
pub def_id: LocalDefId,
2677+
pub def_id: HirOwner,
26762678
pub kind: ItemKind<'hir>,
26772679
pub vis: Visibility<'hir>,
26782680
pub span: Span,
@@ -2858,9 +2860,10 @@ pub enum AssocItemKind {
28582860
// The bodies for items are stored "out of line", in a separate
28592861
// hashmap in the `Crate`. Here we just record the hir-id of the item
28602862
// so it can fetched later.
2863+
// TODO review
28612864
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
28622865
pub struct ForeignItemId {
2863-
pub def_id: LocalDefId,
2866+
pub def_id: HirOwner,
28642867
}
28652868

28662869
impl ForeignItemId {
@@ -2890,7 +2893,7 @@ pub struct ForeignItemRef<'hir> {
28902893
pub struct ForeignItem<'hir> {
28912894
pub ident: Ident,
28922895
pub kind: ForeignItemKind<'hir>,
2893-
pub def_id: LocalDefId,
2896+
pub def_id: HirOwner,
28942897
pub span: Span,
28952898
pub vis: Visibility<'hir>,
28962899
}

0 commit comments

Comments
 (0)