Skip to content

Commit a315d23

Browse files
committed
const_block_items: do not create an AnonConst
1 parent e77b119 commit a315d23

13 files changed

Lines changed: 125 additions & 40 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,8 +3898,9 @@ impl ConstItemRhs {
38983898

38993899
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
39003900
pub struct ConstBlockItem {
3901-
// FIXME(const_block_items): current invariant is body.kind == InlineConst
3902-
pub body: Box<Expr>,
3901+
pub id: NodeId,
3902+
pub span: Span,
3903+
pub block: Box<Block>,
39033904
}
39043905

39053906
impl ConstBlockItem {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
308308
self.lower_define_opaque(hir_id, &define_opaque);
309309
hir::ItemKind::Const(ident, generics, ty, rhs)
310310
}
311-
ItemKind::ConstBlock(ConstBlockItem { body }) => hir::ItemKind::Const(
311+
ItemKind::ConstBlock(ConstBlockItem { span, id, block }) => hir::ItemKind::Const(
312312
self.lower_ident(ConstBlockItem::IDENT),
313313
hir::Generics::empty(),
314314
self.arena.alloc(self.ty_tup(DUMMY_SP, &[])),
315315
hir::ConstItemRhs::Body({
316-
let body = self.lower_expr_mut(body);
316+
let body = hir::Expr {
317+
hir_id: self.lower_node_id(*id),
318+
kind: hir::ExprKind::Block(self.lower_block(block, false), None),
319+
span: self.lower_span(*span),
320+
};
317321
self.record_body(&[], body)
318322
}),
319323
),

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ impl<'a> State<'a> {
205205
define_opaque.as_deref(),
206206
);
207207
}
208-
ast::ItemKind::ConstBlock(ast::ConstBlockItem { body }) => {
209-
self.print_expr(body, FixupContext::default())
208+
ast::ItemKind::ConstBlock(ast::ConstBlockItem { id: _, span: _, block }) => {
209+
let ib = self.ibox(INDENT_UNIT);
210+
self.word("const");
211+
self.nbsp();
212+
{
213+
let cb = self.cbox(0);
214+
let ib = self.ibox(0);
215+
self.print_block_with_attrs(block, &[], cb, ib);
216+
}
217+
self.end(ib);
210218
}
211219
ast::ItemKind::Const(box ast::ConstItem {
212220
defaultness,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,13 @@ impl<'a> Parser<'a> {
275275
self.parse_item_impl(attrs, def_(), false)?
276276
} else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
277277
allow_const_block_items
278-
&& self.token.is_keyword(kw::Const)
279-
&& self.look_ahead(1, |t| *t == token::OpenBrace || t.is_metavar_block())
278+
&& self.check_inline_const(0)
280279
{
281280
// CONST BLOCK ITEM
282-
self.psess.gated_spans.gate(sym::const_block_items, self.token.span);
283281
if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
284282
debug!("Parsing a const block item that does not matter: {:?}", self.token.span);
285283
};
286-
ItemKind::ConstBlock(ConstBlockItem { body: self.parse_expr()? })
284+
ItemKind::ConstBlock(self.parse_const_block_item()?)
287285
} else if let Const::Yes(const_span) = self.parse_constness(case) {
288286
// CONST ITEM
289287
self.recover_const_mut(const_span);
@@ -1473,6 +1471,14 @@ impl<'a> Parser<'a> {
14731471
}
14741472
}
14751473

1474+
fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1475+
self.expect_keyword(exp!(Const))?;
1476+
let const_span = self.prev_token.span;
1477+
self.psess.gated_spans.gate(sym::const_block_items, const_span);
1478+
let block = self.parse_block()?;
1479+
Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1480+
}
1481+
14761482
/// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
14771483
/// `mutability`.
14781484
///

compiler/rustc_resolve/src/late.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,23 +2898,27 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28982898
);
28992899
self.resolve_define_opaques(define_opaque);
29002900
}
2901-
ItemKind::ConstBlock(ConstBlockItem { body }) => self.with_generic_param_rib(
2902-
&[],
2903-
RibKind::Item(HasGenericParams::No, def_kind),
2904-
item.id,
2905-
LifetimeBinderKind::ConstItem,
2906-
DUMMY_SP,
2907-
|this| {
2908-
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
2909-
this.with_constant_rib(
2910-
IsRepeatExpr::No,
2911-
ConstantHasGenerics::Yes,
2912-
Some((ConstBlockItem::IDENT, ConstantItemKind::Const)),
2913-
|this| this.visit_expr(body),
2901+
ItemKind::ConstBlock(ConstBlockItem { id: _, span: _, block }) => self
2902+
.with_generic_param_rib(
2903+
&[],
2904+
RibKind::Item(HasGenericParams::No, def_kind),
2905+
item.id,
2906+
LifetimeBinderKind::ConstItem,
2907+
DUMMY_SP,
2908+
|this| {
2909+
this.with_lifetime_rib(
2910+
LifetimeRibKind::Elided(LifetimeRes::Infer),
2911+
|this| {
2912+
this.with_constant_rib(
2913+
IsRepeatExpr::No,
2914+
ConstantHasGenerics::Yes,
2915+
Some((ConstBlockItem::IDENT, ConstantItemKind::Const)),
2916+
|this| this.resolve_labeled_block(None, block.id, block),
2917+
)
2918+
},
29142919
);
2915-
})
2916-
},
2917-
),
2920+
},
2921+
),
29182922

29192923
ItemKind::Use(use_tree) => {
29202924
let maybe_exported = match use_tree.kind {

tests/ui/consts/const-block-items/assert-fail.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@ error[E0080]: evaluation panicked: assertion failed: false
22
--> $DIR/assert-fail.rs:5:9
33
|
44
LL | const { assert!(false) }
5-
| ^^^^^^^^^^^^^^ evaluation of `_::{constant#0}` failed here
6-
7-
note: erroneous constant encountered
8-
--> $DIR/assert-fail.rs:5:1
9-
|
10-
LL | const { assert!(false) }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^ evaluation of `_` failed here
126

137
error[E0080]: evaluation panicked: assertion failed: 2 + 2 == 5
148
--> $DIR/assert-fail.rs:7:9
159
|
1610
LL | const { assert!(2 + 2 == 5) }
17-
| ^^^^^^^^^^^^^^^^^^^ evaluation of `_::{constant#0}` failed here
18-
19-
note: erroneous constant encountered
20-
--> $DIR/assert-fail.rs:7:1
21-
|
22-
LL | const { assert!(2 + 2 == 5) }
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
2412

2513
error: aborting due to 2 previous errors
2614

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ build-pass
2+
//@ compile-flags: -Zunpretty=hir
3+
4+
#![feature(const_block_items)]
5+
6+
const {
7+
// foo
8+
}
9+
10+
fn main() { }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ build-pass
2+
//@ compile-flags: -Zunpretty=hir
3+
4+
#![feature(const_block_items)]
5+
extern crate std;
6+
#[prelude_import]
7+
use ::std::prelude::rust_2015::*;
8+
9+
const _: () =
10+
{
11+
// foo
12+
};
13+
14+
fn main() { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(const_block_items)]
2+
3+
const {
4+
#![expect(unused)] //~ ERROR: an inner attribute is not permitted in this context
5+
let a = 1;
6+
assert!(true);
7+
}
8+
9+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: an inner attribute is not permitted in this context
2+
--> $DIR/inner-attr.rs:4:5
3+
|
4+
LL | #![expect(unused)]
5+
| ^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | fn main() {}
8+
| ------------ the inner attribute doesn't annotate this function
9+
|
10+
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)