Skip to content

Commit 291d272

Browse files
authored
Rollup merge of rust-lang#150074 - mgca/tuple-expr, r=Mark-Simulacrum
Update provider API docs Tracking issue: rust-lang#132980 Fixes rust-lang#133965 Fixes rust-lang#150613 r? @BoxyUwU
2 parents 77ba34a + 6c2dc40 commit 291d272

File tree

38 files changed

+386
-141
lines changed

38 files changed

+386
-141
lines changed

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: 53 additions & 10 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,8 +2435,26 @@ 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
}
2441+
ExprKind::Tup(exprs) => {
2442+
let exprs = self.arena.alloc_from_iter(exprs.iter().map(|expr| {
2443+
let expr = if let ExprKind::ConstBlock(anon_const) = &expr.kind {
2444+
let def_id = self.local_def_id(anon_const.id);
2445+
let def_kind = self.tcx.def_kind(def_id);
2446+
assert_eq!(DefKind::AnonConst, def_kind);
2447+
2448+
self.lower_anon_const_to_const_arg(anon_const)
2449+
} else {
2450+
self.lower_expr_to_const_arg_direct(&expr)
2451+
};
2452+
2453+
&*self.arena.alloc(expr)
2454+
}));
2455+
2456+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(exprs), span }
2457+
}
24302458
ExprKind::Path(qself, path) => {
24312459
let qpath = self.lower_qpath(
24322460
expr.id,
@@ -2439,7 +2467,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24392467
None,
24402468
);
24412469

2442-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
2470+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath), span }
24432471
}
24442472
ExprKind::Struct(se) => {
24452473
let path = self.lower_qpath(
@@ -2480,11 +2508,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24802508
})
24812509
}));
24822510

2483-
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+
}
24842516
}
24852517
ExprKind::Underscore => ConstArg {
24862518
hir_id: self.lower_node_id(expr.id),
2487-
kind: hir::ConstArgKind::Infer(expr.span, ()),
2519+
kind: hir::ConstArgKind::Infer(()),
2520+
span,
24882521
},
24892522
ExprKind::Block(block, _) => {
24902523
if let [stmt] = block.stmts.as_slice()
@@ -2495,6 +2528,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24952528
| ExprKind::Path(..)
24962529
| ExprKind::Struct(..)
24972530
| ExprKind::Call(..)
2531+
| ExprKind::Tup(..)
24982532
)
24992533
{
25002534
return self.lower_expr_to_const_arg_direct(expr);
@@ -2528,7 +2562,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25282562
return match anon.mgca_disambiguation {
25292563
MgcaDisambiguation::AnonConst => {
25302564
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2531-
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+
}
25322570
}
25332571
MgcaDisambiguation::Direct => self.lower_expr_to_const_arg_direct(&anon.value),
25342572
};
@@ -2565,11 +2603,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25652603
return ConstArg {
25662604
hir_id: self.lower_node_id(anon.id),
25672605
kind: hir::ConstArgKind::Path(qpath),
2606+
span: self.lower_span(expr.span),
25682607
};
25692608
}
25702609

25712610
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2572-
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+
}
25732616
}
25742617

25752618
/// 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 & 16 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,23 +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::Struct(path, _) => path.span(),
501-
ConstArgKind::Path(path) => path.span(),
502-
ConstArgKind::TupleCall(path, _) => path.span(),
503-
ConstArgKind::Anon(anon) => anon.span,
504-
ConstArgKind::Error(span, _) => span,
505-
ConstArgKind::Infer(span, _) => span,
506-
}
507-
}
508498
}
509499

