1
Fork 0

Store a LocalDefId in hir::AnonConst.

This commit is contained in:
Camille GILLOT 2022-11-06 19:17:57 +00:00
parent 18482f7b23
commit 607d0c2a14
12 changed files with 49 additions and 56 deletions

View file

@ -1160,7 +1160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let node_id = self.next_node_id(); let node_id = self.next_node_id();
// Add a definition for the in-band const def. // Add a definition for the in-band const def.
self.create_def( let def_id = self.create_def(
parent_def_id.def_id, parent_def_id.def_id,
node_id, node_id,
DefPathData::AnonConst, DefPathData::AnonConst,
@ -1176,6 +1176,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}; };
let ct = self.with_new_scopes(|this| hir::AnonConst { let ct = self.with_new_scopes(|this| hir::AnonConst {
def_id,
hir_id: this.lower_node_id(node_id), hir_id: this.lower_node_id(node_id),
body: this.lower_const_body(path_expr.span, Some(&path_expr)), body: this.lower_const_body(path_expr.span, Some(&path_expr)),
}); });
@ -2346,6 +2347,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst { fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
self.with_new_scopes(|this| 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), hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)), body: this.lower_const_body(c.value.span, Some(&c.value)),
}) })

View file

@ -40,12 +40,12 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
.iter() .iter()
.map(|(op, op_sp)| match *op { .map(|(op, op_sp)| match *op {
hir::InlineAsmOperand::Const { ref anon_const } => { hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id = let const_value = cx
cx.tcx().hir().local_def_id(anon_const.hir_id).to_def_id(); .tcx()
let const_value = .const_eval_poly(anon_const.def_id.to_def_id())
cx.tcx().const_eval_poly(anon_const_def_id).unwrap_or_else( .unwrap_or_else(|_| {
|_| span_bug!(*op_sp, "asm const cannot be resolved"), span_bug!(*op_sp, "asm const cannot be resolved")
); });
let ty = cx let ty = cx
.tcx() .tcx()
.typeck_body(anon_const.body) .typeck_body(anon_const.body)

View file

@ -1617,7 +1617,7 @@ pub enum ArrayLen {
impl ArrayLen { impl ArrayLen {
pub fn hir_id(&self) -> HirId { pub fn hir_id(&self) -> HirId {
match self { 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,
} }
} }
} }
@ -1633,6 +1633,7 @@ impl ArrayLen {
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
pub struct AnonConst { pub struct AnonConst {
pub hir_id: HirId, pub hir_id: HirId,
pub def_id: LocalDefId,
pub body: BodyId, pub body: BodyId,
} }
@ -3550,7 +3551,7 @@ mod size_asserts {
static_assert_size!(FnDecl<'_>, 40); static_assert_size!(FnDecl<'_>, 40);
static_assert_size!(ForeignItem<'_>, 72); static_assert_size!(ForeignItem<'_>, 72);
static_assert_size!(ForeignItemKind<'_>, 40); static_assert_size!(ForeignItemKind<'_>, 40);
static_assert_size!(GenericArg<'_>, 24); static_assert_size!(GenericArg<'_>, 32);
static_assert_size!(GenericBound<'_>, 48); static_assert_size!(GenericBound<'_>, 48);
static_assert_size!(Generics<'_>, 56); static_assert_size!(Generics<'_>, 56);
static_assert_size!(Impl<'_>, 80); static_assert_size!(Impl<'_>, 80);

View file

@ -432,7 +432,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ty::Const::from_opt_const_arg_anon_const( ty::Const::from_opt_const_arg_anon_const(
tcx, tcx,
ty::WithOptConstParam { ty::WithOptConstParam {
did: tcx.hir().local_def_id(ct.value.hir_id), did: ct.value.def_id,
const_param_did: Some(param.def_id), const_param_did: Some(param.def_id),
}, },
) )
@ -570,8 +570,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ConvertedBindingKind::Equality(self.ast_ty_to_ty(ty).into()) ConvertedBindingKind::Equality(self.ast_ty_to_ty(ty).into())
} }
hir::Term::Const(ref c) => { 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(), c.def_id);
let c = Const::from_anon_const(self.tcx(), local_did);
ConvertedBindingKind::Equality(c.into()) ConvertedBindingKind::Equality(c.into())
} }
}, },
@ -2712,8 +2711,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let length = match length { let length = match length {
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span), &hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
hir::ArrayLen::Body(constant) => { hir::ArrayLen::Body(constant) => {
let length_def_id = tcx.hir().local_def_id(constant.hir_id); ty::Const::from_anon_const(tcx, constant.def_id)
ty::Const::from_anon_const(tcx, length_def_id)
} }
}; };
@ -2721,7 +2719,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.normalize_ty(ast_ty.span, array_ty) self.normalize_ty(ast_ty.span, array_ty)
} }
hir::TyKind::Typeof(ref e) => { 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, _| { let ty = tcx.fold_regions(ty_erased, |r, _| {
if r.is_erased() { tcx.lifetimes.re_static } else { r } if r.is_erased() { tcx.lifetimes.re_static } else { r }
}); });

