Skip to content

Commit ccad7e5

Browse files
committed
const_block_items: tests
1 parent 1209869 commit ccad7e5

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3822,7 +3822,7 @@ pub enum ItemKind {
38223822
/// E.g., `const FOO: i32 = 42;`.
38233823
Const(Box<ConstItem>),
38243824
/// A module-level const block.
3825-
/// Equivalent to `const _: () = const { ... }`.
3825+
/// Equivalent to `const _: () = const { ... };`.
38263826
///
38273827
/// E.g., `const { assert!(true) }`.
38283828
ConstBlock(ConstBlockItem),

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::{
2222
};
2323

2424
use crate::lexer::StripTokens;
25-
use crate::parser::{ForceCollect, Parser};
25+
use crate::parser::{AllowConstBlockItems, ForceCollect, Parser};
2626
use crate::{new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal};
2727

2828
fn psess() -> ParseSess {
@@ -2239,7 +2239,7 @@ fn parse_item_from_source_str(
22392239
psess: &ParseSess,
22402240
) -> PResult<'_, Option<Box<ast::Item>>> {
22412241
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source, StripTokens::Nothing))
2242-
.parse_item(ForceCollect::No)
2242+
.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)
22432243
}
22442244

22452245
// Produces a `rustc_span::span`.
@@ -2254,7 +2254,9 @@ fn string_to_expr(source_str: String) -> Box<ast::Expr> {
22542254

22552255
/// Parses a string, returns an item.
22562256
fn string_to_item(source_str: String) -> Option<Box<ast::Item>> {
2257-
with_error_checking_parse(source_str, &psess(), |p| p.parse_item(ForceCollect::No))
2257+
with_error_checking_parse(source_str, &psess(), |p| {
2258+
p.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)
2259+
})
22582260
}
22592261

22602262
#[test]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-fail
2+
3+
#![feature(const_block_items)]
4+
5+
const { assert!(false) }
6+
//~^ ERROR: evaluation panicked: assertion failed: false [E0080]
7+
const { assert!(2 + 2 == 5) }
8+
//~^ ERROR: evaluation panicked: assertion failed: 2 + 2 == 5 [E0080]
9+
10+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0080]: evaluation panicked: assertion failed: false
2+
--> $DIR/assert-fail.rs:5:9
3+
|
4+
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+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0080]: evaluation panicked: assertion failed: 2 + 2 == 5
14+
--> $DIR/assert-fail.rs:7:9
15+
|
16+
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+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ check-pass
2+
#![feature(const_block_items)]
3+
4+
const { assert!(true) }
5+
const { assert!(2 + 2 == 4) }
6+
7+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
3+
#![feature(const_block_items)]
4+
5+
#[cfg(false)]
6+
const { assert!(false) }
7+
8+
#[expect(unused)]
9+
const {
10+
let a = 1;
11+
assert!(true);
12+
}
13+
14+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ check-pass
2+
3+
#![feature(const_block_items)]
4+
5+
fn main() {
6+
mod foo {
7+
const { assert!(true) }
8+
}
9+
}

0 commit comments

Comments
 (0)