Skip to content

Commit d6dc964

Browse files
authored
Unrolled build for #152234
Rollup merge of #152234 - BoxyUwU:dont_strip_const_blocks, r=oli-obk Dont strip const blocks in array lengths r? oli-obk mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong. We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals. Once we properly support literals in `hir::ConstArgKind` (see #152139 #152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :) Fixes rust-lang/rustfmt#6788 The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening. cc @rust-lang/rustfmt How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
2 parents efc9e1b + a86cfbb commit d6dc964

File tree

8 files changed

+61
-88
lines changed

8 files changed

+61
-88
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
827827
hir_id,
828828
def_id: self.local_def_id(v.id),
829829
data: self.lower_variant_data(hir_id, item_kind, &v.data),
830-
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const_to_anon_const(e)),
830+
disr_expr: v
831+
.disr_expr
832+
.as_ref()
833+
.map(|e| self.lower_anon_const_to_anon_const(e, e.value.span)),
831834
ident: self.lower_ident(v.ident),
832835
span: self.lower_span(v.span),
833836
}
@@ -917,7 +920,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
917920
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
918921
},
919922
vis_span: self.lower_span(f.vis.span),
920-
default: f.default.as_ref().map(|v| self.lower_anon_const_to_anon_const(v)),
923+
default: f
924+
.default
925+
.as_ref()
926+
.map(|v| self.lower_anon_const_to_anon_const(v, v.value.span)),
921927
ty,
922928
safety: self.lower_safety(f.safety, hir::Safety::Safe),
923929
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,15 +2425,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24252425
);
24262426

24272427
let lowered_args = self.arena.alloc_from_iter(args.iter().map(|arg| {
2428-
let const_arg = if let ExprKind::ConstBlock(anon_const) = &arg.kind {
2429-
let def_id = self.local_def_id(anon_const.id);
2430-
let def_kind = self.tcx.def_kind(def_id);
2431-
assert_eq!(DefKind::AnonConst, def_kind);
2432-
self.lower_anon_const_to_const_arg(anon_const)
2433-
} else {
2434-
self.lower_expr_to_const_arg_direct(arg)
2435-
};
2436-
2428+
let const_arg = self.lower_expr_to_const_arg_direct(arg);
24372429
&*self.arena.alloc(const_arg)
24382430
}));
24392431

@@ -2445,16 +2437,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24452437
}
24462438
ExprKind::Tup(exprs) => {
24472439
let exprs = self.arena.alloc_from_iter(exprs.iter().map(|expr| {
2448-
let expr = if let ExprKind::ConstBlock(anon_const) = &expr.kind {
2449-
let def_id = self.local_def_id(anon_const.id);
2450-
let def_kind = self.tcx.def_kind(def_id);
2451-
assert_eq!(DefKind::AnonConst, def_kind);
2452-
2453-
self.lower_anon_const_to_const_arg(anon_const)
2454-
} else {
2455-
self.lower_expr_to_const_arg_direct(&expr)
2456-
};
2457-
2440+
let expr = self.lower_expr_to_const_arg_direct(&expr);
24582441
&*self.arena.alloc(expr)
24592442
}));
24602443

@@ -2494,16 +2477,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24942477
// then go unused as the `Target::ExprField` is not actually
24952478
// corresponding to `Node::ExprField`.
24962479
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
2497-
2498-
let expr = if let ExprKind::ConstBlock(anon_const) = &f.expr.kind {
2499-
let def_id = self.local_def_id(anon_const.id);
2500-
let def_kind = self.tcx.def_kind(def_id);
2501-
assert_eq!(DefKind::AnonConst, def_kind);
2502-
2503-
self.lower_anon_const_to_const_arg(anon_const)
2504-
} else {
2505-
self.lower_expr_to_const_arg_direct(&f.expr)
2506-
};
2480+
let expr = self.lower_expr_to_const_arg_direct(&f.expr);
25072481

25082482
&*self.arena.alloc(hir::ConstArgExprField {
25092483
hir_id,
@@ -2521,13 +2495,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25212495
}
25222496
ExprKind::Array(elements) => {
25232497
let lowered_elems = self.arena.alloc_from_iter(elements.iter().map(|element| {
2524-
let const_arg = if let ExprKind::ConstBlock(anon_const) = &element.kind {
2525-
let def_id = self.local_def_id(anon_const.id);
2526-
assert_eq!(DefKind::AnonConst, self.tcx.def_kind(def_id));
2527-
self.lower_anon_const_to_const_arg(anon_const)
2528-
} else {
2529-
self.lower_expr_to_const_arg_direct(element)
2530-
};
2498+
let const_arg = self.lower_expr_to_const_arg_direct(element);
25312499
&*self.arena.alloc(const_arg)
25322500
}));
25332501
let array_expr = self.arena.alloc(hir::ConstArgArrayExpr {
@@ -2557,6 +2525,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25572525
| ExprKind::Call(..)
25582526
| ExprKind::Tup(..)
25592527
| ExprKind::Array(..)
2528+
| ExprKind::ConstBlock(..)
25602529
)
25612530
{
25622531
return self.lower_expr_to_const_arg_direct(expr);
@@ -2586,6 +2555,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25862555
span,
25872556
}
25882557
}
2558+
ExprKind::ConstBlock(anon_const) => {
2559+
let def_id = self.local_def_id(anon_const.id);
2560+
assert_eq!(DefKind::AnonConst, self.tcx.def_kind(def_id));
2561+
self.lower_anon_const_to_const_arg(anon_const, span)
2562+
}
25892563
_ => overly_complex_const(self),
25902564
}
25912565
}
@@ -2596,11 +2570,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25962570
&mut self,
25972571
anon: &AnonConst,
25982572
) -> &'hir hir::ConstArg<'hir> {
2599-
self.arena.alloc(self.lower_anon_const_to_const_arg(anon))
2573+
self.arena.alloc(self.lower_anon_const_to_const_arg(anon, anon.value.span))
26002574
}
26012575

