Skip to content

Commit d572e6d

Browse files
committed
Add span field for ConstArg
1 parent af76a24 commit d572e6d

20 files changed

Lines changed: 111 additions & 78 deletions

File tree

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
312312

313313
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir, AmbigArg>) {
314314
self.insert(
315-
const_arg.as_unambig_ct().span(),
315+
const_arg.as_unambig_ct().span,
316316
const_arg.hir_id,
317317
Node::ConstArg(const_arg.as_unambig_ct()),
318318
);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,8 +2285,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22852285
// `ExprKind::Paren(ExprKind::Underscore)` and should also be lowered to `GenericArg::Infer`
22862286
match c.value.peel_parens().kind {
22872287
ExprKind::Underscore => {
2288-
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
2289-
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
2288+
let ct_kind = hir::ConstArgKind::Infer(());
2289+
self.arena.alloc(hir::ConstArg {
2290+
hir_id: self.lower_node_id(c.id),
2291+
kind: ct_kind,
2292+
span: self.lower_span(c.value.span),
2293+
})
22902294
}
22912295
_ => self.lower_anon_const_to_const_arg_and_alloc(c),
22922296
}
@@ -2356,7 +2360,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23562360
hir::ConstArgKind::Anon(ct)
23572361
};
23582362

2359-
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
2363+
self.arena.alloc(hir::ConstArg {
2364+
hir_id: self.next_id(),
2365+
kind: ct_kind,
2366+
span: self.lower_span(span),
2367+
})
23602368
}
23612369

23622370
fn lower_const_item_rhs(
@@ -2373,9 +2381,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23732381
let const_arg = ConstArg {
23742382
hir_id: self.next_id(),
23752383
kind: hir::ConstArgKind::Error(
2376-
DUMMY_SP,
23772384
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
23782385
),
2386+
span: DUMMY_SP,
23792387
};
23802388
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
23812389
}
@@ -2388,13 +2396,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23882396

23892397
#[instrument(level = "debug", skip(self), ret)]
23902398
fn lower_expr_to_const_arg_direct(&mut self, expr: &Expr) -> hir::ConstArg<'hir> {
2399+
let span = self.lower_span(expr.span);
2400+
23912401
let overly_complex_const = |this: &mut Self| {
23922402
let e = this.dcx().struct_span_err(
23932403
expr.span,
23942404
"complex const arguments must be placed inside of a `const` block",
23952405
);
23962406

2397-
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(expr.span, e.emit()) }
2407+
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(e.emit()), span }
23982408
};
23992409

24002410
match &expr.kind {
@@ -2425,6 +2435,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24252435
ConstArg {
24262436
hir_id: self.next_id(),
24272437
kind: hir::ConstArgKind::TupleCall(qpath, lowered_args),
2438+
span,
24282439
}
24292440
}
24302441
ExprKind::Tup(exprs) => {
@@ -2442,7 +2453,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24422453
&*self.arena.alloc(expr)
24432454
}));
24442455

2445-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(expr.span, exprs) }
2456+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(exprs), span }
24462457
}
24472458
ExprKind::Path(qself, path) => {
24482459
let qpath = self.lower_qpath(
@@ -2456,7 +2467,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24562467
None,
24572468
);
24582469

2459-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
2470+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath), span }
24602471
}
24612472
ExprKind::Struct(se) => {
24622473
let path = self.lower_qpath(
@@ -2497,11 +2508,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24972508
})
24982509
}));
24992510

