]> git.lizzy.rs Git - rust.git/blob - src/test/ui/parser/issue-35813-postfix-after-cast.rs
Remove trailing whitespace
[rust.git] / src / test / ui / parser / issue-35813-postfix-after-cast.rs
1 // edition:2018
2 #![crate_type = "lib"]
3 #![feature(type_ascription)]
4 use std::future::Future;
5 use std::pin::Pin;
6
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
14 }
15
16 pub fn index_after_cast_to_index() {
17     (&[0]) as &[i32][0];
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;
36 }
37
38 // this tests that the precedence for `!x as Y.Z` is still what we expect
39 pub fn precedence() {
40     let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
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
49 }
50
51 pub fn complex() {
52     let _ = format!(
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
58     );
59 }
60
61 pub fn in_condition() {
62     if 5u64 as i32.max(0) == 0 {
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
67     }
68 }
69
70 pub fn inside_block() {
71     let _ = if true {
72         5u64 as u32.max(0) == 0
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
78     } else { false };
79 }
80
81 static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
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 }
120
121 pub async fn cast_then_await() {
122     Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
123     //~^ ERROR: casts cannot be followed by `.await`
124
125     Box::pin(noop()): Pin<Box<_>>.await;
126     //~^ ERROR: casts cannot be followed by `.await`
127 }
128
129 pub async fn noop() {}
130
131 #[derive(Default)]
132 pub struct Foo {
133     pub bar: u32,
134 }
135
136 pub fn struct_field() {
137     Foo::default() as Foo.bar;
138     //~^ ERROR: cannot be followed by a field access
139     Foo::default(): Foo.bar;
140     //~^ ERROR: cannot be followed by a field access
141 }