Skip to content

Commit 82796dd

Browse files
committed
Brace-ident-colon can certainly no longer start a block
thanks to the removal of type ascription.
1 parent 848b0da commit 82796dd

7 files changed

+88
-61
lines changed

compiler/rustc_parse/src/parser/expr.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -3474,19 +3474,9 @@ impl<'a> Parser<'a> {
34743474
}
34753475

34763476
fn is_certainly_not_a_block(&self) -> bool {
3477+
// `{ ident, ` and `{ ident: ` cannot start a block.
34773478
self.look_ahead(1, |t| t.is_ident())
3478-
&& (
3479-
// `{ ident, ` cannot start a block.
3480-
self.look_ahead(2, |t| t == &token::Comma)
3481-
|| self.look_ahead(2, |t| t == &token::Colon)
3482-
&& (
3483-
// `{ ident: token, ` cannot start a block.
3484-
self.look_ahead(4, |t| t == &token::Comma)
3485-
// `{ ident: ` cannot start a block unless it's a type ascription
3486-
// `ident: Type`.
3487-
|| self.look_ahead(3, |t| !t.can_begin_type())
3488-
)
3489-
)
3479+
&& self.look_ahead(2, |t| t == &token::Comma || t == &token::Colon)
34903480
}
34913481

34923482
fn maybe_parse_struct_expr(

tests/ui/parser/issues/issue-111692.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ mod module {
99
}
1010

1111
fn test(x: module::Type) {
12-
if x == module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
12+
if x == module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here
1313
}
1414
}
1515

1616
fn test2(x: module::Type) {
17-
if x ==module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
17+
if x ==module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here
1818
}
1919
}
2020

2121

2222
fn test3(x: module::Type) {
23-
if x == Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
23+
use module::Type;
24+
if x == Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here
2425
}
2526
}
2627

2728
fn test4(x: module::Type) {
28-
if x == demo_module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
29+
use module as demo_module;
30+
if x == demo_module::Type { x: module::C, y: 1 } { //~ ERROR struct literals are not allowed here
2931
}
3032
}
3133

