Skip to content

Commit 09cd00f

Browse files
committed
Auto merge of #124401 - oli-obk:some_hir_cleanups, r=cjgillot
Some hir cleanups It seemed odd to not put `AnonConst` in the arena, compared with the other types that we did put into an arena. This way we can also give it a `Span` without growing a lot of other HIR data structures because of the extra field. r? compiler
2 parents d2d24e3 + fea1fe7 commit 09cd00f

File tree

16 files changed

+89
-87
lines changed

16 files changed

+89
-87
lines changed

compiler/rustc_ast_lowering/src/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(super) fn index_hir<'hir>(
6262
if let Node::Err(span) = node.node {
6363
let hir_id = HirId { owner: item.def_id(), local_id };
6464
let msg = format!("ID {hir_id} not encountered when visiting item HIR");
65-
tcx.dcx().span_delayed_bug(*span, msg);
65+
tcx.dcx().span_delayed_bug(span, msg);
6666
}
6767
}
6868

@@ -376,7 +376,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
376376
}
377377
}
378378

379-
fn visit_array_length(&mut self, len: &'hir ArrayLen) {
379+
fn visit_array_length(&mut self, len: &'hir ArrayLen<'hir>) {
380380
match len {
381381
ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)),
382382
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),

compiler/rustc_ast_lowering/src/item.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1589,11 +1589,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
15891589
}),
15901590
)),
15911591
)),
1592-
default: Some(hir::AnonConst {
1592+
default: Some(self.arena.alloc(hir::AnonConst {
15931593
def_id: anon_const,
15941594
hir_id: const_id,
15951595
body: const_body,
1596-
}),
1596+
span,
1597+
})),
15971598
is_host_effect: true,
15981599
},
15991600
colon_span: None,

compiler/rustc_ast_lowering/src/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1178,14 +1178,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11781178
tokens: None,
11791179
};
11801180

1181-
let ct = self.with_new_scopes(span, |this| hir::AnonConst {
1182-
def_id,
1183-
hir_id: this.lower_node_id(node_id),
1184-
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
1181+
let ct = self.with_new_scopes(span, |this| {
1182+
self.arena.alloc(hir::AnonConst {
1183+
def_id,
1184+
hir_id: this.lower_node_id(node_id),
1185+
body: this
1186+
.lower_const_body(path_expr.span, Some(&path_expr)),
1187+
span,
1188+
})
11851189
});
11861190
return GenericArg::Const(ConstArg {
11871191
value: ct,
1188-
span,
11891192
is_desugared_from_effects: false,
11901193
});
11911194
}
@@ -1197,7 +1200,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11971200
}
11981201
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
11991202
value: self.lower_anon_const(ct),
1200-
span: self.lower_span(ct.value.span),
12011203
is_desugared_from_effects: false,
12021204
}),
12031205
}
@@ -2315,7 +2317,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23152317
}
23162318

23172319
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
2318-
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
2320+
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
23192321
match c.value.kind {
23202322
ExprKind::Underscore => {
23212323
if self.tcx.features().generic_arg_infer {
@@ -2338,12 +2340,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23382340
}
23392341
}
23402342

2341-
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
2342-
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
2343+
fn lower_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2344+
self.arena.alloc(self.with_new_scopes(c.value.span, |this| hir::AnonConst {
23432345
def_id: this.local_def_id(c.id),
23442346
hir_id: this.lower_node_id(c.id),
23452347
body: this.lower_const_body(c.value.span, Some(&c.value)),
2346-
})
2348+
span: this.lower_span(c.value.span),
2349+
}))
23472350
}
23482351

23492352
fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
@@ -2650,8 +2653,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26502653

26512654
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26522655
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2653-
value: hir::AnonConst { def_id, hir_id, body },
2654-
span,
2656+
value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body, span }),
26552657
is_desugared_from_effects: true,
26562658
}))
26572659
}

compiler/rustc_hir/src/hir.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,8 @@ impl<'hir> PathSegment<'hir> {
229229
}
230230

