diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2025-03-24 00:01:30 +0100 |
|---|---|---|
| committer | León Orell Valerian Liehr <me@fmease.dev> | 2025-03-25 15:15:21 +0100 |
| commit | 82796dd8585c0c160fe2a50b12f77455a7e0f8d7 (patch) | |
| tree | 201d11e67d43b164d736942aedd27a90e0bc94cd | |
| parent | 848b0da34fa20d0ff5d12d1d4f506affc765534b (diff) | |
| download | rust-82796dd8585c0c160fe2a50b12f77455a7e0f8d7.tar.gz rust-82796dd8585c0c160fe2a50b12f77455a7e0f8d7.zip | |
Brace-ident-colon can certainly no longer start a block
thanks to the removal of type ascription.
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 14 | ||||
| -rw-r--r-- | tests/ui/parser/issues/issue-111692.rs | 10 | ||||
| -rw-r--r-- | tests/ui/parser/issues/issue-111692.stderr | 30 | ||||
| -rw-r--r-- | tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs | 2 | ||||
| -rw-r--r-- | tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr | 4 | ||||
| -rw-r--r-- | tests/ui/parser/type-ascription-in-pattern.rs | 9 | ||||
| -rw-r--r-- | tests/ui/parser/type-ascription-in-pattern.stderr | 80 |
7 files changed, 88 insertions, 61 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index c48f91643e8..79d12ed4bbe 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -3474,19 +3474,9 @@ impl<'a> Parser<'a> { } fn is_certainly_not_a_block(&self) -> bool { + // `{ ident, ` and `{ ident: ` cannot start a block. self.look_ahead(1, |t| t.is_ident()) - && ( - // `{ ident, ` cannot start a block. - self.look_ahead(2, |t| t == &token::Comma) - || self.look_ahead(2, |t| t == &token::Colon) - && ( - // `{ ident: token, ` cannot start a block. - self.look_ahead(4, |t| t == &token::Comma) - // `{ ident: ` cannot start a block unless it's a type ascription - // `ident: Type`. - || self.look_ahead(3, |t| !t.can_begin_type()) - ) - ) + && self.look_ahead(2, |t| t == &token::Comma || t == &token::Colon) } fn maybe_parse_struct_expr( diff --git a/tests/ui/parser/issues/issue-111692.rs b/tests/ui/parser/issues/issue-111692.rs index 56096f706a8..de6de222754 100644 --- a/tests/ui/parser/issues/issue-111692.rs +++ b/tests/ui/parser/issues/issue-111692.rs @@ -9,23 +9,25 @@ mod module { } fn test(x: module::Type) { - if x == module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal + if x == module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here } } fn test2(x: module::Type) { - if x ==module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal + if x ==module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here } } fn test3(x: module::Type) { - if x == Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal + use module::Type; + if x == Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here } } fn test4(x: module::Type) { - if x == demo_module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal + use module as demo_module; + if x == demo_module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here } } diff --git a/tests/ui/parser/issues/issue-111692.stderr b/tests/ui/parser/issues/issue-111692.stderr index 068b0483b0f..979dfade1ba 100644 --- a/tests/ui/parser/issues/issue-111692.stderr +++ b/tests/ui/parser/issues/issue-111692.stderr @@ -1,43 +1,43 @@ -error: invalid struct literal - --> $DIR/issue-111692.rs:12:21 +error: struct literals are not allowed here + --> $DIR/issue-111692.rs:12:13 | LL | if x == module::Type { x: module::C, y: 1 } { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: you might need to surround the struct literal with parentheses +help: surround the struct literal with parentheses | LL | if x == (module::Type { x: module::C, y: 1 }) { | + + -error: invalid struct literal - --> $DIR/issue-111692.rs:17:20 +error: struct literals are not allowed here + --> $DIR/issue-111692.rs:17:12 | LL | if x ==module::Type { x: module::C, y: 1 } { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: you might need to surround the struct literal with parentheses +help: surround the struct literal with parentheses | LL | if x ==(module::Type { x: module::C, y: 1 }) { | + + -error: invalid struct literal - --> $DIR/issue-111692.rs:23:13 +error: struct literals are not allowed here + --> $DIR/issue-111692.rs:24:13 | LL | if x == Type { x: module::C, y: 1 } { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: you might need to surround the struct literal with parentheses +help: surround the struct literal with parentheses | LL | if x == (Type { x: module::C, y: 1 }) { | + + -error: invalid struct literal - --> $DIR/issue-111692.rs:28:26 +error: struct literals are not allowed here + --> $DIR/issue-111692.rs:30:13 | LL | if x == demo_module::Type { x: module::C, y: 1 } { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: you might need to surround the struct literal with parentheses +help: surround the struct literal with parentheses | LL | if x == (demo_module::Type { x: module::C, y: 1 }) { | + + diff --git a/tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs b/tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs index 8be7c9ee8ac..3211b6c7bb9 100644 --- a/tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs +++ b/tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs @@ -7,7 +7,7 @@ impl Example { fn one() -> i32 { 1 } fn main() { - if Example { a: one(), }.is_pos() { //~ ERROR invalid struct literal + if Example { a: one(), }.is_pos() { //~ ERROR struct literals are not allowed here println!("Positive!"); } } diff --git a/tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr b/tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr index f7822ba1124..8eba2230f8f 100644 --- a/tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr +++ b/tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr @@ -1,10 +1,10 @@ -error: invalid struct literal +error: struct literals are not allowed here --> $DIR/method-call-on-struct-literal-in-if-condition.rs:10:8 | LL | if Example { a: one(), }.is_pos() { | ^^^^^^^^^^^^^^^^^^^^^ | -help: you might need to surround the struct literal with parentheses +help: surround the struct literal with parentheses | LL | if (Example { a: one(), }).is_pos() { | + + diff --git a/tests/ui/parser/type-ascription-in-pattern.rs b/tests/ui/parser/type-ascription-in-pattern.rs index fec168afba1..18d7061d69c 100644 --- a/tests/ui/parser/type-ascription-in-pattern.rs +++ b/tests/ui/parser/type-ascription-in-pattern.rs @@ -1,11 +1,10 @@ fn foo(x: bool) -> i32 { - match x { + match x { //~ ERROR struct literals are not allowed here x: i32 => x, //~ ERROR expected - //~^ ERROR mismatched types - true => 42., - false => 0.333, + true => 42., //~ ERROR expected identifier + false => 0.333, //~ ERROR expected identifier } -} +} //~ ERROR expected one of fn main() { match foo(true) { diff --git a/tests/ui/parser/type-ascription-in-pattern.stderr b/tests/ui/parser/type-ascription-in-pattern.stderr index 09190754993..135879f208b 100644 --- a/tests/ui/parser/type-ascription-in-pattern.stderr +++ b/tests/ui/parser/type-ascription-in-pattern.stderr @@ -1,18 +1,64 @@ -error: expected one of `@` or `|`, found `:` - --> $DIR/type-ascription-in-pattern.rs:3:10 +error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, `}`, or an operator, found `=>` + --> $DIR/type-ascription-in-pattern.rs:3:16 | +LL | match x { + | - while parsing this struct LL | x: i32 => x, - | ^ --- specifying the type of a pattern isn't supported - | | - | expected one of `@` or `|` + | -^^ expected one of 8 possible tokens + | | + | help: try adding a comma: `,` + +error: expected identifier, found keyword `true` + --> $DIR/type-ascription-in-pattern.rs:4:9 | -help: maybe write a path separator here +LL | match x { + | - while parsing this struct +LL | x: i32 => x, +LL | true => 42., + | ^^^^ expected identifier, found keyword + +error: expected identifier, found keyword `false` + --> $DIR/type-ascription-in-pattern.rs:5:9 | -LL | x::i32 => x, - | ~~ +LL | match x { + | - while parsing this struct +... +LL | false => 0.333, + | ^^^^^ expected identifier, found keyword + +error: struct literals are not allowed here + --> $DIR/type-ascription-in-pattern.rs:2:11 + | +LL | match x { + | ___________^ +LL | | x: i32 => x, +LL | | true => 42., +LL | | false => 0.333, +LL | | } + | |_____^ + | +help: surround the struct literal with parentheses + | +LL ~ match (x { +LL | x: i32 => x, +LL | true => 42., +LL | false => 0.333, +LL ~ }) + | + +error: expected one of `.`, `?`, `{`, or an operator, found `}` + --> $DIR/type-ascription-in-pattern.rs:7:1 + | +LL | match x { + | ----- while parsing this `match` expression +... +LL | } + | - expected one of `.`, `?`, `{`, or an operator +LL | } + | ^ unexpected token error: expected one of `...`, `..=`, `..`, or `|`, found `:` - --> $DIR/type-ascription-in-pattern.rs:12:11 + --> $DIR/type-ascription-in-pattern.rs:11:11 | LL | 42: i32 => (), | ^ --- specifying the type of a pattern isn't supported @@ -20,7 +66,7 @@ LL | 42: i32 => (), | expected one of `...`, `..=`, `..`, or `|` error: expected `|`, found `:` - --> $DIR/type-ascription-in-pattern.rs:13:10 + --> $DIR/type-ascription-in-pattern.rs:12:10 | LL | _: f64 => (), | ^ --- specifying the type of a pattern isn't supported @@ -28,7 +74,7 @@ LL | _: f64 => (), | expected `|` error: expected one of `@` or `|`, found `:` - --> $DIR/type-ascription-in-pattern.rs:14:10 + --> $DIR/type-ascription-in-pattern.rs:13:10 | LL | x: i32 => (), | ^ --- specifying the type of a pattern isn't supported @@ -40,15 +86,5 @@ help: maybe write a path separator here LL | x::i32 => (), | ~~ -error[E0308]: mismatched types - --> $DIR/type-ascription-in-pattern.rs:3:19 - | -LL | fn foo(x: bool) -> i32 { - | --- expected `i32` because of return type -LL | match x { -LL | x: i32 => x, - | ^ expected `i32`, found `bool` - -error: aborting due to 5 previous errors +error: aborting due to 8 previous errors -For more information about this error, try `rustc --explain E0308`. |
