diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 8d2d37228c16..c435e632815a 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3926,7 +3926,7 @@ pub enum ItemKind { /// E.g., `const FOO: i32 = 42;`. Const(Box), /// A module-level const block. - /// Equivalent to `const _: () = const { ... }`. + /// Equivalent to `const _: () = const { ... };`. /// /// E.g., `const { assert!(true) }`. ConstBlock(ConstBlockItem), diff --git a/compiler/rustc_parse/src/parser/tests.rs b/compiler/rustc_parse/src/parser/tests.rs index c57b9baa0c4d..4b69d4cf3e74 100644 --- a/compiler/rustc_parse/src/parser/tests.rs +++ b/compiler/rustc_parse/src/parser/tests.rs @@ -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>> { 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 { /// Parses a string, returns an item. fn string_to_item(source_str: String) -> Option> { - 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] diff --git a/tests/ui/consts/const-block-items/assert-fail.rs b/tests/ui/consts/const-block-items/assert-fail.rs new file mode 100644 index 000000000000..a7e3478666ef --- /dev/null +++ b/tests/ui/consts/const-block-items/assert-fail.rs @@ -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() {} diff --git a/tests/ui/consts/const-block-items/assert-fail.stderr b/tests/ui/consts/const-block-items/assert-fail.stderr new file mode 100644 index 000000000000..78bd89478541 --- /dev/null +++ b/tests/ui/consts/const-block-items/assert-fail.stderr @@ -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`. diff --git a/tests/ui/consts/const-block-items/assert-pass.rs b/tests/ui/consts/const-block-items/assert-pass.rs new file mode 100644 index 000000000000..c409cc5b91f7 --- /dev/null +++ b/tests/ui/consts/const-block-items/assert-pass.rs @@ -0,0 +1,7 @@ +//@ check-pass +#![feature(const_block_items)] + +const { assert!(true) } +const { assert!(2 + 2 == 4) } + +fn main() {} diff --git a/tests/ui/consts/const-block-items/typecheck.rs b/tests/ui/consts/const-block-items/typecheck.rs new file mode 100644 index 000000000000..2e521cbfda05 --- /dev/null +++ b/tests/ui/consts/const-block-items/typecheck.rs @@ -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 +} + +const { id(2) } +//~^ ERROR: mismatched types [E0308] +const { id(()) } + + +fn main() {} diff --git a/tests/ui/consts/const-block-items/typecheck.stderr b/tests/ui/consts/const-block-items/typecheck.stderr new file mode 100644 index 000000000000..93d1eb1bc9e4 --- /dev/null +++ b/tests/ui/consts/const-block-items/typecheck.stderr @@ -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 { + | ^^ ---- + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/consts/const-block-item-macro-codegen.rs b/tests/ui/consts/const-item-with-block-body/macro-codegen.rs similarity index 100% rename from tests/ui/consts/const-block-item-macro-codegen.rs rename to tests/ui/consts/const-item-with-block-body/macro-codegen.rs diff --git a/tests/ui/consts/const-block-item.rs b/tests/ui/consts/const-item-with-block-body/static.rs similarity index 100% rename from tests/ui/consts/const-block-item.rs rename to tests/ui/consts/const-item-with-block-body/static.rs diff --git a/tests/ui/consts/const-block-item.stderr b/tests/ui/consts/const-item-with-block-body/static.stderr similarity index 84% rename from tests/ui/consts/const-block-item.stderr rename to tests/ui/consts/const-item-with-block-body/static.stderr index b325976a60b6..feaabb92803f 100644 --- a/tests/ui/consts/const-block-item.stderr +++ b/tests/ui/consts/const-item-with-block-body/static.stderr @@ -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 { | ^^^^^ diff --git a/tests/ui/parser/const-block-items/attrs.rs b/tests/ui/parser/const-block-items/attrs.rs new file mode 100644 index 000000000000..eb7a0554ea9c --- /dev/null +++ b/tests/ui/parser/const-block-items/attrs.rs @@ -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() {} diff --git a/tests/ui/parser/const-block-items/mod-in-fn.rs b/tests/ui/parser/const-block-items/mod-in-fn.rs new file mode 100644 index 000000000000..ec98f95a8448 --- /dev/null +++ b/tests/ui/parser/const-block-items/mod-in-fn.rs @@ -0,0 +1,9 @@ +//@ check-pass + +#![feature(const_block_items)] + +fn main() { + mod foo { + const { assert!(true) } + } +}