Skip to content

Commit f82ca8b

Browse files
committed
Add more error cases to issue 35813 tests
1 parent 0cf2049 commit f82ca8b

File tree

2 files changed

+225
-58
lines changed

2 files changed

+225
-58
lines changed
Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,129 @@
11
// edition:2018
22
#![crate_type = "lib"]
3+
#![feature(type_ascription)]
34
use std::future::Future;
45
use std::pin::Pin;
56

67
// This tests the parser for "x as Y[z]". It errors, but we want to give useful
78
// errors and parse such that further code gives useful errors.
89
pub fn index_after_as_cast() {
910
vec![1, 2, 3] as Vec<i32>[0];
10-
//~^ ERROR: casts followed by index operators are not supported
11+
//~^ ERROR: casts cannot be followed by indexing
12+
vec![1, 2, 3]: Vec<i32>[0];
13+
//~^ ERROR: casts cannot be followed by indexing
1114
}
1215

1316
pub fn index_after_cast_to_index() {
1417
(&[0]) as &[i32][0];
15-
//~^ ERROR: casts followed by index operators are not supported
18+
//~^ ERROR: casts cannot be followed by indexing
19+
(&[0i32]): &[i32; 1][0];
20+
//~^ ERROR: casts cannot be followed by indexing
21+
}
22+
23+
pub fn cast_after_cast() {
24+
if 5u64 as i32 as u16 == 0u16 {
25+
26+
}
27+
if 5u64: u64: u64 == 0u64 {
28+
29+
}
30+
let _ = 5u64: u64: u64 as u8 as i8 == 9i8;
31+
let _ = 0i32: i32: i32;
32+
let _ = 0 as i32: i32;
33+
let _ = 0i32: i32 as i32;
34+
let _ = 0 as i32 as i32;
35+
let _ = 0i32: i32: i32 as u32 as i32;
1636
}
1737

1838
// this tests that the precedence for `!x as Y.Z` is still what we expect
1939
pub fn precedence() {
2040
let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
21-
//~^ ERROR: casts followed by index operators are not supported
41+
//~^ ERROR: casts cannot be followed by indexing
42+
}
43+
44+
pub fn method_calls() {
45+
0 as i32.max(0);
46+
//~^ ERROR: casts cannot be followed by a method call
47+
0: i32.max(0);
48+
//~^ ERROR: casts cannot be followed by a method call
2249
}
2350

2451
pub fn complex() {
2552
let _ = format!(
26-
"{}",
27-
if true { 33 } else { 44 } as i32.max(0)
28-
//~^ ERROR: casts followed by method call expressions are not supported
53+
"{} and {}",
54+
if true { 33 } else { 44 } as i32.max(0),
55+
//~^ ERROR: casts cannot be followed by a method call
56+
if true { 33 } else { 44 }: i32.max(0)
57+
//~^ ERROR: casts cannot be followed by a method call
2958
);
3059
}
3160

3261
pub fn in_condition() {
3362
if 5u64 as i32.max(0) == 0 {
34-
//~^ ERROR: casts followed by method call expressions are not supported
63+
//~^ ERROR: casts cannot be followed by a method call
64+
}
65+
if 5u64: u64.max(0) == 0 {
66+
//~^ ERROR: casts cannot be followed by a method call
3567
}
3668
}
3769

3870
pub fn inside_block() {
3971
let _ = if true {
4072
5u64 as u32.max(0) == 0
41-
//~^ ERROR: casts followed by method call expressions are not supported
73+
//~^ ERROR: casts cannot be followed by a method call
74+
} else { false };
75+
let _ = if true {
76+
5u64: u64.max(0) == 0
77+
//~^ ERROR: casts cannot be followed by a method call
4278
} else { false };
4379
}
4480

