1
Fork 0

put hir::AnonConst on the hir arena

This commit is contained in:
Oli Scherer 2024-04-26 12:57:02 +00:00
parent e7da0fa62f
commit 4dc46f7f17
11 changed files with 47 additions and 41 deletions

View file

@ -375,7 +375,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
} }
} }
fn visit_array_length(&mut self, len: &'hir ArrayLen) { fn visit_array_length(&mut self, len: &'hir ArrayLen<'hir>) {
match len { match len {
ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)), ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)),
ArrayLen::Body(..) => intravisit::walk_array_len(self, len), ArrayLen::Body(..) => intravisit::walk_array_len(self, len),

View file

@ -1588,11 +1588,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
}), }),
)), )),
)), )),
default: Some(hir::AnonConst { default: Some(self.arena.alloc(hir::AnonConst {
def_id: anon_const, def_id: anon_const,
hir_id: const_id, hir_id: const_id,
body: const_body, body: const_body,
}), })),
is_host_effect: true, is_host_effect: true,
}, },
colon_span: None, colon_span: None,

View file

@ -1181,10 +1181,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
tokens: None, tokens: None,
}; };
let ct = self.with_new_scopes(span, |this| hir::AnonConst { let ct = self.with_new_scopes(span, |this| {
self.arena.alloc(hir::AnonConst {
def_id, 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)),
})
}); });
return GenericArg::Const(ConstArg { return GenericArg::Const(ConstArg {
value: ct, value: ct,
@ -2318,7 +2321,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen { fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
match c.value.kind { match c.value.kind {
ExprKind::Underscore => { ExprKind::Underscore => {
if self.tcx.features().generic_arg_infer { if self.tcx.features().generic_arg_infer {
@ -2341,12 +2344,12 @@ 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 hir::AnonConst {
self.with_new_scopes(c.value.span, |this| hir::AnonConst { self.arena.alloc(self.with_new_scopes(c.value.span, |this| hir::AnonConst {
def_id: this.local_def_id(c.id), 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)),
}) }))
} }
fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource { fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
@ -2653,7 +2656,7 @@ impl<'hir> GenericArgsCtor<'hir> {
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id))); lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.args.push(hir::GenericArg::Const(hir::ConstArg { self.args.push(hir::GenericArg::Const(hir::ConstArg {
value: hir::AnonConst { def_id, hir_id, body }, value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body }),
span, span,
is_desugared_from_effects: true, is_desugared_from_effects: true,
})) }))

View file

@ -230,8 +230,8 @@ impl<'hir> PathSegment<'hir> {
} }
#[derive(Clone, Copy, Debug, HashStable_Generic)] #[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg { pub struct ConstArg<'hir> {
pub value: AnonConst, pub value: &'hir AnonConst,
pub span: Span, pub span: Span,
/// Indicates whether this comes from a `~const` desugaring. /// Indicates whether this comes from a `~const` desugaring.
pub is_desugared_from_effects: bool, pub is_desugared_from_effects: bool,
@ -253,7 +253,7 @@ impl InferArg {
pub enum GenericArg<'hir> { pub enum GenericArg<'hir> {
Lifetime(&'hir Lifetime), Lifetime(&'hir Lifetime),
Type(&'hir Ty<'hir>), Type(&'hir Ty<'hir>),
Const(ConstArg), Const(ConstArg<'hir>),
Infer(InferArg), Infer(InferArg),
} }
@ -491,7 +491,7 @@ pub enum GenericParamKind<'hir> {
Const { Const {
ty: &'hir Ty<'hir>, ty: &'hir Ty<'hir>,
/// Optional default value for the const generic param /// Optional default value for the const generic param
default: Option<AnonConst>, default: Option<&'hir AnonConst>,
is_host_effect: bool, is_host_effect: bool,
}, },
} }
@ -1563,12 +1563,12 @@ impl fmt::Display for ConstContext {
pub type Lit = Spanned<LitKind>; pub type Lit = Spanned<LitKind>;
#[derive(Copy, Clone, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum ArrayLen { pub enum ArrayLen<'hir> {
Infer(InferArg), Infer(InferArg),
Body(AnonConst), Body(&'hir AnonConst),
} }
impl ArrayLen { impl ArrayLen<'_> {
pub fn hir_id(&self) -> HirId { pub fn hir_id(&self) -> HirId {
match self { match self {
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => { ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
@ -1965,7 +1965,7 @@ pub enum ExprKind<'hir> {
/// ///
/// E.g., `[1; 5]`. The first expression is the element /// E.g., `[1; 5]`. The first expression is the element
/// to be repeated; the second is the number of times to repeat it. /// to be repeated; the second is the number of times to repeat it.
Repeat(&'hir Expr<'hir>, ArrayLen), Repeat(&'hir Expr<'hir>, ArrayLen<'hir>),
/// A suspension point for coroutines (i.e., `yield <expr>`). /// A suspension point for coroutines (i.e., `yield <expr>`).
Yield(&'hir Expr<'hir>, YieldSource), Yield(&'hir Expr<'hir>, YieldSource),
@ -2345,7 +2345,7 @@ pub struct TypeBinding<'hir> {
#[derive(Debug, Clone, Copy, HashStable_Generic)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub enum Term<'hir> { pub enum Term<'hir> {
Ty(&'hir Ty<'hir>), Ty(&'hir Ty<'hir>),
Const(AnonConst), Const(&'hir AnonConst),
} }
impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> { impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
@ -2354,8 +2354,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
} }
} }
impl<'hir> From<AnonConst> for Term<'hir> { impl<'hir> From<&'hir AnonConst> for Term<'hir> {
fn from(c: AnonConst) -> Self { fn from(c: &'hir AnonConst) -> Self {
Term::Const(c) Term::Const(c)
} }
} }
@ -2646,7 +2646,7 @@ pub enum TyKind<'hir> {
/// A variable length slice (i.e., `[T]`). /// A variable length slice (i.e., `[T]`).
Slice(&'hir Ty<'hir>), Slice(&'hir Ty<'hir>),
/// A fixed length array (i.e., `[T; n]`). /// A fixed length array (i.e., `[T; n]`).
Array(&'hir Ty<'hir>, ArrayLen), Array(&'hir Ty<'hir>, ArrayLen<'hir>),
/// A raw pointer (i.e., `*const T` or `*mut T`). /// A raw pointer (i.e., `*const T` or `*mut T`).
Ptr(MutTy<'hir>), Ptr(MutTy<'hir>),
/// A reference (i.e., `&'a T` or `&'a mut T`). /// A reference (i.e., `&'a T` or `&'a mut T`).
@ -2675,7 +2675,7 @@ pub enum TyKind<'hir> {
/// where `Bound` is a trait or a lifetime. /// where `Bound` is a trait or a lifetime.
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax), TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
/// Unused for now. /// Unused for now.
Typeof(AnonConst), Typeof(&'hir AnonConst),
/// `TyKind::Infer` means the type should be inferred instead of it having been /// `TyKind::Infer` means the type should be inferred instead of it having been
/// specified. This can appear anywhere in a type. /// specified. This can appear anywhere in a type.
Infer, Infer,
@ -2708,10 +2708,10 @@ pub enum InlineAsmOperand<'hir> {
out_expr: Option<&'hir Expr<'hir>>, out_expr: Option<&'hir Expr<'hir>>,
}, },
Const { Const {
anon_const: AnonConst, anon_const: &'hir AnonConst,
}, },
SymFn { SymFn {
anon_const: AnonConst, anon_const: &'hir AnonConst,
}, },
SymStatic { SymStatic {
path: QPath<'hir>, path: QPath<'hir>,
@ -2913,7 +2913,7 @@ pub struct Variant<'hir> {
/// Fields and constructor id of the variant. /// Fields and constructor id of the variant.
pub data: VariantData<'hir>, pub data: VariantData<'hir>,
/// Explicit discriminant (e.g., `Foo = 1`). /// Explicit discriminant (e.g., `Foo = 1`).
pub disr_expr: Option<AnonConst>, pub disr_expr: Option<&'hir AnonConst>,
/// Span /// Span
pub span: Span, pub span: Span,
} }
@ -3833,7 +3833,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<'_>, 32); static_assert_size!(GenericArg<'_>, 24);
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

@ -338,7 +338,7 @@ pub trait Visitor<'v>: Sized {
fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result { fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
walk_pat_field(self, f) walk_pat_field(self, f)
} }
fn visit_array_length(&mut self, len: &'v ArrayLen) -> Self::Result { fn visit_array_length(&mut self, len: &'v ArrayLen<'v>) -> Self::Result {
walk_array_len(self, len) walk_array_len(self, len)
} }
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result { fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
@ -703,7 +703,7 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
visitor.visit_pat(field.pat) visitor.visit_pat(field.pat)
} }
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) -> V::Result { pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen<'v>) -> V::Result {
match len { match len {
// FIXME: Use `visit_infer` here. // FIXME: Use `visit_infer` here.
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id), ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),

View file

@ -143,7 +143,7 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
_ => {} _ => {}
} }
} }
fn visit_array_length(&mut self, length: &'v hir::ArrayLen) { fn visit_array_length(&mut self, length: &'v hir::ArrayLen<'v>) {
if let hir::ArrayLen::Infer(inf) = length { if let hir::ArrayLen::Infer(inf) = length {
self.0.push(inf.span); self.0.push(inf.span);
} }

View file

@ -968,7 +968,7 @@ impl<'a> State<'a> {
self.print_else(elseopt) self.print_else(elseopt)
} }
fn print_array_length(&mut self, len: &hir::ArrayLen) { fn print_array_length(&mut self, len: &hir::ArrayLen<'_>) {
match len { match len {
hir::ArrayLen::Infer(..) => self.word("_"), hir::ArrayLen::Infer(..) => self.word("_"),
hir::ArrayLen::Body(ct) => self.print_anon_const(ct), hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
@ -1052,7 +1052,7 @@ impl<'a> State<'a> {
self.end() self.end()
} }
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen) { fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen<'_>) {
self.ibox(INDENT_UNIT); self.ibox(INDENT_UNIT);
self.word("["); self.word("[");
self.print_expr(element); self.print_expr(element);

View file

@ -1444,7 +1444,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return; return;
}; };
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length && let hir::ArrayLen::Body(&hir::AnonConst { hir_id, .. }) = length
{ {
let span = self.tcx.hir().span(hir_id); let span = self.tcx.hir().span(hir_id);
self.dcx().try_steal_modify_and_emit_err( self.dcx().try_steal_modify_and_emit_err(
@ -1483,7 +1483,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn check_expr_repeat( fn check_expr_repeat(
&self, &self,
element: &'tcx hir::Expr<'tcx>, element: &'tcx hir::Expr<'tcx>,
count: &'tcx hir::ArrayLen, count: &'tcx hir::ArrayLen<'tcx>,
expected: Expectation<'tcx>, expected: Expectation<'tcx>,
expr: &'tcx hir::Expr<'tcx>, expr: &'tcx hir::Expr<'tcx>,
) -> Ty<'tcx> { ) -> Ty<'tcx> {

View file

@ -439,7 +439,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
} }
pub fn lower_array_length(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> { pub fn lower_array_length(&self, length: &hir::ArrayLen<'tcx>) -> ty::Const<'tcx> {
match length { match length {
hir::ArrayLen::Infer(inf) => self.ct_infer(self.tcx.types.usize, None, inf.span), hir::ArrayLen::Infer(inf) => self.ct_infer(self.tcx.types.usize, None, inf.span),
hir::ArrayLen::Body(anon_const) => { hir::ArrayLen::Body(anon_const) => {

View file

@ -281,7 +281,10 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
Lifetime(lifetime.ident.name) Lifetime(lifetime.ident.name)
} }
pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg, cx: &mut DocContext<'tcx>) -> Constant { pub(crate) fn clean_const<'tcx>(
constant: &hir::ConstArg<'_>,
cx: &mut DocContext<'tcx>,
) -> Constant {
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id(); let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
Constant { Constant {
type_: Box::new(clean_middle_ty( type_: Box::new(clean_middle_ty(
@ -2450,7 +2453,7 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
fn clean_variant_data<'tcx>( fn clean_variant_data<'tcx>(
variant: &hir::VariantData<'tcx>, variant: &hir::VariantData<'tcx>,
disr_expr: &Option<hir::AnonConst>, disr_expr: &Option<&hir::AnonConst>,
cx: &mut DocContext<'tcx>, cx: &mut DocContext<'tcx>,
) -> Variant { ) -> Variant {
let discriminant = disr_expr let discriminant = disr_expr

View file

@ -224,7 +224,7 @@ impl HirEqInterExpr<'_, '_, '_> {
}) })
} }
pub fn eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool { pub fn eq_array_length(&mut self, left: ArrayLen<'_>, right: ArrayLen<'_>) -> bool {
match (left, right) { match (left, right) {
(ArrayLen::Infer(..), ArrayLen::Infer(..)) => true, (ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
(ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_body(l_ct.body, r_ct.body), (ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_body(l_ct.body, r_ct.body),
@ -1116,7 +1116,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
} }
} }
pub fn hash_array_length(&mut self, length: ArrayLen) { pub fn hash_array_length(&mut self, length: ArrayLen<'_>) {
match length { match length {
ArrayLen::Infer(..) => {}, ArrayLen::Infer(..) => {},
ArrayLen::Body(anon_const) => self.hash_body(anon_const.body), ArrayLen::Body(anon_const) => self.hash_body(anon_const.body),