View file

@ -297,9 +297,8 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
hir::GenericParamKind::Const { default, .. } => { hir::GenericParamKind::Const { default, .. } => {
self.tcx.ensure().type_of(param.def_id); self.tcx.ensure().type_of(param.def_id);
if let Some(default) = default { if let Some(default) = default {
let default_def_id = self.tcx.hir().local_def_id(default.hir_id);
// need to store default and type of default // need to store default and type of default
self.tcx.ensure().type_of(default_def_id); self.tcx.ensure().type_of(default.def_id);
self.tcx.ensure().const_param_default(param.def_id); self.tcx.ensure().const_param_default(param.def_id);
} }
} }
@ -877,7 +876,7 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
let discr = if let Some(ref e) = v.disr_expr { let discr = if let Some(ref e) = v.disr_expr {
distance_from_explicit = 0; distance_from_explicit = 0;
ty::VariantDiscr::Explicit(tcx.hir().local_def_id(e.hir_id).to_def_id()) ty::VariantDiscr::Explicit(e.def_id.to_def_id())
} else { } else {
ty::VariantDiscr::Relative(distance_from_explicit) ty::VariantDiscr::Relative(distance_from_explicit)
}; };

View file

@ -316,10 +316,9 @@ fn const_evaluatable_predicates_of<'tcx>(
impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> { impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) { fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
let def_id = self.tcx.hir().local_def_id(c.hir_id); let ct = ty::Const::from_anon_const(self.tcx, c.def_id);
let ct = ty::Const::from_anon_const(self.tcx, def_id);
if let ty::ConstKind::Unevaluated(_) = ct.kind() { if let ty::ConstKind::Unevaluated(_) = ct.kind() {
let span = self.tcx.hir().span(c.hir_id); let span = self.tcx.def_span(c.def_id);
self.preds.insert(( self.preds.insert((
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct)) ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
.to_predicate(self.tcx), .to_predicate(self.tcx),

View file

@ -488,9 +488,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match length { match length {
&hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span), &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
hir::ArrayLen::Body(anon_const) => { hir::ArrayLen::Body(anon_const) => {
let const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); let span = self.tcx.def_span(anon_const.def_id);
let span = self.tcx.hir().span(anon_const.hir_id); let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
let c = ty::Const::from_anon_const(self.tcx, const_def_id);
self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
self.normalize_associated_types_in(span, c) self.normalize_associated_types_in(span, c)
} }
@ -502,10 +501,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ast_c: &hir::AnonConst, ast_c: &hir::AnonConst,
param_def_id: DefId, param_def_id: DefId,
) -> ty::Const<'tcx> { ) -> ty::Const<'tcx> {
let const_def = ty::WithOptConstParam { let const_def =
did: self.tcx.hir().local_def_id(ast_c.hir_id), ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) };
const_param_did: Some(param_def_id),
};
let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def); let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def);
self.register_wf_obligation( self.register_wf_obligation(
c.into(), c.into(),

View file

@ -1407,7 +1407,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
} }
fn visit_anon_const(&mut self, c: &'hir AnonConst) { fn visit_anon_const(&mut self, c: &'hir AnonConst) {
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id)); self.body_owners.push(c.def_id);
intravisit::walk_anon_const(self, c) intravisit::walk_anon_const(self, c)
} }

