Skip to content

Commit 6c3cbcd

Browse files
authored
Rollup merge of rust-lang#113803 - compiler-errors:const-interp-block, r=fee1-dead
Fix inline_const with interpolated block Interpolation already worked when we had a `const $block` that wasn't a statement expr: ``` fn foo() { let _ = const $block; } ``` But it was failing when the const block was in statement expr position: ``` fn foo() { const $block; } ``` ... because of a bug in a check for const items. This fixes that. --- cc rust-lang#112953 (comment), though I don't think this requires an FCP since it's already supported in exprs and seems to me to be fully a parser bug.
2 parents c7c8914 + 7197979 commit 6c3cbcd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

compiler/rustc_parse/src/parser/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,8 @@ impl<'a> Parser<'a> {
12101210
fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
12111211
// Avoid const blocks and const closures to be parsed as const items
12121212
if (self.check_const_closure() == is_closure)
1213-
&& self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
1213+
&& !self
1214+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
12141215
&& self.eat_keyword_case(kw::Const, case)
12151216
{
12161217
Const::Yes(self.prev_token.uninterpolated_span())

tests/ui/inline-const/interpolated.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// check-pass
2+
3+
#![feature(inline_const)]
4+
5+
// This used to be unsupported since the parser first tries to check if we have
6+
// any nested items, and then checks for statements (and expressions). The heuristic
7+
// that we were using to detect the beginning of a const item was incorrect, so
8+
// this used to fail.
9+
macro_rules! m {
10+
($b:block) => {
11+
fn foo() {
12+
const $b
13+
}
14+
}
15+
}
16+
17+
// This has worked since inline-consts were implemented, since the position that
18+
// the const block is located at doesn't support nested items (e.g. because
19+
// `let x = const X: u32 = 1;` is invalid), so there's no ambiguity parsing the
20+
// inline const.
21+
macro_rules! m2 {
22+
($b:block) => {
23+
fn foo2() {
24+
let _ = const $b;
25+
}
26+
}
27+
}
28+
29+
m!({});
30+
m2!({});
31+
32+
fn main() {}

0 commit comments

Comments
 (0)