510500
/// See [`ConstArg`].
511501
#[derive(Clone, Copy, Debug, HashStable_Generic)]
512502
#[repr(u8, C)]
513503
pub enum ConstArgKind<'hir, Unambig = ()> {
504+
Tup(&'hir [&'hir ConstArg<'hir, Unambig>]),
514505
/// **Note:** Currently this is only used for bare const params
515506
/// (`N` where `fn foo<const N: usize>(...)`),
516507
/// not paths to any const (`N` where `const N: usize = ...`).
@@ -523,10 +514,10 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
523514
/// Tuple constructor variant
524515
TupleCall(QPath<'hir>, &'hir [&'hir ConstArg<'hir>]),
525516
/// Error const
526-
Error(Span, ErrorGuaranteed),
517+
Error(ErrorGuaranteed),
527518
/// This variant is not always used to represent inference consts, sometimes
528519
/// [`GenericArg::Infer`] is used instead.
529-
Infer(Span, Unambig),
520+
Infer(Unambig),
530521
}
531522

532523
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -572,7 +563,7 @@ impl GenericArg<'_> {
572563
match self {
573564
GenericArg::Lifetime(l) => l.ident.span,
574565
GenericArg::Type(t) => t.span,
575-
GenericArg::Const(c) => c.span(),
566+
GenericArg::Const(c) => c.span,
576567
GenericArg::Infer(i) => i.span,
577568
}
578569
}

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: 8 additions & 4 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,9 +1078,13 @@ 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) => {
1085+
walk_list!(visitor, visit_const_arg, *exprs);
1086+
V::Result::output()
1087+
}
10841088
ConstArgKind::Struct(qpath, field_exprs) => {
10851089
try_visit!(visitor.visit_qpath(qpath, *hir_id, qpath.span()));
10861090

@@ -1099,7 +1103,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10991103
}
11001104
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
11011105
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
1102-
ConstArgKind::Error(_, _) => V::Result::output(), // errors and spans are not important
1106+
ConstArgKind::Error(_) => V::Result::output(), // errors and spans are not important
11031107
}
11041108
}
11051109

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,23 +1479,27 @@ fn rendered_precise_capturing_args<'tcx>(
14791479

14801480
fn const_param_default<'tcx>(
14811481
tcx: TyCtxt<'tcx>,
1482-
def_id: LocalDefId,
1482+
local_def_id: LocalDefId,
14831483
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
14841484
let hir::Node::GenericParam(hir::GenericParam {
14851485
kind: hir::GenericParamKind::Const { default: Some(default_ct), .. },
14861486
..
1487-
}) = tcx.hir_node_by_def_id(def_id)
1487+
}) = tcx.hir_node_by_def_id(local_def_id)
14881488
else {
14891489
span_bug!(
1490-
tcx.def_span(def_id),
1490+
tcx.def_span(local_def_id),
14911491
"`const_param_default` expected a generic parameter with a constant"
14921492
)
14931493
};
1494-
let icx = ItemCtxt::new(tcx, def_id);
1495-
let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
1494+
1495+
let icx = ItemCtxt::new(tcx, local_def_id);
1496+
1497+
let def_id = local_def_id.to_def_id();
1498+
let identity_args = ty::GenericArgs::identity_for_item(tcx, tcx.parent(def_id));
1499+
14961500
let ct = icx
14971501
.lowerer()
1498-
.lower_const_arg(default_ct, FeedConstTy::Param(def_id.to_def_id(), identity_args));
1502+
.lower_const_arg(default_ct, FeedConstTy::with_type_of(tcx, def_id, identity_args));
14991503
ty::EarlyBinder::bind(ct)
15001504
}
15011505

@@ -1553,7 +1557,7 @@ fn const_of_item<'tcx>(
15531557
let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
15541558
let ct = icx
15551559
.lowerer()
1556-
.lower_const_arg(ct_arg, FeedConstTy::Param(def_id.to_def_id(), identity_args));
1560+
.lower_const_arg(ct_arg, FeedConstTy::with_type_of(tcx, def_id.to_def_id(), identity_args));
15571561
if let Err(e) = icx.check_tainted_by_errors()
15581562
&& !ct.references_error()
15591563
{

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

0 commit comments

Comments
 (0)