4581
static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
46-
//~^ ERROR: casts followed by index operators are not supported
82+
//~^ ERROR: casts cannot be followed by indexing
83+
84+
static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
85+
//~^ ERROR: casts cannot be followed by indexing
86+
87+
88+
pub fn cast_then_try() -> Result<u64,u64> {
89+
Err(0u64) as Result<u64,u64>?;
90+
//~^ ERROR: casts cannot be followed by ?
91+
Err(0u64): Result<u64,u64>?;
92+
//~^ ERROR: casts cannot be followed by ?
93+
Ok(1)
94+
}
95+
96+
97+
pub fn cast_then_call() {
98+
type F = fn(u8);
99+
// type ascription won't actually do [unique drop fn type] -> fn(u8) casts.
100+
let drop_ptr = drop as fn(u8);
101+
drop as F();
102+
//~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
103+
drop_ptr: F();
104+
//~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
105+
}
106+
107+
pub fn cast_to_fn_should_work() {
108+
let drop_ptr = drop as fn(u8);
109+
drop as fn(u8);
110+
drop_ptr: fn(u8);
111+
}
112+
113+
pub fn parens_after_cast_error() {
114+
let drop_ptr = drop as fn(u8);
115+
drop as fn(u8)(0);
116+
//~^ ERROR: casts cannot be followed by a function call
117+
drop_ptr: fn(u8)(0);
118+
//~^ ERROR: casts cannot be followed by a function call
119+
}
47120

48121
pub async fn cast_then_await() {
49122
Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
50-
//~^ ERROR: casts followed by awaits are not supported
123+
//~^ ERROR: casts cannot be followed by `.await`
124+
125+
Box::pin(noop()): Pin<Box<_>>.await;
126+
//~^ ERROR: casts cannot be followed by `.await`
51127
}
52128

