Skip to content

Commit 7b9ed2a

Browse files
committed
Add span field for ConstArg
1 parent b0698b4 commit 7b9ed2a

18 files changed

Lines changed: 108 additions & 73 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: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,8 +2283,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22832283
// `ExprKind::Paren(ExprKind::Underscore)` and should also be lowered to `GenericArg::Infer`
22842284
match c.value.peel_parens().kind {
22852285
ExprKind::Underscore => {
2286-
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
2287-
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
2286+
let ct_kind = hir::ConstArgKind::Infer(());
2287+
self.arena.alloc(hir::ConstArg {
2288+
hir_id: self.lower_node_id(c.id),
2289+
kind: ct_kind,
2290+
span: self.lower_span(c.value.span),
2291+
})
22882292
}
22892293
_ => self.lower_anon_const_to_const_arg(c),
22902294
}
@@ -2354,7 +2358,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23542358
hir::ConstArgKind::Anon(ct)
23552359
};
23562360

2357-
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
2361+
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind, span })
23582362
}
23592363

23602364
fn lower_const_item_rhs(
@@ -2371,9 +2375,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23712375
let const_arg = ConstArg {
23722376
hir_id: self.next_id(),
23732377
kind: hir::ConstArgKind::Error(
2374-
DUMMY_SP,
23752378
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
23762379
),
2380+
span: DUMMY_SP,
23772381
};
23782382
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
23792383
}
@@ -2392,7 +2396,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23922396
"complex const arguments must be placed inside of a `const` block",
23932397
);
23942398

2395-
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(expr.span, e.emit()) }
2399+
ConstArg {
2400+
hir_id: this.next_id(),
2401+
kind: hir::ConstArgKind::Error(e.emit()),
2402+
span: expr.span,
2403+
}
23962404
};
23972405

23982406
match &expr.kind {
@@ -2423,6 +2431,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24232431
ConstArg {
24242432
hir_id: self.next_id(),
24252433
kind: hir::ConstArgKind::TupleCall(qpath, lowered_args),
2434+
span: expr.span,
24262435
}
24272436
}
24282437
ExprKind::Tup(exprs) => {
@@ -2440,7 +2449,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24402449
&*self.arena.alloc(expr)
24412450
}));
24422451

2443-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(expr.span, exprs) }
2452+
ConstArg {
2453+
hir_id: self.next_id(),
2454+
kind: hir::ConstArgKind::Tup(exprs),
2455+
span: expr.span,
2456+
}
24442457
}
24452458
ExprKind::Path(qself, path) => {
24462459
let qpath = self.lower_qpath(
@@ -2454,7 +2467,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24542467
None,
24552468
);
24562469

2457-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
2470+
ConstArg {
2471+
hir_id: self.next_id(),
2472+
kind: hir::ConstArgKind::Path(qpath),
2473+
span: expr.span,
2474+
}
24582475
}
24592476
ExprKind::Struct(se) => {
24602477
let path = self.lower_qpath(
@@ -2495,11 +2512,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24952512
})
24962513
}));
24972514

2498-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2515+
ConstArg {
2516+
hir_id: self.next_id(),
2517+
kind: hir::ConstArgKind::Struct(path, fields),
2518+
span: expr.span,
2519+
}
24992520
}
25002521
ExprKind::Underscore => ConstArg {
25012522
hir_id: self.lower_node_id(expr.id),
2502-
kind: hir::ConstArgKind::Infer(expr.span, ()),
2523+
kind: hir::ConstArgKind::Infer(()),
2524+
span: expr.span,
25032525
},
25042526
ExprKind::Block(block, _) => {
25052527
if let [stmt] = block.stmts.as_slice()
@@ -2541,7 +2563,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25412563
return match anon.mgca_disambiguation {
25422564
MgcaDisambiguation::AnonConst => {
25432565
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2544-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2566+
ConstArg {
2567+
hir_id: self.next_id(),
2568+
kind: hir::ConstArgKind::Anon(lowered_anon),
2569+
span: lowered_anon.span,
2570+
}
25452571
}
25462572
MgcaDisambiguation::Direct => self.lower_expr_to_const_arg_direct(&anon.value),
25472573
};
@@ -2578,11 +2604,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25782604
return ConstArg {
25792605
hir_id: self.lower_node_id(anon.id),
25802606
kind: hir::ConstArgKind::Path(qpath),
2607+
span: expr.span,
25812608
};
25822609
}
25832610

25842611
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2585-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(lowered_anon) }
2612+
ConstArg {
2613+
hir_id: self.next_id(),
2614+
kind: hir::ConstArgKind::Anon(lowered_anon),
2615+
span: expr.span,
2616+
}
25862617
}
25872618

