const_block_items: tests

This commit is contained in:
Pavel Grigorenko 2025-11-26 23:48:09 +03:00
parent 5eb01938c9
commit e77b11924f
12 changed files with 124 additions and 5 deletions

View file

@ -3926,7 +3926,7 @@ pub enum ItemKind {
/// E.g., `const FOO: i32 = 42;`.
Const(Box<ConstItem>),
/// A module-level const block.
/// Equivalent to `const _: () = const { ... }`.
/// Equivalent to `const _: () = const { ... };`.
///
/// E.g., `const { assert!(true) }`.
ConstBlock(ConstBlockItem),

View file

@ -22,7 +22,7 @@ use rustc_span::{
};
use crate::lexer::StripTokens;
use crate::parser::{ForceCollect, Parser};
use crate::parser::{AllowConstBlockItems, ForceCollect, Parser};
use crate::{new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal};
fn psess() -> ParseSess {
@ -2241,7 +2241,7 @@ fn parse_item_from_source_str(
psess: &ParseSess,
) -> PResult<'_, Option<Box<ast::Item>>> {
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source, StripTokens::Nothing))
.parse_item(ForceCollect::No)
.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)
}
// Produces a `rustc_span::span`.
@ -2256,7 +2256,9 @@ fn string_to_expr(source_str: String) -> Box<ast::Expr> {
/// Parses a string, returns an item.
fn string_to_item(source_str: String) -> Option<Box<ast::Item>> {
with_error_checking_parse(source_str, &psess(), |p| p.parse_item(ForceCollect::No))
with_error_checking_parse(source_str, &psess(), |p| {
p.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)
})
}
#[test]

View file

@ -0,0 +1,10 @@
//@ check-fail
#![feature(const_block_items)]
const { assert!(false) }
//~^ ERROR: evaluation panicked: assertion failed: false [E0080]
const { assert!(2 + 2 == 5) }
//~^ ERROR: evaluation panicked: assertion failed: 2 + 2 == 5 [E0080]
fn main() {}

View file

@ -0,0 +1,27 @@
error[E0080]: evaluation panicked: assertion failed: false
--> $DIR/assert-fail.rs:5:9
|
LL | const { assert!(false) }
| ^^^^^^^^^^^^^^ evaluation of `_::{constant#0}` failed here
note: erroneous constant encountered
--> $DIR/assert-fail.rs:5:1
|
LL | const { assert!(false) }
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: evaluation panicked: assertion failed: 2 + 2 == 5
--> $DIR/assert-fail.rs:7:9
|
LL | const { assert!(2 + 2 == 5) }
| ^^^^^^^^^^^^^^^^^^^ evaluation of `_::{constant#0}` failed here
note: erroneous constant encountered
--> $DIR/assert-fail.rs:7:1
|
LL | const { assert!(2 + 2 == 5) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,7 @@
//@ check-pass
#![feature(const_block_items)]
const { assert!(true) }
const { assert!(2 + 2 == 4) }
fn main() {}

View file

@ -0,0 +1,20 @@
//@ check-fail
#![feature(const_block_items)]
const {
assert!(true);
2 + 2 //~ ERROR: mismatched types [E0308]
}
const fn id<T>(t: T) -> T {
t
}
const { id(2) }
//~^ ERROR: mismatched types [E0308]
const { id(()) }
fn main() {}

View file

@ -0,0 +1,30 @@
error[E0308]: mismatched types
--> $DIR/typecheck.rs:7:5
|
LL | 2 + 2
| ^^^^^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/typecheck.rs:15:12
|
LL | const { id(2) }
| -- ^ expected `()`, found integer
| |
| arguments to this function are incorrect
|
help: the return type of this call is `{integer}` due to the type of the argument passed
--> $DIR/typecheck.rs:15:9
|
LL | const { id(2) }
| ^^^-^
| |
| this argument influences the return type of `id`
note: function defined here
--> $DIR/typecheck.rs:11:10
|
LL | const fn id<T>(t: T) -> T {
| ^^ ----
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -1,5 +1,5 @@
warning: trait `Value` is never used
--> $DIR/const-block-item.rs:5:15
--> $DIR/static.rs:5:15
|
LL | pub trait Value {
| ^^^^^

View file

@ -0,0 +1,14 @@
//@ check-pass
#![feature(const_block_items)]
#[cfg(false)]
const { assert!(false) }
#[expect(unused)]
const {
let a = 1;
assert!(true);
}
fn main() {}

View file

@ -0,0 +1,9 @@
//@ check-pass
#![feature(const_block_items)]
fn main() {
mod foo {
const { assert!(true) }
}
}