53129
pub async fn noop() {}
@@ -59,5 +135,7 @@ pub struct Foo {
59135

60136
pub fn struct_field() {
61137
Foo::default() as Foo.bar;
62-
//~^ ERROR: casts followed by field access expressions are not supported
138+
//~^ ERROR: cannot be followed by a field access
139+
Foo::default(): Foo.bar;
140+
//~^ ERROR: cannot be followed by a field access
63141
}
Lines changed: 136 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,163 @@
1-
error: casts followed by index operators are not supported
2-
--> $DIR/issue-35813-postfix-after-cast.rs:9:5
1+
error: casts cannot be followed by indexing
2+
--> $DIR/issue-35813-postfix-after-cast.rs:10:5
33
|
44
LL | vec![1, 2, 3] as Vec<i32>[0];
5-
| -------------------------^^^
6-
| |
7-
| help: try surrounding the expression with parentheses: `(vec![1, 2, 3] as Vec<i32>)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(vec![1, 2, 3] as Vec<i32>)`
86

9-
error: casts followed by index operators are not supported
10-
--> $DIR/issue-35813-postfix-after-cast.rs:14:5
7+
error: casts cannot be followed by indexing
8+
--> $DIR/issue-35813-postfix-after-cast.rs:12:5
9+
|
10+
LL | vec![1, 2, 3]: Vec<i32>[0];
11+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(vec![1, 2, 3]: Vec<i32>)`
12+
13+
error: casts cannot be followed by indexing
14+
--> $DIR/issue-35813-postfix-after-cast.rs:17:5
1115
|
1216
LL | (&[0]) as &[i32][0];
13-
| ----------------^^^
14-
| |
15-
| help: try surrounding the expression with parentheses: `((&[0]) as &[i32])`
17+
| ^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `((&[0]) as &[i32])`
1618

17-
error: casts followed by index operators are not supported
18-
--> $DIR/issue-35813-postfix-after-cast.rs:20:18
19+
error: casts cannot be followed by indexing
20+
--> $DIR/issue-35813-postfix-after-cast.rs:19:5
21+
|
22+
LL | (&[0i32]): &[i32; 1][0];
23+
| ^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `((&[0i32]): &[i32; 1])`
24+
25+
error: casts cannot be followed by indexing
26+
--> $DIR/issue-35813-postfix-after-cast.rs:40:18
1927
|
2028
LL | let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
21-
| ---------------------------^^^
22-
| |
23-
| help: try surrounding the expression with parentheses: `(&vec![1, 2, 3] as &Vec<i32>)`
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(&vec![1, 2, 3] as &Vec<i32>)`
2430

25-
error: casts followed by method call expressions are not supported
26-
--> $DIR/issue-35813-postfix-after-cast.rs:33:8
31+
error: casts cannot be followed by a method call
32+
--> $DIR/issue-35813-postfix-after-cast.rs:45:5
33+
|
34+
LL | 0 as i32.max(0);
35+
| ^^^^^^^^ help: try surrounding the expression in parentheses: `(0 as i32)`
36+
37+
error: casts cannot be followed by a method call
38+
--> $DIR/issue-35813-postfix-after-cast.rs:47:5
39+
|
40+
LL | 0: i32.max(0);
41+
| ^^^^^^ help: try surrounding the expression in parentheses: `(0: i32)`
42+
43+
error: casts cannot be followed by a method call
44+
--> $DIR/issue-35813-postfix-after-cast.rs:62:8
2745
|
2846
LL | if 5u64 as i32.max(0) == 0 {
29-
| -----------^^^^^^^
30-
| |
31-
| help: try surrounding the expression with parentheses: `(5u64 as i32)`
47+
| ^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(5u64 as i32)`
3248

33-
error: casts followed by method call expressions are not supported
34-
--> $DIR/issue-35813-postfix-after-cast.rs:40:9
49+
error: casts cannot be followed by a method call
50+
--> $DIR/issue-35813-postfix-after-cast.rs:65:8
51+
|
52+
LL | if 5u64: u64.max(0) == 0 {
53+
| ^^^^^^^^^ help: try surrounding the expression in parentheses: `(5u64: u64)`
54+
55+
error: casts cannot be followed by a method call
56+
--> $DIR/issue-35813-postfix-after-cast.rs:72:9
3557
|
3658
LL | 5u64 as u32.max(0) == 0
37-
| -----------^^^^^^^
38-
| |
39-
| help: try surrounding the expression with parentheses: `(5u64 as u32)`
59+
| ^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(5u64 as u32)`
60+
61+
error: casts cannot be followed by a method call
62+
--> $DIR/issue-35813-postfix-after-cast.rs:76:9
63+
|
64+
LL | 5u64: u64.max(0) == 0
65+
| ^^^^^^^^^ help: try surrounding the expression in parentheses: `(5u64: u64)`
4066

41-
error: casts followed by index operators are not supported
42-
--> $DIR/issue-35813-postfix-after-cast.rs:45:24
67+
error: casts cannot be followed by indexing
68+
--> $DIR/issue-35813-postfix-after-cast.rs:81:24
4369
|
4470
LL | static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
45-
| ------------------^^^^^^
46-
| |
47-
| help: try surrounding the expression with parentheses: `(&[1,2,3] as &[i32])`
71+
| ^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(&[1,2,3] as &[i32])`
72+
73+
error: casts cannot be followed by indexing
74+
--> $DIR/issue-35813-postfix-after-cast.rs:84:25
75+
|
76+
LL | static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
77+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(&[1i32,2,3]: &[i32; 3])`
78+
79+
error: casts cannot be followed by ?
80+
--> $DIR/issue-35813-postfix-after-cast.rs:89:5
81+
|
82+
LL | Err(0u64) as Result<u64,u64>?;
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(Err(0u64) as Result<u64,u64>)`
84+
85+
error: casts cannot be followed by ?
86+
--> $DIR/issue-35813-postfix-after-cast.rs:91:5
87+
|
88+
LL | Err(0u64): Result<u64,u64>?;
89+
| ^^^^^^^^^-^^^^^^^^^^^^^^^^
90+
| |
91+
| help: maybe write a path separator here: `::`
92+
|
93+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
94+
= note: for more information, see https://github.com/rust-lang/rust/issues/23416
4895

49-
error: casts followed by awaits are not supported
50-
--> $DIR/issue-35813-postfix-after-cast.rs:49:5
96+
error: casts cannot be followed by a function call
97+
--> $DIR/issue-35813-postfix-after-cast.rs:115:5
98+
|
99+
LL | drop as fn(u8)(0);
100+
| ^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(drop as fn(u8))`
101+
102+
error: casts cannot be followed by a function call
103+
--> $DIR/issue-35813-postfix-after-cast.rs:117:5
104+
|
105+
LL | drop_ptr: fn(u8)(0);
106+
| ^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(drop_ptr: fn(u8))`
107+
108+
error: casts cannot be followed by `.await`
109+
--> $DIR/issue-35813-postfix-after-cast.rs:122:5
51110
|
52111
LL | Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
53-
| -----------------------------------------------------^^^^^^
54-
| |
55-
| help: try surrounding the expression with parentheses: `(Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>)`
112+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>)`
113+
114+
error: casts cannot be followed by `.await`
115+
--> $DIR/issue-35813-postfix-after-cast.rs:125:5
116+
|
117+
LL | Box::pin(noop()): Pin<Box<_>>.await;
118+
| ^^^^^^^^^^^^^^^^-^^^^^^^^^^^^
119+
| |
120+
| help: maybe write a path separator here: `::`
121+
|
122+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
123+
= note: for more information, see https://github.com/rust-lang/rust/issues/23416
56124

57-
error: casts followed by field access expressions are not supported
58-
--> $DIR/issue-35813-postfix-after-cast.rs:61:5
125+
error: casts cannot be followed by a field access
126+
--> $DIR/issue-35813-postfix-after-cast.rs:137:5
59127
|
60128
LL | Foo::default() as Foo.bar;
61-
| ---------------------^^^^
62-
| |
63-
| help: try surrounding the expression with parentheses: `(Foo::default() as Foo)`
129+
| ^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(Foo::default() as Foo)`
130+
131+
error: casts cannot be followed by a field access
132+
--> $DIR/issue-35813-postfix-after-cast.rs:139:5
133+
|
134+
LL | Foo::default(): Foo.bar;
135+
| ^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(Foo::default(): Foo)`
136+
137+
error: casts cannot be followed by a method call
138+
--> $DIR/issue-35813-postfix-after-cast.rs:54:9
139+
|
140+
LL | if true { 33 } else { 44 } as i32.max(0),
141+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(if true { 33 } else { 44 } as i32)`
142+
143+
error: casts cannot be followed by a method call
144+
--> $DIR/issue-35813-postfix-after-cast.rs:56:9
145+
|
146+
LL | if true { 33 } else { 44 }: i32.max(0)
147+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try surrounding the expression in parentheses: `(if true { 33 } else { 44 }: i32)`
148+
149+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
150+
--> $DIR/issue-35813-postfix-after-cast.rs:101:13
151+
|
152+
LL | drop as F();
153+
| ^^^ only `Fn` traits may use parentheses
64154

65-
error: casts followed by method call expressions are not supported
66-
--> $DIR/issue-35813-postfix-after-cast.rs:27:9
155+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
156+
--> $DIR/issue-35813-postfix-after-cast.rs:103:15
67157
|
68-
LL | if true { 33 } else { 44 } as i32.max(0)
69-
| ---------------------------------^^^^^^^
70-
| |
71-
| help: try surrounding the expression with parentheses: `(if true { 33 } else { 44 } as i32)`
158+
LL | drop_ptr: F();
159+
| ^^^ only `Fn` traits may use parentheses
72160

73-
error: aborting due to 9 previous errors
161+
error: aborting due to 25 previous errors
74162

163+
For more information about this error, try `rustc --explain E0214`.

0 commit comments

Comments
 (0)