25882619
/// 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
@@ -511,6 +511,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
511511
self.arena.alloc(hir::ConstArg {
512512
hir_id: self.next_id(),
513513
kind: hir::ConstArgKind::Anon(self.arena.alloc(anon_const)),
514+
span,
514515
})
515516
}
516517

@@ -555,6 +556,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
555556
})
556557
});
557558
let hir_id = self.next_id();
558-
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id })
559+
self.arena.alloc(hir::ConstArg { kind: hir::ConstArgKind::Anon(ct), hir_id, span })
559560
}
560561
}

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/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
@@ -2325,7 +2325,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23252325
&& (anon_const_type.has_free_regions() || anon_const_type.has_erased_regions())
23262326
{
23272327
let e = self.dcx().span_err(
2328-
const_arg.span(),
2328+
const_arg.span,
23292329
"anonymous constants with lifetimes in their type are not yet supported",
23302330
);
23312331
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2336,7 +2336,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23362336
// variables otherwise we will ICE.
23372337
if anon_const_type.has_non_region_infer() {
23382338
let e = self.dcx().span_err(
2339-
const_arg.span(),
2339+
const_arg.span,
23402340
"anonymous constants with inferred types are not yet supported",
23412341
);
23422342
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2346,7 +2346,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23462346
// give the anon const any of the generics from the parent.
23472347
if anon_const_type.has_non_region_param() {
23482348
let e = self.dcx().span_err(
2349-
const_arg.span(),
2349+
const_arg.span,
23502350
"anonymous constants referencing generics are not yet supported",
23512351
);
23522352
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
@@ -2358,7 +2358,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23582358

23592359
let hir_id = const_arg.hir_id;
23602360
match const_arg.kind {
2361-
hir::ConstArgKind::Tup(span, exprs) => self.lower_const_arg_tup(exprs, feed, span),
2361+
hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, feed, const_arg.span),
23622362
hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
23632363
debug!(?maybe_qself, ?path);
23642364
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
@@ -2372,19 +2372,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23722372
hir_self_ty,
23732373
segment,
23742374
hir_id,
2375-
const_arg.span(),
2375+
const_arg.span,
23762376
)
23772377
.unwrap_or_else(|guar| Const::new_error(tcx, guar))
23782378
}
23792379
hir::ConstArgKind::Struct(qpath, inits) => {
2380-
self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span())
2380+
self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span)
23812381
}
23822382
hir::ConstArgKind::TupleCall(qpath, args) => {
2383-
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span())
2383+
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span)
23842384
}
23852385
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
2386-
hir::ConstArgKind::Infer(span, ()) => self.ct_infer(None, span),
2387-
hir::ConstArgKind::Error(_, e) => ty::Const::new_error(tcx, e),
2386+
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
2387+
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
23882388
}
23892389
}
23902390

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,13 +1137,13 @@ impl<'a> State<'a> {
11371137

11381138
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11391139
match &const_arg.kind {
1140-
ConstArgKind::Tup(_, exprs) => {
1140+
ConstArgKind::Tup(exprs) => {
11411141
self.popen();
11421142
self.commasep_cmnt(
11431143
Inconsistent,
11441144
exprs,
11451145
|s, arg| s.print_const_arg(arg),
1146-
|arg| arg.span(),
1146+
|arg| arg.span,
11471147
);
11481148
self.pclose();
11491149
}
@@ -1152,7 +1152,7 @@ impl<'a> State<'a> {
11521152
ConstArgKind::TupleCall(..) => self.word("/* TUPLE CALL */"),
11531153
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11541154
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
1155-
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
1155+
ConstArgKind::Error(_) => self.word("/*ERROR*/"),
11561156
ConstArgKind::Infer(..) => self.word("_"),
11571157
}
11581158
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17051705
return;
17061706
};
17071707
if let hir::TyKind::Array(_, ct) = ty.peel_refs().kind {
1708-
let span = ct.span();
1708+
let span = ct.span;
17091709
self.dcx().try_steal_modify_and_emit_err(
17101710
span,
17111711
StashKey::UnderscoreForArrayLengths,
@@ -1746,7 +1746,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17461746
expr: &'tcx hir::Expr<'tcx>,
17471747
) -> Ty<'tcx> {
17481748
let tcx = self.tcx;
1749-
let count_span = count.span();
1749+
let count_span = count.span;
17501750
let count = self.try_structurally_resolve_const(
17511751
count_span,
17521752
self.normalize(count_span, self.lower_const_arg(count, FeedConstTy::No)),

0 commit comments

Comments
 (0)