Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a7fe502

Browse files
committed
Auto merge of rust-lang#124401 - oli-obk:some_hir_cleanups, r=<try>
Some hir cleanups r? `@ghost`
2 parents 5ff8fbb + e4f8b93 commit a7fe502

File tree

13 files changed

+64
-59
lines changed

13 files changed

+64
-59
lines changed

compiler/rustc_ast_lowering/src/index.rs

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

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

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

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,11 +1588,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
15881588
}),
15891589
)),
15901590
)),
1591-
default: Some(hir::AnonConst {
1591+
default: Some(self.arena.alloc(hir::AnonConst {
15921592
def_id: anon_const,
15931593
hir_id: const_id,
15941594
body: const_body,
1595-
}),
1595+
span,
1596+
})),
15961597
is_host_effect: true,
15971598
},
15981599
colon_span: None,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,14 +1181,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11811181
tokens: None,
11821182
};
11831183

1184-
let ct = self.with_new_scopes(span, |this| hir::AnonConst {
1185-
def_id,
1186-
hir_id: this.lower_node_id(node_id),
1187-
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
1184+
let ct = self.with_new_scopes(span, |this| {
1185+
self.arena.alloc(hir::AnonConst {
1186+
def_id,
1187+
hir_id: this.lower_node_id(node_id),
1188+
body: this
1189+
.lower_const_body(path_expr.span, Some(&path_expr)),
1190+
span,
1191+
})
11881192
});
11891193
return GenericArg::Const(ConstArg {
11901194
value: ct,
1191-
span,
11921195
is_desugared_from_effects: false,
11931196
});
11941197
}
@@ -1200,7 +1203,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12001203
}
12011204
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
12021205
value: self.lower_anon_const(ct),
1203-
span: self.lower_span(ct.value.span),
12041206
is_desugared_from_effects: false,
12051207
}),
12061208
}
@@ -2318,7 +2320,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23182320
}
23192321

23202322
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
2321-
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
2323+
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
23222324
match c.value.kind {
23232325
ExprKind::Underscore => {
23242326
if self.tcx.features().generic_arg_infer {
@@ -2341,12 +2343,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23412343
}
23422344
}
23432345

2344-
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
2345-
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
2346+
fn lower_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2347+
self.arena.alloc(self.with_new_scopes(c.value.span, |this| hir::AnonConst {
23462348
def_id: this.local_def_id(c.id),
23472349
hir_id: this.lower_node_id(c.id),
23482350
body: this.lower_const_body(c.value.span, Some(&c.value)),
2349-
})
2351+
span: this.lower_span(c.value.span),
2352+
}))
23502353
}
23512354

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

26542657
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26552658
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2656-
value: hir::AnonConst { def_id, hir_id, body },
2657-
span,
2659+
value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body, span }),
26582660
is_desugared_from_effects: true,
26592661
}))
26602662
}

compiler/rustc_hir/src/hir.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,8 @@ impl<'hir> PathSegment<'hir> {
230230
}
231231

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

