Skip to content
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

Record LocalDefId in HIR nodes instead of a side table #104170

Merged
merged 8 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// `static |_task_context| -> <ret_ty> { body }`:
let generator_kind = {
let c = self.arena.alloc(hir::Closure {
def_id: self.local_def_id(closure_node_id),
binder: hir::ClosureBinder::Default,
capture_clause,
bound_generic_params: &[],
Expand Down Expand Up @@ -887,6 +888,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let fn_decl = self.lower_fn_decl(decl, None, fn_decl_span, FnDeclKind::Closure, None);

let c = self.arena.alloc(hir::Closure {
def_id: self.local_def_id(closure_id),
binder: binder_clause,
capture_clause,
bound_generic_params,
Expand Down Expand Up @@ -991,6 +993,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_fn_decl(&outer_decl, None, fn_decl_span, FnDeclKind::Closure, None);

let c = self.arena.alloc(hir::Closure {
def_id: self.local_def_id(closure_id),
binder: binder_clause,
capture_clause,
bound_generic_params,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_variant(&mut self, v: &'hir Variant<'hir>) {
self.insert(v.span, v.id, Node::Variant(v));
self.with_parent(v.id, |this| {
self.insert(v.span, v.hir_id, Node::Variant(v));
self.with_parent(v.hir_id, |this| {
// Register the constructor of this variant.
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
let id = self.lower_node_id(v.id);
self.lower_attrs(id, &v.attrs);
let hir_id = self.lower_node_id(v.id);
self.lower_attrs(hir_id, &v.attrs);
hir::Variant {
id,
data: self.lower_variant_data(id, &v.data),
hir_id,
def_id: self.local_def_id(v.id),
data: self.lower_variant_data(hir_id, &v.data),
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
ident: self.lower_ident(v.ident),
span: self.lower_span(v.span),
Expand All @@ -739,12 +740,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
fields.iter().enumerate().map(|f| self.lower_field_def(f)),
),
ctor_id,
self.local_def_id(id),
)
}
VariantData::Unit(id) => {
let ctor_id = self.lower_node_id(id);
self.alias_attrs(ctor_id, parent_id);
hir::VariantData::Unit(ctor_id)
hir::VariantData::Unit(ctor_id, self.local_def_id(id))
}
}
}
Expand All @@ -767,6 +769,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::FieldDef {
span: self.lower_span(f.span),
hir_id,
def_id: self.local_def_id(f.id),
ident: match f.ident {
Some(ident) => self.lower_ident(ident),
// FIXME(jseyfried): positional field hygiene.
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
),
};
let hir_id = self.lower_node_id(node_id);
let def_id = self.local_def_id(node_id);
Some(hir::GenericParam {
hir_id,
def_id,
name,
span: self.lower_span(ident.span),
pure_wrt_drop: false,
Expand Down Expand Up @@ -1158,7 +1160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let node_id = self.next_node_id();

// Add a definition for the in-band const def.
self.create_def(
let def_id = self.create_def(
parent_def_id.def_id,
node_id,
DefPathData::AnonConst,
Expand All @@ -1174,6 +1176,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
};

let ct = self.with_new_scopes(|this| hir::AnonConst {
def_id,
hir_id: this.lower_node_id(node_id),
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
});
Expand Down Expand Up @@ -1521,6 +1524,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

hir::GenericParam {
hir_id,
def_id: lctx.local_def_id(new_node_id),
name,
span: lifetime.ident.span,
pure_wrt_drop: false,
Expand Down Expand Up @@ -1978,6 +1982,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

hir::GenericParam {
hir_id,
def_id: this.local_def_id(new_node_id),
name,
span: lifetime.ident.span,
pure_wrt_drop: false,
Expand Down Expand Up @@ -2176,6 +2181,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.lower_attrs(hir_id, &param.attrs);
hir::GenericParam {
hir_id,
def_id: self.local_def_id(param.id),
name,
span: self.lower_span(param.span()),
pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
Expand Down Expand Up @@ -2280,6 +2286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Set the name to `impl Bound1 + Bound2`.
let param = hir::GenericParam {
hir_id: self.lower_node_id(node_id),
def_id,
name: ParamName::Plain(self.lower_ident(ident)),
pure_wrt_drop: false,
span: self.lower_span(span),
Expand Down Expand Up @@ -2340,6 +2347,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
self.with_new_scopes(|this| hir::AnonConst {
def_id: this.local_def_id(c.id),
hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)),
})
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_borrowck/src/type_check/input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
user_provided_sig = None;
} else {
let typeck_results = self.tcx().typeck(mir_def_id);
user_provided_sig = typeck_results.user_provided_sigs.get(&mir_def_id.to_def_id()).map(
|user_provided_poly_sig| {
user_provided_sig =
typeck_results.user_provided_sigs.get(&mir_def_id).map(|user_provided_poly_sig| {
// Instantiate the canonicalized variables from
// user-provided signature (e.g., the `_` in the code
// above) with fresh variables.
Expand All @@ -60,8 +60,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
LateBoundRegionConversionTime::FnCall,
poly_sig,
)
},
);
});
}

debug!(?normalized_input_tys, ?body.local_decls);
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_codegen_ssa/src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
.iter()
.map(|(op, op_sp)| match *op {
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id =
cx.tcx().hir().local_def_id(anon_const.hir_id).to_def_id();
let const_value =
cx.tcx().const_eval_poly(anon_const_def_id).unwrap_or_else(
|_| span_bug!(*op_sp, "asm const cannot be resolved"),
);
let const_value = cx
.tcx()
.const_eval_poly(anon_const.def_id.to_def_id())
.unwrap_or_else(|_| {
span_bug!(*op_sp, "asm const cannot be resolved")
});
let ty = cx
.tcx()
.typeck_body(anon_const.body)
Expand Down
29 changes: 21 additions & 8 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ pub enum GenericParamKind<'hir> {
#[derive(Debug, HashStable_Generic)]
pub struct GenericParam<'hir> {
pub hir_id: HirId,
pub def_id: LocalDefId,
pub name: ParamName,
pub span: Span,
pub pure_wrt_drop: bool,
Expand Down Expand Up @@ -921,6 +922,7 @@ pub struct Crate<'hir> {

#[derive(Debug, HashStable_Generic)]
pub struct Closure<'hir> {
pub def_id: LocalDefId,
pub binder: ClosureBinder,
pub capture_clause: CaptureBy,
pub bound_generic_params: &'hir [GenericParam<'hir>],
Expand Down Expand Up @@ -1615,7 +1617,7 @@ pub enum ArrayLen {
impl ArrayLen {
pub fn hir_id(&self) -> HirId {
match self {
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, body: _ }) => hir_id,
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, .. }) => hir_id,
}
}
}
Expand All @@ -1627,10 +1629,11 @@ impl ArrayLen {
/// explicit discriminant values for enum variants.
///
/// You can check if this anon const is a default in a const param
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_hir_id(..)`
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)`
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
pub struct AnonConst {
pub hir_id: HirId,
pub def_id: LocalDefId,
pub body: BodyId,
}

Expand Down Expand Up @@ -2798,7 +2801,8 @@ pub struct Variant<'hir> {
/// Name of the variant.
pub ident: Ident,
/// Id of the variant (not the constructor, see `VariantData::ctor_hir_id()`).
pub id: HirId,
pub hir_id: HirId,
pub def_id: LocalDefId,
/// Fields and constructor id of the variant.
pub data: VariantData<'hir>,
/// Explicit discriminant (e.g., `Foo = 1`).
Expand Down Expand Up @@ -2865,6 +2869,7 @@ pub struct FieldDef<'hir> {
pub vis_span: Span,
pub ident: Ident,
pub hir_id: HirId,
pub def_id: LocalDefId,
pub ty: &'hir Ty<'hir>,
}

Expand All @@ -2886,11 +2891,11 @@ pub enum VariantData<'hir> {
/// A tuple variant.
///
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
Tuple(&'hir [FieldDef<'hir>], HirId),
Tuple(&'hir [FieldDef<'hir>], HirId, LocalDefId),
/// A unit variant.
///
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
Unit(HirId),
Unit(HirId, LocalDefId),
}

impl<'hir> VariantData<'hir> {
Expand All @@ -2902,11 +2907,19 @@ impl<'hir> VariantData<'hir> {
}
}

/// Return the `LocalDefId` of this variant's constructor, if it has one.
pub fn ctor_def_id(&self) -> Option<LocalDefId> {
match *self {
VariantData::Struct(_, _) => None,
VariantData::Tuple(_, _, def_id) | VariantData::Unit(_, def_id) => Some(def_id),
}
}

/// Return the `HirId` of this variant's constructor, if it has one.
pub fn ctor_hir_id(&self) -> Option<HirId> {
match *self {
VariantData::Struct(_, _) => None,
VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id),
VariantData::Tuple(_, hir_id, _) | VariantData::Unit(hir_id, _) => Some(hir_id),
}
}
}
Expand Down Expand Up @@ -3532,7 +3545,7 @@ impl<'hir> Node<'hir> {
/// Get the fields for the tuple-constructor,
/// if this node is a tuple constructor, otherwise None
pub fn tuple_fields(&self) -> Option<&'hir [FieldDef<'hir>]> {
if let Node::Ctor(&VariantData::Tuple(fields, _)) = self { Some(fields) } else { None }
if let Node::Ctor(&VariantData::Tuple(fields, _, _)) = self { Some(fields) } else { None }
}
}

Expand All @@ -3548,7 +3561,7 @@ mod size_asserts {
static_assert_size!(FnDecl<'_>, 40);
static_assert_size!(ForeignItem<'_>, 72);
static_assert_size!(ForeignItemKind<'_>, 40);
static_assert_size!(GenericArg<'_>, 24);
static_assert_size!(GenericArg<'_>, 32);
static_assert_size!(GenericBound<'_>, 48);
static_assert_size!(Generics<'_>, 56);
static_assert_size!(Impl<'_>, 80);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
walk_list!(visitor, visit_arm, arms);
}
ExprKind::Closure(&Closure {
def_id: _,
binder: _,
bound_generic_params,
fn_decl,
Expand Down Expand Up @@ -1084,7 +1085,7 @@ pub fn walk_enum_def<'v, V: Visitor<'v>>(

pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
visitor.visit_ident(variant.ident);
visitor.visit_id(variant.id);
visitor.visit_id(variant.hir_id);
visitor.visit_variant_data(&variant.data);
walk_list!(visitor, visit_anon_const, &variant.disr_expr);
}
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ty::Const::from_opt_const_arg_anon_const(
tcx,
ty::WithOptConstParam {
did: tcx.hir().local_def_id(ct.value.hir_id),
did: ct.value.def_id,
const_param_did: Some(param.def_id),
},
)
Expand Down Expand Up @@ -570,8 +570,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ConvertedBindingKind::Equality(self.ast_ty_to_ty(ty).into())
}
hir::Term::Const(ref c) => {
let local_did = self.tcx().hir().local_def_id(c.hir_id);
let c = Const::from_anon_const(self.tcx(), local_did);
let c = Const::from_anon_const(self.tcx(), c.def_id);
ConvertedBindingKind::Equality(c.into())
}
},
Expand Down Expand Up @@ -856,7 +855,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
&self,
bounds: &mut Bounds<'hir>,
ast_bounds: &'hir [hir::GenericBound<'hir>],
self_ty_where_predicates: Option<(hir::HirId, &'hir [hir::WherePredicate<'hir>])>,
self_ty_where_predicates: Option<(LocalDefId, &'hir [hir::WherePredicate<'hir>])>,
span: Span,
) {
let tcx = self.tcx();
Expand All @@ -876,10 +875,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
};
search_bounds(ast_bounds);
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id();
for clause in where_clause {
if let hir::WherePredicate::BoundPredicate(pred) = clause {
if pred.is_param_bound(self_ty_def_id) {
if pred.is_param_bound(self_ty.to_def_id()) {
search_bounds(pred.bounds);
}
}
Expand Down Expand Up @@ -2713,16 +2711,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let length = match length {
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
hir::ArrayLen::Body(constant) => {
let length_def_id = tcx.hir().local_def_id(constant.hir_id);
ty::Const::from_anon_const(tcx, length_def_id)
ty::Const::from_anon_const(tcx, constant.def_id)
}
};

let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length));
self.normalize_ty(ast_ty.span, array_ty)
}
hir::TyKind::Typeof(ref e) => {
let ty_erased = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
let ty_erased = tcx.type_of(e.def_id);
let ty = tcx.fold_regions(ty_erased, |r, _| {
if r.is_erased() { tcx.lifetimes.re_static } else { r }
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {

// Const parameters are well formed if their type is structural match.
hir::GenericParamKind::Const { ty: hir_ty, default: _ } => {
let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id));
let ty = tcx.type_of(param.def_id);

if tcx.features().adt_const_params {
if let Some(non_structural_match_ty) =
Expand Down
Loading