3 #![feature(type_ascription)]
4 use std::future::Future;
7 // This tests the parser for "x as Y[z]". It errors, but we want to give useful
8 // errors and parse such that further code gives useful errors.
9 pub fn index_after_as_cast() {
10 vec![1, 2, 3] as Vec<i32>[0];
11 //~^ ERROR: casts cannot be followed by indexing
12 vec![1, 2, 3]: Vec<i32>[0];
13 //~^ ERROR: casts cannot be followed by indexing
16 pub fn index_after_cast_to_index() {
18 //~^ ERROR: casts cannot be followed by indexing
19 (&[0i32]): &[i32; 1][0];
20 //~^ ERROR: casts cannot be followed by indexing
23 pub fn cast_after_cast() {
24 if 5u64 as i32 as u16 == 0u16 {
27 if 5u64: u64: u64 == 0u64 {
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;
38 pub fn cast_cast_method_call() {
39 let _ = 0i32: i32: i32.count_ones();
40 //~^ ERROR: casts cannot be followed by a method call
41 let _ = 0 as i32: i32.count_ones();
42 //~^ ERROR: casts cannot be followed by a method call
43 let _ = 0i32: i32 as i32.count_ones();
44 //~^ ERROR: casts cannot be followed by a method call
45 let _ = 0 as i32 as i32.count_ones();
46 //~^ ERROR: casts cannot be followed by a method call
47 let _ = 0i32: i32: i32 as u32 as i32.count_ones();
48 //~^ ERROR: casts cannot be followed by a method call
49 let _ = 0i32: i32.count_ones(): u32;
50 //~^ ERROR: casts cannot be followed by a method call
51 let _ = 0 as i32.count_ones(): u32;
52 //~^ ERROR: casts cannot be followed by a method call
53 let _ = 0i32: i32.count_ones() as u32;
54 //~^ ERROR: casts cannot be followed by a method call
55 let _ = 0 as i32.count_ones() as u32;
56 //~^ ERROR: casts cannot be followed by a method call
57 let _ = 0i32: i32: i32.count_ones() as u32 as i32;
58 //~^ ERROR: casts cannot be followed by a method call
61 pub fn multiline_error() {
65 //~^^^ ERROR: casts cannot be followed by a method call
68 // this tests that the precedence for `!x as Y.Z` is still what we expect
70 let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
71 //~^ ERROR: casts cannot be followed by indexing
74 pub fn method_calls() {
76 //~^ ERROR: casts cannot be followed by a method call
78 //~^ ERROR: casts cannot be followed by a method call
84 if true { 33 } else { 44 } as i32.max(0),
85 //~^ ERROR: casts cannot be followed by a method call
86 if true { 33 } else { 44 }: i32.max(0)
87 //~^ ERROR: casts cannot be followed by a method call
91 pub fn in_condition() {
92 if 5u64 as i32.max(0) == 0 {
93 //~^ ERROR: casts cannot be followed by a method call
95 if 5u64: u64.max(0) == 0 {
96 //~^ ERROR: casts cannot be followed by a method call
100 pub fn inside_block() {
102 5u64 as u32.max(0) == 0
103 //~^ ERROR: casts cannot be followed by a method call
106 5u64: u64.max(0) == 0
107 //~^ ERROR: casts cannot be followed by a method call
111 static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
112 //~^ ERROR: casts cannot be followed by indexing
114 static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
115 //~^ ERROR: casts cannot be followed by indexing
118 pub fn cast_then_try() -> Result<u64,u64> {
119 Err(0u64) as Result<u64,u64>?;
120 //~^ ERROR: casts cannot be followed by ?
121 Err(0u64): Result<u64,u64>?;
122 //~^ ERROR: casts cannot be followed by ?
127 pub fn cast_then_call() {
129 // type ascription won't actually do [unique drop fn type] -> fn(u8) casts.
130 let drop_ptr = drop as fn(u8);
132 //~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
134 //~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
137 pub fn cast_to_fn_should_work() {
138 let drop_ptr = drop as fn(u8);
143 pub fn parens_after_cast_error() {
144 let drop_ptr = drop as fn(u8);
146 //~^ ERROR: casts cannot be followed by a function call
148 //~^ ERROR: casts cannot be followed by a function call
151 pub async fn cast_then_await() {
152 Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
153 //~^ ERROR: casts cannot be followed by `.await`
155 Box::pin(noop()): Pin<Box<_>>.await;
156 //~^ ERROR: casts cannot be followed by `.await`
159 pub async fn noop() {}
166 pub fn struct_field() {
167 Foo::default() as Foo.bar;
168 //~^ ERROR: cannot be followed by a field access
169 Foo::default(): Foo.bar;
170 //~^ ERROR: cannot be followed by a field access