View file

@ -266,9 +266,9 @@ impl<'tcx> Const<'tcx> {
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> { pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> {
let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) { let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) {
hir::Node::GenericParam(hir::GenericParam { hir::Node::GenericParam(hir::GenericParam {
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) }, kind: hir::GenericParamKind::Const { default: Some(ac), .. },
.. ..
}) => tcx.hir().local_def_id(ac.hir_id), }) => ac.def_id,
_ => span_bug!( _ => span_bug!(
tcx.def_span(def_id), tcx.def_span(def_id),
"`const_param_default` expected a generic parameter with a constant" "`const_param_default` expected a generic parameter with a constant"

View file

@ -608,24 +608,22 @@ impl<'tcx> Cx<'tcx> {
out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)), out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
}, },
hir::InlineAsmOperand::Const { ref anon_const } => { hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
let value = mir::ConstantKind::from_anon_const( let value = mir::ConstantKind::from_anon_const(
tcx, tcx,
anon_const_def_id, anon_const.def_id,
self.param_env, self.param_env,
); );
let span = tcx.hir().span(anon_const.hir_id); let span = tcx.def_span(anon_const.def_id);
InlineAsmOperand::Const { value, span } InlineAsmOperand::Const { value, span }
} }
hir::InlineAsmOperand::SymFn { ref anon_const } => { hir::InlineAsmOperand::SymFn { ref anon_const } => {
let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
let value = mir::ConstantKind::from_anon_const( let value = mir::ConstantKind::from_anon_const(
tcx, tcx,
anon_const_def_id, anon_const.def_id,
self.param_env, self.param_env,
); );
let span = tcx.hir().span(anon_const.hir_id); let span = tcx.def_span(anon_const.def_id);
InlineAsmOperand::SymFn { value, span } InlineAsmOperand::SymFn { value, span }
} }
@ -640,7 +638,7 @@ impl<'tcx> Cx<'tcx> {
hir::ExprKind::ConstBlock(ref anon_const) => { hir::ExprKind::ConstBlock(ref anon_const) => {
let ty = self.typeck_results().node_type(anon_const.hir_id); let ty = self.typeck_results().node_type(anon_const.hir_id);
let did = tcx.hir().local_def_id(anon_const.hir_id).to_def_id(); let did = anon_const.def_id.to_def_id();
let typeck_root_def_id = tcx.typeck_root_def_id(did); let typeck_root_def_id = tcx.typeck_root_def_id(did);
let parent_substs = let parent_substs =
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id)); tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));

View file

@ -565,8 +565,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
id: hir::HirId, id: hir::HirId,
span: Span, span: Span,
) -> PatKind<'tcx> { ) -> PatKind<'tcx> {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); let value = mir::ConstantKind::from_inline_const(self.tcx, anon_const.def_id);
let value = mir::ConstantKind::from_inline_const(self.tcx, anon_const_def_id);
// Evaluate early like we do in `lower_path`. // Evaluate early like we do in `lower_path`.
let value = value.eval(self.tcx, self.param_env); let value = value.eval(self.tcx, self.param_env);

View file

