Brace-ident-colon can certainly no longer start a block

thanks to the removal of type ascription.
This commit is contained in:
León Orell Valerian Liehr 2025-03-24 00:01:30 +01:00
parent 848b0da34f
commit 82796dd858
No known key found for this signature in database
GPG key ID: D17A07215F68E713
7 changed files with 88 additions and 61 deletions

View file

@ -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(

View file

@ -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
}
}

View file

@ -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 }) {
| + +

View file

@ -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!");
}
}

View file

@ -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() {
| + +

View file

@ -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) {

View file

@ -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 8 previous errors
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0308`.