2500-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2511+
ConstArg {
2512+
hir_id: self.next_id(),
2513+
kind: hir::ConstArgKind::Struct(path, fields),
2514+
span,
2515+
}
25012516
}
25022517
ExprKind::Underscore => ConstArg {
25032518
hir_id: self.lower_node_id(expr.id),
2504-
kind: hir::ConstArgKind::Infer(expr.span, ()),
2519+
kind: hir::ConstArgKind::Infer(()),
2520+
span,
25052521
},
25062522
ExprKind::Block(block, _) => {
25072523
if let [stmt] = block.stmts.as_slice()
@@ -2546,7 +2562,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25462562
return match anon.mgca_disambiguation {
25472563
MgcaDisambiguation::AnonConst => {
25482564
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2549-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2565+
ConstArg {
2566+
hir_id: self.next_id(),
2567+
kind: hir::ConstArgKind::Anon(lowered_anon),
2568+
span: lowered_anon.span,
2569+
}
25502570
}
25512571
MgcaDisambiguation::Direct => self.lower_expr_to_const_arg_direct(&anon.value),
25522572
};
@@ -2583,11 +2603,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25832603
return ConstArg {
25842604
hir_id: self.lower_node_id(anon.id),
25852605
kind: hir::ConstArgKind::Path(qpath),
2606+
span: self.lower_span(expr.span),
25862607
};
25872608
}
25882609

25892610
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2590-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2611+
ConstArg {
2612+
hir_id: self.next_id(),
2613+
kind: hir::ConstArgKind::Anon(lowered_anon),
2614+
span: self.lower_span(expr.span),
2615+
}
25912616
}
25922617

25932618
/// See [`hir::ConstArg`] for when to use this function vs

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
513513
self.arena.alloc(hir::ConstArg {
514514
hir_id: self.next_id(),
515515
kind: hir::ConstArgKind::Anon(self.arena.alloc(anon_const)),
516+
span,
516517
})
517518
}
518519

@@ -557,6 +558,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
557558
})
558559
});
559560
let hir_id = self.next_id();
560-
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id })
561+
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id, span })
561562
}
562563
}

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'hir> ConstItemRhs<'hir> {
423423
pub fn span<'tcx>(&self, tcx: impl crate::intravisit::HirTyCtxt<'tcx>) -> Span {
424424
match self {
425425
ConstItemRhs::Body(body_id) => tcx.hir_body(*body_id).value.span,
426-
ConstItemRhs::TypeConst(ct_arg) => ct_arg.span(),
426+
ConstItemRhs::TypeConst(ct_arg) => ct_arg.span,
427427
}
428428
}
429429
}
@@ -447,6 +447,7 @@ pub struct ConstArg<'hir, Unambig = ()> {
447447
#[stable_hasher(ignore)]
448448
pub hir_id: HirId,
449449
pub kind: ConstArgKind<'hir, Unambig>,
450+
pub span: Span,
450451
}
451452

452453
impl<'hir> ConstArg<'hir, AmbigArg> {
@@ -475,7 +476,7 @@ impl<'hir> ConstArg<'hir> {
475476
/// Functions accepting ambiguous consts will not handle the [`ConstArgKind::Infer`] variant, if
476477
/// infer consts are relevant to you then care should be taken to handle them separately.
477478
pub fn try_as_ambig_ct(&self) -> Option<&ConstArg<'hir, AmbigArg>> {
478-
if let ConstArgKind::Infer(_, ()) = self.kind {
479+
if let ConstArgKind::Infer(()) = self.kind {
479480
return None;
480481
}
481482

@@ -494,25 +495,13 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
494495
_ => None,
495496
}
496497
}
497-
498-
pub fn span(&self) -> Span {
499-
match self.kind {
500-
ConstArgKind::Tup(span, ..) => span,
501-
ConstArgKind::Struct(path, _) => path.span(),
502-
ConstArgKind::Path(path) => path.span(),
503-
ConstArgKind::TupleCall(path, _) => path.span(),
504-
ConstArgKind::Anon(anon) => anon.span,
505-
ConstArgKind::Error(span, _) => span,
506-
ConstArgKind::Infer(span, _) => span,
507-
}
508-
}
509498
}
510499