@ -119,33 +119,33 @@ hir-stats HIR STATS
hir-stats Name Accumulated Size Count Item Size hir-stats Name Accumulated Size Count Item Size
hir-stats ---------------------------------------------------------------- hir-stats ----------------------------------------------------------------
hir-stats ForeignItemRef 24 ( 0.3%) 1 24 hir-stats ForeignItemRef 24 ( 0.3%) 1 24
hir-stats Lifetime 32 ( 0.4%) 1 32 hir-stats Lifetime 32 ( 0.3%) 1 32
hir-stats Mod 32 ( 0.4%) 1 32 hir-stats Mod 32 ( 0.3%) 1 32
hir-stats ExprField 40 ( 0.4%) 1 40 hir-stats ExprField 40 ( 0.4%) 1 40
hir-stats TraitItemRef 56 ( 0.6%) 2 28 hir-stats TraitItemRef 56 ( 0.6%) 2 28
hir-stats Local 64 ( 0.7%) 1 64 hir-stats Local 64 ( 0.7%) 1 64
hir-stats Param 64 ( 0.7%) 2 32 hir-stats Param 64 ( 0.7%) 2 32
hir-stats InlineAsm 72 ( 0.8%) 1 72 hir-stats InlineAsm 72 ( 0.8%) 1 72
hir-stats ImplItemRef 72 ( 0.8%) 2 36 hir-stats ImplItemRef 72 ( 0.8%) 2 36
hir-stats Body 96 ( 1.1%) 3 32 hir-stats Body 96 ( 1.0%) 3 32
hir-stats GenericArg 96 ( 1.1%) 4 24 hir-stats FieldDef 96 ( 1.0%) 2 48
hir-stats - Type 24 ( 0.3%) 1 hir-stats Arm 96 ( 1.0%) 2 48
hir-stats - Lifetime 72 ( 0.8%) 3 hir-stats Stmt 96 ( 1.0%) 3 32
hir-stats FieldDef 96 ( 1.1%) 2 48 hir-stats - Local 32 ( 0.3%) 1
hir-stats Arm 96 ( 1.1%) 2 48 hir-stats - Semi 32 ( 0.3%) 1
hir-stats Stmt 96 ( 1.1%) 3 32 hir-stats - Expr 32 ( 0.3%) 1
hir-stats - Local 32 ( 0.4%) 1
hir-stats - Semi 32 ( 0.4%) 1
hir-stats - Expr 32 ( 0.4%) 1
hir-stats FnDecl 120 ( 1.3%) 3 40 hir-stats FnDecl 120 ( 1.3%) 3 40
hir-stats Attribute 128 ( 1.4%) 4 32 hir-stats Attribute 128 ( 1.4%) 4 32
hir-stats GenericArg 128 ( 1.4%) 4 32
hir-stats - Type 32 ( 0.3%) 1
hir-stats - Lifetime 96 ( 1.0%) 3
hir-stats GenericArgs 144 ( 1.6%) 3 48 hir-stats GenericArgs 144 ( 1.6%) 3 48
hir-stats Variant 160 ( 1.8%) 2 80 hir-stats Variant 160 ( 1.7%) 2 80
hir-stats GenericBound 192 ( 2.1%) 4 48 hir-stats GenericBound 192 ( 2.1%) 4 48
hir-stats - Trait 192 ( 2.1%) 4 hir-stats - Trait 192 ( 2.1%) 4
hir-stats WherePredicate 192 ( 2.1%) 3 64 hir-stats WherePredicate 192 ( 2.1%) 3 64
hir-stats - BoundPredicate 192 ( 2.1%) 3 hir-stats - BoundPredicate 192 ( 2.1%) 3
hir-stats Block 288 ( 3.2%) 6 48 hir-stats Block 288 ( 3.1%) 6 48
hir-stats Pat 360 ( 3.9%) 5 72 hir-stats Pat 360 ( 3.9%) 5 72
hir-stats - Wild 72 ( 0.8%) 1 hir-stats - Wild 72 ( 0.8%) 1
hir-stats - Struct 72 ( 0.8%) 1 hir-stats - Struct 72 ( 0.8%) 1
@ -169,10 +169,10 @@ hir-stats - Enum 80 ( 0.9%) 1
hir-stats - ExternCrate 80 ( 0.9%) 1 hir-stats - ExternCrate 80 ( 0.9%) 1
hir-stats - ForeignMod 80 ( 0.9%) 1 hir-stats - ForeignMod 80 ( 0.9%) 1
hir-stats - Impl 80 ( 0.9%) 1 hir-stats - Impl 80 ( 0.9%) 1
hir-stats - Fn 160 ( 1.8%) 2 hir-stats - Fn 160 ( 1.7%) 2
hir-stats - Use 400 ( 4.4%) 5 hir-stats - Use 400 ( 4.4%) 5
hir-stats Path 1_280 (14.0%) 32 40 hir-stats Path 1_280 (14.0%) 32 40
hir-stats PathSegment 1_920 (21.0%) 40 48 hir-stats PathSegment 1_920 (21.0%) 40 48
hir-stats ---------------------------------------------------------------- hir-stats ----------------------------------------------------------------
hir-stats Total 9_128 hir-stats Total 9_160
hir-stats hir-stats