26022576
#[instrument(level = "debug", skip(self))]
2603-
fn lower_anon_const_to_const_arg(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
2577+
fn lower_anon_const_to_const_arg(
2578+
&mut self,
2579+
anon: &AnonConst,
2580+
span: Span,
2581+
) -> hir::ConstArg<'hir> {
26042582
let tcx = self.tcx;
26052583

26062584
// We cannot change parsing depending on feature gates available,
@@ -2611,7 +2589,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26112589
if tcx.features().min_generic_const_args() {
26122590
return match anon.mgca_disambiguation {
26132591
MgcaDisambiguation::AnonConst => {
2614-
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2592+
let lowered_anon = self.lower_anon_const_to_anon_const(anon, span);
26152593
ConstArg {
26162594
hir_id: self.next_id(),
26172595
kind: hir::ConstArgKind::Anon(lowered_anon),
@@ -2657,7 +2635,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26572635
};
26582636
}
26592637

2660-
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2638+
let lowered_anon = self.lower_anon_const_to_anon_const(anon, anon.value.span);
26612639
ConstArg {
26622640
hir_id: self.next_id(),
26632641
kind: hir::ConstArgKind::Anon(lowered_anon),
@@ -2667,15 +2645,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26672645

26682646
/// See [`hir::ConstArg`] for when to use this function vs
26692647
/// [`Self::lower_anon_const_to_const_arg`].
2670-
fn lower_anon_const_to_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2648+
fn lower_anon_const_to_anon_const(
2649+
&mut self,
2650+
c: &AnonConst,
2651+
span: Span,
2652+
) -> &'hir hir::AnonConst {
26712653
self.arena.alloc(self.with_new_scopes(c.value.span, |this| {
26722654
let def_id = this.local_def_id(c.id);
26732655
let hir_id = this.lower_node_id(c.id);
26742656
hir::AnonConst {
26752657
def_id,
26762658
hir_id,
26772659
body: this.lower_const_body(c.value.span, Some(&c.value)),
2678-
span: this.lower_span(c.value.span),
2660+
span: this.lower_span(span),
26792661
}
26802662
}))
26812663
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,16 +1627,8 @@ impl<'a> Parser<'a> {
16271627
let first_expr = self.parse_expr()?;
16281628
if self.eat(exp!(Semi)) {
16291629
// Repeating array syntax: `[ 0; 512 ]`
1630-
let count = if self.eat_keyword(exp!(Const)) {
1631-
// While we could just disambiguate `Direct` from `AnonConst` by
1632-
// treating all const block exprs as `AnonConst`, that would
1633-
// complicate the DefCollector and likely all other visitors.
1634-
// So we strip the const blockiness and just store it as a block
1635-
// in the AST with the extra disambiguator on the AnonConst
1636-
self.parse_mgca_const_block(false)?
1637-
} else {
1638-
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?
1639-
};
1630+
let count =
1631+
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?;
16401632
self.expect(close)?;
16411633
ExprKind::Repeat(first_expr, count)
16421634
} else if self.eat(exp!(Comma)) {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ use rustc_ast::tokenstream::{
3333
};
3434
use rustc_ast::util::case::Case;
3535
use rustc_ast::{
36-
self as ast, AnonConst, AttrArgs, AttrId, BlockCheckMode, ByRef, Const, CoroutineKind,
37-
DUMMY_NODE_ID, DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, MgcaDisambiguation,
38-
Mutability, Recovered, Safety, StrLit, Visibility, VisibilityKind,
36+
self as ast, AnonConst, AttrArgs, AttrId, ByRef, Const, CoroutineKind, DUMMY_NODE_ID,
37+
DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, MgcaDisambiguation, Mutability,
38+
Recovered, Safety, StrLit, Visibility, VisibilityKind,
3939
};
4040
use rustc_ast_pretty::pprust;
4141
use rustc_data_structures::debug_assert_matches;
@@ -1269,19 +1269,6 @@ impl<'a> Parser<'a> {
12691269
}
12701270
}
12711271

1272-
fn parse_mgca_const_block(&mut self, gate_syntax: bool) -> PResult<'a, AnonConst> {
1273-
let kw_span = self.prev_token.span;
1274-
let value = self.parse_expr_block(None, kw_span, BlockCheckMode::Default)?;
1275-
if gate_syntax {
1276-
self.psess.gated_spans.gate(sym::min_generic_const_args, kw_span.to(value.span));
1277-
}
1278-
Ok(AnonConst {
1279-
id: ast::DUMMY_NODE_ID,
1280-
value,
1281-
mgca_disambiguation: MgcaDisambiguation::AnonConst,
1282-
})
1283-
}
1284-
12851272
/// Parses inline const expressions.
12861273
fn parse_const_block(&mut self, span: Span) -> PResult<'a, Box<Expr>> {
12871274
self.expect_keyword(exp!(Const))?;

compiler/rustc_parse/src/parser/path.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ impl<'a> Parser<'a> {
847847
/// - A literal.
848848
/// - A numeric literal prefixed by `-`.
849849
/// - A single-segment path.
850+
/// - A const block (under mGCA)
850851
pub(super) fn expr_is_valid_const_arg(&self, expr: &Box<rustc_ast::Expr>) -> bool {
851852
match &expr.kind {
852853
ast::ExprKind::Block(_, _)
@@ -863,6 +864,10 @@ impl<'a> Parser<'a> {
863864
{
864865
true
865866
}
867+
ast::ExprKind::ConstBlock(_) => {
868+
self.psess.gated_spans.gate(sym::min_generic_const_args, expr.span);
869+
true
870+
}
866871
_ => false,
867872
}
868873
}
@@ -874,14 +879,6 @@ impl<'a> Parser<'a> {
874879
let (value, mgca_disambiguation) = if self.token.kind == token::OpenBrace {
875880
let value = self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)?;
876881
(value, MgcaDisambiguation::Direct)
877-
} else if self.eat_keyword(exp!(Const)) {
878-
// While we could just disambiguate `Direct` from `AnonConst` by
879-
// treating all const block exprs as `AnonConst`, that would
880-
// complicate the DefCollector and likely all other visitors.
881-
// So we strip the const blockiness and just store it as a block
882-
// in the AST with the extra disambiguator on the AnonConst
883-
let value = self.parse_mgca_const_block(true)?;
884-
(value.value, MgcaDisambiguation::AnonConst)
885882
} else {
886883
self.parse_unambiguous_unbraced_const_arg()?
887884
};

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -658,16 +658,8 @@ impl<'a> Parser<'a> {
658658
};
659659

660660
let ty = if self.eat(exp!(Semi)) {
661-
let mut length = if self.eat_keyword(exp!(Const)) {
662-
// While we could just disambiguate `Direct` from `AnonConst` by
663-
// treating all const block exprs as `AnonConst`, that would
664-
// complicate the DefCollector and likely all other visitors.
665-
// So we strip the const blockiness and just store it as a block
666-
// in the AST with the extra disambiguator on the AnonConst
667-
self.parse_mgca_const_block(false)?
668-
} else {
669-
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?
670-
};
661+
let mut length =
662+
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?;
671663

672664
if let Err(e) = self.expect(exp!(CloseBracket)) {
673665
// Try to recover from `X<Y, ...>` when `X::<Y, ...>` works
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn foo() {
2+
let a = [(); const { let x = 1; x }];
3+
}
4+
5+
fn foo() {
6+
let x = [(); const { 1 }];
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn foo() {
2+
let a = [(); const {
3+
let x = 1;
4+
x
5+
}];
6+
}
7+
8+
fn foo() {
9+
let x = [(); const { 1 }];
10+
}

0 commit comments

Comments
 (0)