Skip to content

Commit 4841313

Browse files
committed
const_block_items: tests
1 parent 038fee1 commit 4841313

File tree

12 files changed

+124
-5
lines changed

12 files changed

+124
-5
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,7 @@ pub enum ItemKind {
38363836
/// E.g., `const FOO: i32 = 42;`.
38373837
Const(Box<ConstItem>),
38383838
/// A module-level const block.
3839-
/// Equivalent to `const _: () = const { ... }`.
3839+
/// Equivalent to `const _: () = const { ... };`.
38403840
///
38413841
/// E.g., `const { assert!(true) }`.
38423842
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ check-fail
2+
3+
#![feature(const_block_items)]
4+
5+
const {
6+
assert!(true);
7+
2 + 2 //~ ERROR: mismatched types [E0308]
8+
}
9+
10+
11+
const fn id<T>(t: T) -> T {
12+
t
13+
}
14+
15+
const { id(2) }
16+
//~^ ERROR: mismatched types [E0308]
17+
const { id(()) }
18+
19+
20+
fn main() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/typecheck.rs:7:5
3+
|
4+
LL | 2 + 2
5+
| ^^^^^ expected `()`, found integer
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/typecheck.rs:15:12
9+
|
10+
LL | const { id(2) }
11+
| -- ^ expected `()`, found integer
12+
| |
13+
| arguments to this function are incorrect
14+
|
15+
help: the return type of this call is `{integer}` due to the type of the argument passed
16+
--> $DIR/typecheck.rs:15:9
17+
|
18+
LL | const { id(2) }
19+
| ^^^-^
20+
| |
21+
| this argument influences the return type of `id`
22+
note: function defined here
23+
--> $DIR/typecheck.rs:11:10
24+
|
25+
LL | const fn id<T>(t: T) -> T {
26+
| ^^ ----
27+
28+
error: aborting due to 2 previous errors
29+
30+
For more information about this error, try `rustc --explain E0308`.

tests/ui/consts/const-block-item-macro-codegen.rs renamed to tests/ui/consts/const-item-with-block-body/macro-codegen.rs

File renamed without changes.
File renamed without changes.

tests/ui/consts/const-block-item.stderr renamed to tests/ui/consts/const-item-with-block-body/static.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: trait `Value` is never used
2-
--> $DIR/const-block-item.rs:5:15
2+
--> $DIR/static.rs:5:15
33
|
44
LL | pub trait Value {
55
| ^^^^^

0 commit comments

Comments
 (0)