231231
#[derive(Clone, Copy, Debug, HashStable_Generic)]
232-
pub struct ConstArg {
233-
pub value: AnonConst,
234-
pub span: Span,
232+
pub struct ConstArg<'hir> {
233+
pub value: &'hir AnonConst,
235234
/// Indicates whether this comes from a `~const` desugaring.
236235
pub is_desugared_from_effects: bool,
237236
}
@@ -252,7 +251,7 @@ impl InferArg {
252251
pub enum GenericArg<'hir> {
253252
Lifetime(&'hir Lifetime),
254253
Type(&'hir Ty<'hir>),
255-
Const(ConstArg),
254+
Const(ConstArg<'hir>),
256255
Infer(InferArg),
257256
}
258257

@@ -261,7 +260,7 @@ impl GenericArg<'_> {
261260
match self {
262261
GenericArg::Lifetime(l) => l.ident.span,
263262
GenericArg::Type(t) => t.span,
264-
GenericArg::Const(c) => c.span,
263+
GenericArg::Const(c) => c.value.span,
265264
GenericArg::Infer(i) => i.span,
266265
}
267266
}
@@ -490,7 +489,7 @@ pub enum GenericParamKind<'hir> {
490489
Const {
491490
ty: &'hir Ty<'hir>,
492491
/// Optional default value for the const generic param
493-
default: Option<AnonConst>,
492+
default: Option<&'hir AnonConst>,
494493
is_host_effect: bool,
495494
},
496495
}
@@ -1562,12 +1561,12 @@ impl fmt::Display for ConstContext {
15621561
pub type Lit = Spanned<LitKind>;
15631562

15641563
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1565-
pub enum ArrayLen {
1564+
pub enum ArrayLen<'hir> {
15661565
Infer(InferArg),
1567-
Body(AnonConst),
1566+
Body(&'hir AnonConst),
15681567
}
15691568

1570-
impl ArrayLen {
1569+
impl ArrayLen<'_> {
15711570
pub fn hir_id(&self) -> HirId {
15721571
match self {
15731572
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
@@ -1590,6 +1589,7 @@ pub struct AnonConst {
15901589
pub hir_id: HirId,
15911590
pub def_id: LocalDefId,
15921591
pub body: BodyId,
1592+
pub span: Span,
15931593
}
15941594

15951595
/// An inline constant expression `const { something }`.
@@ -2002,7 +2002,7 @@ pub enum ExprKind<'hir> {
20022002
///
20032003
/// E.g., `[1; 5]`. The first expression is the element
20042004
/// to be repeated; the second is the number of times to repeat it.
2005-
Repeat(&'hir Expr<'hir>, ArrayLen),
2005+
Repeat(&'hir Expr<'hir>, ArrayLen<'hir>),
20062006

20072007
/// A suspension point for coroutines (i.e., `yield <expr>`).
20082008
Yield(&'hir Expr<'hir>, YieldSource),
@@ -2382,7 +2382,7 @@ pub struct TypeBinding<'hir> {
23822382
#[derive(Debug, Clone, Copy, HashStable_Generic)]
23832383
pub enum Term<'hir> {
23842384
Ty(&'hir Ty<'hir>),
2385-
Const(AnonConst),
2385+
Const(&'hir AnonConst),
23862386
}
23872387

23882388
impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
@@ -2391,8 +2391,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
23912391
}
23922392
}
23932393

2394-
impl<'hir> From<AnonConst> for Term<'hir> {
2395-
fn from(c: AnonConst) -> Self {
2394+
impl<'hir> From<&'hir AnonConst> for Term<'hir> {
2395+
fn from(c: &'hir AnonConst) -> Self {
23962396
Term::Const(c)
23972397
}
23982398
}
@@ -2683,7 +2683,7 @@ pub enum TyKind<'hir> {
26832683
/// A variable length slice (i.e., `[T]`).
26842684
Slice(&'hir Ty<'hir>),
26852685
/// A fixed length array (i.e., `[T; n]`).
2686-
Array(&'hir Ty<'hir>, ArrayLen),
2686+
Array(&'hir Ty<'hir>, ArrayLen<'hir>),
26872687
/// A raw pointer (i.e., `*const T` or `*mut T`).
26882688
Ptr(MutTy<'hir>),
26892689
/// A reference (i.e., `&'a T` or `&'a mut T`).
@@ -2712,7 +2712,7 @@ pub enum TyKind<'hir> {
27122712
/// where `Bound` is a trait or a lifetime.
27132713
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
27142714
/// Unused for now.
2715-
Typeof(AnonConst),
2715+
Typeof(&'hir AnonConst),
27162716
/// `TyKind::Infer` means the type should be inferred instead of it having been
27172717
/// specified. This can appear anywhere in a type.
27182718
Infer,
@@ -2745,10 +2745,10 @@ pub enum InlineAsmOperand<'hir> {
27452745
out_expr: Option<&'hir Expr<'hir>>,
27462746
},
27472747
Const {
2748-
anon_const: AnonConst,
2748+
anon_const: &'hir AnonConst,
27492749
},
27502750
SymFn {
2751-
anon_const: AnonConst,
2751+
anon_const: &'hir AnonConst,
27522752
},
27532753
SymStatic {
27542754
path: QPath<'hir>,
@@ -2950,7 +2950,7 @@ pub struct Variant<'hir> {
29502950
/// Fields and constructor id of the variant.
29512951
pub data: VariantData<'hir>,
29522952
/// Explicit discriminant (e.g., `Foo = 1`).
2953-
pub disr_expr: Option<AnonConst>,
2953+
pub disr_expr: Option<&'hir AnonConst>,
29542954
/// Span
29552955
pub span: Span,
29562956
}
@@ -3479,15 +3479,13 @@ impl<'hir> OwnerNode<'hir> {
34793479
}
34803480
}
34813481

3482-
// Span by reference to pass to `Node::Err`.
3483-
#[allow(rustc::pass_by_value)]
3484-
pub fn span(&self) -> &'hir Span {
3482+
pub fn span(&self) -> Span {
34853483
match self {
34863484
OwnerNode::Item(Item { span, .. })
34873485
| OwnerNode::ForeignItem(ForeignItem { span, .. })
34883486
| OwnerNode::ImplItem(ImplItem { span, .. })
3489-
| OwnerNode::TraitItem(TraitItem { span, .. }) => span,
3490-
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => inner_span,
3487+
| OwnerNode::TraitItem(TraitItem { span, .. }) => *span,
3488+
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => *inner_span,
34913489
OwnerNode::Synthetic => unreachable!(),
34923490
}
34933491
}
@@ -3632,9 +3630,7 @@ pub enum Node<'hir> {
36323630
PreciseCapturingNonLifetimeArg(&'hir PreciseCapturingNonLifetimeArg),
36333631
// Created by query feeding
36343632
Synthetic,
3635-
// Span by reference to minimize `Node`'s size
3636-
#[allow(rustc::pass_by_value)]
3637-
Err(&'hir Span),
3633+
Err(Span),
36383634
}
36393635

36403636
impl<'hir> Node<'hir> {
@@ -3871,7 +3867,7 @@ mod size_asserts {
38713867
static_assert_size!(FnDecl<'_>, 40);
38723868
static_assert_size!(ForeignItem<'_>, 72);
38733869
static_assert_size!(ForeignItemKind<'_>, 40);
3874-
static_assert_size!(GenericArg<'_>, 32);
3870+
static_assert_size!(GenericArg<'_>, 24);
38753871
static_assert_size!(GenericBound<'_>, 48);
38763872
static_assert_size!(Generics<'_>, 56);
38773873
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub trait Visitor<'v>: Sized {
338338
fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
339339
walk_pat_field(self, f)
340340
}
341-
fn visit_array_length(&mut self, len: &'v ArrayLen) -> Self::Result {
341+
fn visit_array_length(&mut self, len: &'v ArrayLen<'v>) -> Self::Result {
342342
walk_array_len(self, len)
343343
}
344344
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<'
703703
visitor.visit_pat(field.pat)
704704
}
705705

706-
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) -> V::Result {
706+
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen<'v>) -> V::Result {
707707
match len {
708708
// FIXME: Use `visit_infer` here.
709709
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
143143
_ => {}
144144
}
145145
}
146-
fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
146+
fn visit_array_length(&mut self, length: &'v hir::ArrayLen<'v>) {
147147
if let hir::ArrayLen::Infer(inf) = length {
148148
self.0.push(inf.span);
149149
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
2424
let hir_id = tcx.local_def_id_to_hir_id(def_id);
2525

2626
let node = tcx.hir_node(hir_id);
27-
let Node::AnonConst(_) = node else {
27+
let Node::AnonConst(&AnonConst { span, .. }) = node else {
2828
span_bug!(
2929
tcx.def_span(def_id),
3030
"expected anon const in `anon_const_type_of`, got {node:?}"
@@ -134,7 +134,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
134134
// I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
135135
return Ty::new_error_with_message(
136136
tcx,
137-
tcx.def_span(def_id),
137+
span,
138138
"unexpected non-GAT usage of an anon const",
139139
);
140140
}
@@ -152,7 +152,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
152152
let Some(type_dependent_def) = tables.type_dependent_def_id(parent_node_id) else {
153153
return Ty::new_error_with_message(
154154
tcx,
155-
tcx.def_span(def_id),
155+
span,
156156
format!("unable to find type-dependent def for {parent_node_id:?}"),
157157
);
158158
};
@@ -194,15 +194,15 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
194194
} else {
195195
return Ty::new_error_with_message(
196196
tcx,
197-
tcx.def_span(def_id),
197+
span,
198198
format!("unable to find const parent for {hir_id} in pat {pat:?}"),
199199
);
200200
}
201201
}
202202
_ => {
203203
return Ty::new_error_with_message(
204204
tcx,
205-
tcx.def_span(def_id),
205+
span,
206206
format!("unexpected const parent path {parent_node:?}"),
207207
);
208208
}
@@ -226,19 +226,15 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
226226
.map(|idx| (idx, seg))
227227
})
228228
}) else {
229-
return Ty::new_error_with_message(
230-
tcx,
231-
tcx.def_span(def_id),
232-
"no arg matching AnonConst in path",
233-
);
229+
return Ty::new_error_with_message(tcx, span, "no arg matching AnonConst in path");
234230
};
235231

236232
let generics = match tcx.res_generics_def_id(segment.res) {
237233
Some(def_id) => tcx.generics_of(def_id),
238234
None => {
239235
return Ty::new_error_with_message(
240236
tcx,
241-
tcx.def_span(def_id),
237+
span,
242238
format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
243239
);
244240
}
@@ -250,7 +246,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
250246
_ => {
251247
return Ty::new_error_with_message(
252248
tcx,
253-
tcx.def_span(def_id),
249+
span,
254250
format!("unexpected const parent in type_of(): {parent_node:?}"),
255251
);
256252
}
@@ -278,7 +274,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
278274
} else {
279275
return Ty::new_error_with_message(
280276
tcx,
281-
tcx.def_span(def_id),
277+
span,
282278
format!("const generic parameter not found in {generics:?} at position {arg_idx:?}"),
283279
);
284280
}

0 commit comments

Comments
 (0)