@@ -262,7 +261,7 @@ impl GenericArg<'_> {
262261
match self {
263262
GenericArg::Lifetime(l) => l.ident.span,
264263
GenericArg::Type(t) => t.span,
265-
GenericArg::Const(c) => c.span,
264+
GenericArg::Const(c) => c.value.span,
266265
GenericArg::Infer(i) => i.span,
267266
}
268267
}
@@ -491,7 +490,7 @@ pub enum GenericParamKind<'hir> {
491490
Const {
492491
ty: &'hir Ty<'hir>,
493492
/// Optional default value for the const generic param
494-
default: Option<AnonConst>,
493+
default: Option<&'hir AnonConst>,
495494
is_host_effect: bool,
496495
},
497496
}
@@ -1563,12 +1562,12 @@ impl fmt::Display for ConstContext {
15631562
pub type Lit = Spanned<LitKind>;
15641563

15651564
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1566-
pub enum ArrayLen {
1565+
pub enum ArrayLen<'hir> {
15671566
Infer(InferArg),
1568-
Body(AnonConst),
1567+
Body(&'hir AnonConst),
15691568
}
15701569

1571-
impl ArrayLen {
1570+
impl ArrayLen<'_> {
15721571
pub fn hir_id(&self) -> HirId {
15731572
match self {
15741573
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
@@ -1591,6 +1590,7 @@ pub struct AnonConst {
15911590
pub hir_id: HirId,
15921591
pub def_id: LocalDefId,
15931592
pub body: BodyId,
1593+
pub span: Span,
15941594
}
15951595

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

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

23892389
impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
@@ -2392,8 +2392,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
23922392
}
23932393
}
23942394

2395-
impl<'hir> From<AnonConst> for Term<'hir> {
2396-
fn from(c: AnonConst) -> Self {
2395+
impl<'hir> From<&'hir AnonConst> for Term<'hir> {
2396+
fn from(c: &'hir AnonConst) -> Self {
23972397
Term::Const(c)
23982398
}
23992399
}
@@ -2684,7 +2684,7 @@ pub enum TyKind<'hir> {
26842684
/// A variable length slice (i.e., `[T]`).
26852685
Slice(&'hir Ty<'hir>),
26862686
/// A fixed length array (i.e., `[T; n]`).
2687-
Array(&'hir Ty<'hir>, ArrayLen),
2687+
Array(&'hir Ty<'hir>, ArrayLen<'hir>),
26882688
/// A raw pointer (i.e., `*const T` or `*mut T`).
26892689
Ptr(MutTy<'hir>),
26902690
/// A reference (i.e., `&'a T` or `&'a mut T`).
@@ -2713,7 +2713,7 @@ pub enum TyKind<'hir> {
27132713
/// where `Bound` is a trait or a lifetime.
27142714
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
27152715
/// Unused for now.
2716-
Typeof(AnonConst),
2716+
Typeof(&'hir AnonConst),
27172717
/// `TyKind::Infer` means the type should be inferred instead of it having been
27182718
/// specified. This can appear anywhere in a type.
27192719
Infer,
@@ -2746,10 +2746,10 @@ pub enum InlineAsmOperand<'hir> {
27462746
out_expr: Option<&'hir Expr<'hir>>,
27472747
},
27482748
Const {
2749-
anon_const: AnonConst,
2749+
anon_const: &'hir AnonConst,
27502750
},
27512751
SymFn {
2752-
anon_const: AnonConst,
2752+
anon_const: &'hir AnonConst,
27532753
},
27542754
SymStatic {
27552755
path: QPath<'hir>,
@@ -2951,7 +2951,7 @@ pub struct Variant<'hir> {
29512951
/// Fields and constructor id of the variant.
29522952
pub data: VariantData<'hir>,
29532953
/// Explicit discriminant (e.g., `Foo = 1`).
2954-
pub disr_expr: Option<AnonConst>,
2954+
pub disr_expr: Option<&'hir AnonConst>,
29552955
/// Span
29562956
pub span: Span,
29572957
}
@@ -3480,15 +3480,13 @@ impl<'hir> OwnerNode<'hir> {
34803480
}
34813481
}
34823482

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

36413637
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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_pretty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl<'a> State<'a> {
968968
self.print_else(elseopt)
969969
}
970970

971-
fn print_array_length(&mut self, len: &hir::ArrayLen) {
971+
fn print_array_length(&mut self, len: &hir::ArrayLen<'_>) {
972972
match len {
973973
hir::ArrayLen::Infer(..) => self.word("_"),
974974
hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
@@ -1052,7 +1052,7 @@ impl<'a> State<'a> {
10521052
self.end()
10531053
}
10541054

1055-
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen) {
1055+
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen<'_>) {
10561056
self.ibox(INDENT_UNIT);
10571057
self.word("[");
10581058
self.print_expr(element);

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14441444
return;
14451445
};
14461446
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
1447-
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
1447+
&& let hir::ArrayLen::Body(&hir::AnonConst { hir_id, .. }) = length
14481448
{
14491449
let span = self.tcx.hir().span(hir_id);
14501450
self.dcx().try_steal_modify_and_emit_err(
@@ -1483,7 +1483,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14831483
fn check_expr_repeat(
14841484
&self,
14851485
element: &'tcx hir::Expr<'tcx>,
1486-
count: &'tcx hir::ArrayLen,
1486+
count: &'tcx hir::ArrayLen<'tcx>,
14871487
expected: Expectation<'tcx>,
14881488
expr: &'tcx hir::Expr<'tcx>,
14891489
) -> Ty<'tcx> {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439439
}
440440
}
441441

442-
pub fn lower_array_length(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> {
442+
pub fn lower_array_length(&self, length: &hir::ArrayLen<'tcx>) -> ty::Const<'tcx> {
443443
match length {
444444
hir::ArrayLen::Infer(inf) => self.ct_infer(self.tcx.types.usize, None, inf.span),
445445
hir::ArrayLen::Body(anon_const) => {

compiler/rustc_lint/src/pass_by_value.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
7373
GenericArg::Type(ty) => {
7474
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_else(|_| "_".into())
7575
}
76-
GenericArg::Const(c) => {
77-
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_else(|_| "_".into())
78-
}
76+
GenericArg::Const(c) => cx
77+
.tcx
78+
.sess
79+
.source_map()
80+
.span_to_snippet(c.value.span)
81+
.unwrap_or_else(|_| "_".into()),
7982
GenericArg::Infer(_) => String::from("_"),
8083
})
8184
.collect::<Vec<_>>();

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ impl<'hir> Map<'hir> {
912912
Node::ArrayLenInfer(inf) => inf.span,
913913
Node::PreciseCapturingNonLifetimeArg(param) => param.ident.span,
914914
Node::Synthetic => unreachable!(),
915-
Node::Err(span) => *span,
915+
Node::Err(span) => span,
916916
}
917917
}
918918

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
281281
Lifetime(lifetime.ident.name)
282282
}
283283

284-
pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg, cx: &mut DocContext<'tcx>) -> Constant {
284+
pub(crate) fn clean_const<'tcx>(
285+
constant: &hir::ConstArg<'_>,
286+
cx: &mut DocContext<'tcx>,
287+
) -> Constant {
285288
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
286289
Constant {
287290
type_: Box::new(clean_middle_ty(
@@ -2450,7 +2453,7 @@ pub(crate) fn clean_variant_def_with_args<'tcx>(
24502453

24512454
fn clean_variant_data<'tcx>(
24522455
variant: &hir::VariantData<'tcx>,
2453-
disr_expr: &Option<hir::AnonConst>,
2456+
disr_expr: &Option<&hir::AnonConst>,
24542457
cx: &mut DocContext<'tcx>,
24552458
) -> Variant {
24562459
let discriminant = disr_expr

0 commit comments

Comments
 (0)