tests/ui/parser/issues/issue-111692.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
error: invalid struct literal
2-
--> $DIR/issue-111692.rs:12:21
1+
error: struct literals are not allowed here
2+
--> $DIR/issue-111692.rs:12:13
33
|
44
LL | if x == module::Type { x: module::C, y: 1 } {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
help: you might need to surround the struct literal with parentheses
7+
help: surround the struct literal with parentheses
88
|
99
LL | if x == (module::Type { x: module::C, y: 1 }) {
1010
| + +
1111

12-
error: invalid struct literal
13-
--> $DIR/issue-111692.rs:17:20
12+
error: struct literals are not allowed here
13+
--> $DIR/issue-111692.rs:17:12
1414
|
1515
LL | if x ==module::Type { x: module::C, y: 1 } {
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
|
18-
help: you might need to surround the struct literal with parentheses
18+
help: surround the struct literal with parentheses
1919
|
2020
LL | if x ==(module::Type { x: module::C, y: 1 }) {
2121
| + +
2222

23-
error: invalid struct literal
24-
--> $DIR/issue-111692.rs:23:13
23+
error: struct literals are not allowed here
24+
--> $DIR/issue-111692.rs:24:13
2525
|
2626
LL | if x == Type { x: module::C, y: 1 } {
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2828
|
29-
help: you might need to surround the struct literal with parentheses
29+
help: surround the struct literal with parentheses
3030
|
3131
LL | if x == (Type { x: module::C, y: 1 }) {
3232
| + +
3333

34-
error: invalid struct literal
35-
--> $DIR/issue-111692.rs:28:26
34+
error: struct literals are not allowed here
35+
--> $DIR/issue-111692.rs:30:13
3636
|
3737
LL | if x == demo_module::Type { x: module::C, y: 1 } {
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939
|
40-
help: you might need to surround the struct literal with parentheses
40+
help: surround the struct literal with parentheses
4141
|
4242
LL | if x == (demo_module::Type { x: module::C, y: 1 }) {
4343
| + +

tests/ui/parser/method-call-on-struct-literal-in-if-condition.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ impl Example {
77
fn one() -> i32 { 1 }
88

99
fn main() {
10-
if Example { a: one(), }.is_pos() { //~ ERROR invalid struct literal
10+
if Example { a: one(), }.is_pos() { //~ ERROR struct literals are not allowed here
1111
println!("Positive!");
1212
}
1313
}

tests/ui/parser/method-call-on-struct-literal-in-if-condition.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: invalid struct literal
1+
error: struct literals are not allowed here
22
--> $DIR/method-call-on-struct-literal-in-if-condition.rs:10:8
33
|
44
LL | if Example { a: one(), }.is_pos() {
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
help: you might need to surround the struct literal with parentheses
7+
help: surround the struct literal with parentheses
88
|
99
LL | if (Example { a: one(), }).is_pos() {
1010
| + +

tests/ui/parser/type-ascription-in-pattern.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
fn foo(x: bool) -> i32 {
2-
match x {
2+
match x { //~ ERROR struct literals are not allowed here
33
x: i32 => x, //~ ERROR expected
4-
//~^ ERROR mismatched types
5-
true => 42.,
6-
false => 0.333,
4+
true => 42., //~ ERROR expected identifier
5+
false => 0.333, //~ ERROR expected identifier
76
}
8-
}
7+
} //~ ERROR expected one of
98

109
fn main() {
1110
match foo(true) {

tests/ui/parser/type-ascription-in-pattern.stderr

+58-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,80 @@
1-
error: expected one of `@` or `|`, found `:`
2-
--> $DIR/type-ascription-in-pattern.rs:3:10
1+
error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, `}`, or an operator, found `=>`
2+
--> $DIR/type-ascription-in-pattern.rs:3:16
33
|
4+
LL | match x {
5+
| - while parsing this struct
46
LL | x: i32 => x,
5-
| ^ --- specifying the type of a pattern isn't supported
6-
| |
7-
| expected one of `@` or `|`
7+
| -^^ expected one of 8 possible tokens
8+
| |
9+
| help: try adding a comma: `,`
10+
11+
error: expected identifier, found keyword `true`
12+
--> $DIR/type-ascription-in-pattern.rs:4:9
813
|
9-
help: maybe write a path separator here
14+
LL | match x {
15+
| - while parsing this struct
16+
LL | x: i32 => x,
17+
LL | true => 42.,
18+
| ^^^^ expected identifier, found keyword
19+
20+
error: expected identifier, found keyword `false`
21+
--> $DIR/type-ascription-in-pattern.rs:5:9
1022
|
11-
LL | x::i32 => x,
12-
| ~~
23+
LL | match x {
24+
| - while parsing this struct
25+
...
26+
LL | false => 0.333,
27+
| ^^^^^ expected identifier, found keyword
28+
29+
error: struct literals are not allowed here
30+
--> $DIR/type-ascription-in-pattern.rs:2:11
31+
|
32+
LL | match x {
33+
| ___________^
34+
LL | | x: i32 => x,
35+
LL | | true => 42.,
36+
LL | | false => 0.333,
37+
LL | | }
38+
| |_____^
39+
|
40+
help: surround the struct literal with parentheses
41+
|
42+
LL ~ match (x {
43+
LL | x: i32 => x,
44+
LL | true => 42.,
45+
LL | false => 0.333,
46+
LL ~ })
47+
|
48+
49+
error: expected one of `.`, `?`, `{`, or an operator, found `}`
50+
--> $DIR/type-ascription-in-pattern.rs:7:1
51+
|
52+
LL | match x {
53+
| ----- while parsing this `match` expression
54+
...
55+
LL | }
56+
| - expected one of `.`, `?`, `{`, or an operator
57+
LL | }
58+
| ^ unexpected token
1359

1460
error: expected one of `...`, `..=`, `..`, or `|`, found `:`
15-
--> $DIR/type-ascription-in-pattern.rs:12:11
61+
--> $DIR/type-ascription-in-pattern.rs:11:11
1662
|
1763
LL | 42: i32 => (),
1864
| ^ --- specifying the type of a pattern isn't supported
1965
| |
2066
| expected one of `...`, `..=`, `..`, or `|`
2167

2268
error: expected `|`, found `:`
23-
--> $DIR/type-ascription-in-pattern.rs:13:10
69+
--> $DIR/type-ascription-in-pattern.rs:12:10
2470
|
2571
LL | _: f64 => (),
2672
| ^ --- specifying the type of a pattern isn't supported
2773
| |
2874
| expected `|`
2975

3076
error: expected one of `@` or `|`, found `:`
31-
--> $DIR/type-ascription-in-pattern.rs:14:10
77+
--> $DIR/type-ascription-in-pattern.rs:13:10
3278
|
3379
LL | x: i32 => (),
3480
| ^ --- specifying the type of a pattern isn't supported
@@ -40,15 +86,5 @@ help: maybe write a path separator here
4086
LL | x::i32 => (),
4187
| ~~
4288

43-
error[E0308]: mismatched types
44-
--> $DIR/type-ascription-in-pattern.rs:3:19
45-
|
46-
LL | fn foo(x: bool) -> i32 {
47-
| --- expected `i32` because of return type
48-
LL | match x {
49-
LL | x: i32 => x,
50-
| ^ expected `i32`, found `bool`
51-
52-
error: aborting due to 5 previous errors
89+
error: aborting due to 8 previous errors
5390

54-
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)