511500
/// See [`ConstArg`].
512501
#[derive(Clone, Copy, Debug, HashStable_Generic)]
513502
#[repr(u8, C)]
514503
pub enum ConstArgKind<'hir, Unambig = ()> {
515-
Tup(Span, &'hir [&'hir ConstArg<'hir, Unambig>]),
504+
Tup(&'hir [&'hir ConstArg<'hir, Unambig>]),
516505
/// **Note:** Currently this is only used for bare const params
517506
/// (`N` where `fn foo<const N: usize>(...)`),
518507
/// not paths to any const (`N` where `const N: usize = ...`).
@@ -525,10 +514,10 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
525514
/// Tuple constructor variant
526515
TupleCall(QPath<'hir>, &'hir [&'hir ConstArg<'hir>]),
527516
/// Error const
528-
Error(Span, ErrorGuaranteed),
517+
Error(ErrorGuaranteed),
529518
/// This variant is not always used to represent inference consts, sometimes
530519
/// [`GenericArg::Infer`] is used instead.
531-
Infer(Span, Unambig),
520+
Infer(Unambig),
532521
}
533522

534523
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -574,7 +563,7 @@ impl GenericArg<'_> {
574563
match self {
575564
GenericArg::Lifetime(l) => l.ident.span,
576565
GenericArg::Type(t) => t.span,
577-
GenericArg::Const(c) => c.span(),
566+
GenericArg::Const(c) => c.span,
578567
GenericArg::Infer(i) => i.span,
579568
}
580569
}

compiler/rustc_hir/src/hir/tests.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ define_tests! {
2424
cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
2525
cast_array TyKind Array {
2626
0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
27-
1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
27+
1: &ConstArg {
2828
hir_id: HirId::INVALID,
29-
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
30-
body: BodyId { hir_id: HirId::INVALID },
29+
kind: ConstArgKind::Anon(&AnonConst {
30+
hir_id: HirId::INVALID,
31+
def_id: LocalDefId { local_def_index: DefIndex::ZERO },
32+
body: BodyId { hir_id: HirId::INVALID },
33+
span: DUMMY_SP,
34+
}),
3135
span: DUMMY_SP,
32-
})}
36+
},
3337
}
3438

3539
cast_anon ConstArgKind Anon {

compiler/rustc_hir/src/intravisit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ pub fn walk_unambig_const_arg<'v, V: Visitor<'v>>(
10681068
match const_arg.try_as_ambig_ct() {
10691069
Some(ambig_ct) => visitor.visit_const_arg(ambig_ct),
10701070
None => {
1071-
let ConstArg { hir_id, kind: _ } = const_arg;
1072-
visitor.visit_infer(*hir_id, const_arg.span(), InferKind::Const(const_arg))
1071+
let ConstArg { hir_id, kind: _, span } = const_arg;
1072+
visitor.visit_infer(*hir_id, *span, InferKind::Const(const_arg))
10731073
}
10741074
}
10751075
}
@@ -1078,10 +1078,10 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10781078
visitor: &mut V,
10791079
const_arg: &'v ConstArg<'v, AmbigArg>,
10801080
) -> V::Result {
1081-
let ConstArg { hir_id, kind } = const_arg;
1081+
let ConstArg { hir_id, kind, span: _ } = const_arg;
10821082
try_visit!(visitor.visit_id(*hir_id));
10831083
match kind {
1084-
ConstArgKind::Tup(_, exprs) => {
1084+
ConstArgKind::Tup(exprs) => {
10851085
walk_list!(visitor, visit_const_arg, *exprs);
10861086
V::Result::output()
10871087
}
@@ -1103,7 +1103,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
11031103
}
11041104
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
11051105
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
1106-
ConstArgKind::Error(_, _) => V::Result::output(), // errors and spans are not important
1106+
ConstArgKind::Error(_) => V::Result::output(), // errors and spans are not important
11071107
}
11081108
}
11091109

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
381381
{
382382
let span = match term {
383383
hir::Term::Ty(ty) => ty.span,
384-
hir::Term::Const(ct) => ct.span(),
384+
hir::Term::Const(ct) => ct.span,
385385
};
386386
(span, Some(ident.span), assoc_item.as_tag(), assoc_tag)
387387
} else {
@@ -1466,7 +1466,7 @@ pub fn prohibit_assoc_item_constraint(
14661466
hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(c) },
14671467
GenericParamDefKind::Const { .. },
14681468
) => {
1469-
suggest_direct_use(&mut err, c.span());
1469+
suggest_direct_use(&mut err, c.span);
14701470
}
14711471
(hir::AssocItemConstraintKind::Bound { bounds }, _) => {
14721472
// Suggest `impl<T: Bound> Trait<T> for Foo` when finding

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23312331
&& (anon_const_type.has_free_regions() || anon_const_type.has_erased_regions())
23322332
{
23332333
let e = self.dcx().span_err(
2334-
const_arg.span(),
2334+
const_arg.span,
23352335
"anonymous constants with lifetimes in their type are not yet supported",
23362336
);
23372337
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2342,7 +2342,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23422342
// variables otherwise we will ICE.
23432343
if anon_const_type.has_non_region_infer() {
23442344
let e = self.dcx().span_err(
2345-
const_arg.span(),
2345+
const_arg.span,
23462346
"anonymous constants with inferred types are not yet supported",
23472347
);
23482348
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2352,7 +2352,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23522352
// give the anon const any of the generics from the parent.
23532353
if anon_const_type.has_non_region_param() {
23542354
let e = self.dcx().span_err(
2355-
const_arg.span(),
2355+
const_arg.span,
23562356
"anonymous constants referencing generics are not yet supported",
23572357
);
23582358
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2364,7 +2364,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23642364

23652365
let hir_id = const_arg.hir_id;
23662366
match const_arg.kind {
2367-
hir::ConstArgKind::Tup(span, exprs) => self.lower_const_arg_tup(exprs, feed, span),
2367+
hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, feed, const_arg.span),
23682368
hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
23692369
debug!(?maybe_qself, ?path);
23702370
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
@@ -2378,19 +2378,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23782378
hir_self_ty,
23792379
segment,
23802380
hir_id,
2381-
const_arg.span(),
2381+
const_arg.span,
23822382
)
23832383
.unwrap_or_else(|guar| Const::new_error(tcx, guar))
23842384
}
23852385
hir::ConstArgKind::Struct(qpath, inits) => {
2386-
self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span())
2386+
self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span)
23872387
}
23882388
hir::ConstArgKind::TupleCall(qpath, args) => {
2389-
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span())
2389+
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span)
23902390
}
23912391
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
2392-
hir::ConstArgKind::Infer(span, ()) => self.ct_infer(None, span),
2393-
hir::ConstArgKind::Error(_, e) => ty::Const::new_error(tcx, e),
2392+
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
2393+
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
23942394
}
23952395
}
23962396

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,21 +1141,21 @@ impl<'a> State<'a> {
11411141

11421142
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11431143
match &const_arg.kind {
1144-
ConstArgKind::Tup(_, exprs) => {
1144+
ConstArgKind::Tup(exprs) => {
11451145
self.popen();
11461146
self.commasep_cmnt(
11471147
Inconsistent,
11481148
exprs,
11491149
|s, arg| s.print_const_arg(arg),
1150-
|arg| arg.span(),
1150+
|arg| arg.span,
11511151
);
11521152
self.pclose();
11531153
}
11541154
ConstArgKind::Struct(qpath, fields) => self.print_const_struct(qpath, fields),
11551155
ConstArgKind::TupleCall(qpath, args) => self.print_const_ctor(qpath, args),
11561156
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11571157
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
1158-
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
1158+
ConstArgKind::Error(_) => self.word("/*ERROR*/"),
11591159
ConstArgKind::Infer(..) => self.word("_"),
11601160
}
11611161
}

0 commit comments

Comments
 (0)