diff options
| author | David Ross <daboross@daboross.net> | 2020-02-08 21:34:38 -0800 |
|---|---|---|
| committer | David Ross <daboross@daboross.net> | 2020-02-15 19:50:50 -0800 |
| commit | 940f65782cc5df7fecad27b38cc25b6d1eeaf2e8 (patch) | |
| tree | efb0a542523c63ed8de3ca72ee26e5abe3b8f4e0 /src/test/ui/parser | |
| parent | 8ba3ca0e6bef416ecba3c8ded1f67a953d28600f (diff) | |
| download | rust-940f65782cc5df7fecad27b38cc25b6d1eeaf2e8.tar.gz rust-940f65782cc5df7fecad27b38cc25b6d1eeaf2e8.zip | |
Parse & reject postfix operators after casts
This adds parsing for expressions like 'x as Ty[0]' which will immediately error out, but still give the rest of the parser a valid parse tree to continue.
Diffstat (limited to 'src/test/ui/parser')
| -rw-r--r-- | src/test/ui/parser/issue-35813-postfix-after-cast.rs | 63 | ||||
| -rw-r--r-- | src/test/ui/parser/issue-35813-postfix-after-cast.stderr | 74 |
2 files changed, 137 insertions, 0 deletions
diff --git a/src/test/ui/parser/issue-35813-postfix-after-cast.rs b/src/test/ui/parser/issue-35813-postfix-after-cast.rs new file mode 100644 index 00000000000..dd608b263ec --- /dev/null +++ b/src/test/ui/parser/issue-35813-postfix-after-cast.rs @@ -0,0 +1,63 @@ +// edition:2018 +#![crate_type = "lib"] +use std::future::Future; +use std::pin::Pin; + +// This tests the parser for "x as Y[z]". It errors, but we want to give useful +// errors and parse such that further code gives useful errors. +pub fn index_after_as_cast() { + vec![1, 2, 3] as Vec<i32>[0]; + //~^ ERROR: casts followed by index operators are not supported +} + +pub fn index_after_cast_to_index() { + (&[0]) as &[i32][0]; + //~^ ERROR: casts followed by index operators are not supported +} + +// this tests that the precedence for `!x as Y.Z` is still what we expect +pub fn precedence() { + let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0]; + //~^ ERROR: casts followed by index operators are not supported +} + +pub fn complex() { + let _ = format!( + "{}", + if true { 33 } else { 44 } as i32.max(0) + //~^ ERROR: casts followed by method call expressions are not supported + ); +} + +pub fn in_condition() { + if 5u64 as i32.max(0) == 0 { + //~^ ERROR: casts followed by method call expressions are not supported + } +} + +pub fn inside_block() { + let _ = if true { + 5u64 as u32.max(0) == 0 + //~^ ERROR: casts followed by method call expressions are not supported + } else { false }; +} + +static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]); +//~^ ERROR: casts followed by index operators are not supported + +pub async fn cast_then_await() { + Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await; + //~^ ERROR: casts followed by awaits are not supported +} + +pub async fn noop() {} + +#[derive(Default)] +pub struct Foo { + pub bar: u32, +} + +pub fn struct_field() { + Foo::default() as Foo.bar; + //~^ ERROR: casts followed by field access expressions are not supported +} diff --git a/src/test/ui/parser/issue-35813-postfix-after-cast.stderr b/src/test/ui/parser/issue-35813-postfix-after-cast.stderr new file mode 100644 index 00000000000..9459e076ea0 --- /dev/null +++ b/src/test/ui/parser/issue-35813-postfix-after-cast.stderr @@ -0,0 +1,74 @@ +error: casts followed by index operators are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:9:5 + | +LL | vec![1, 2, 3] as Vec<i32>[0]; + | -------------------------^^^ + | | + | help: try surrounding the expression with parentheses: `(vec![1, 2, 3] as Vec<i32>)` + +error: casts followed by index operators are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:14:5 + | +LL | (&[0]) as &[i32][0]; + | ----------------^^^ + | | + | help: try surrounding the expression with parentheses: `((&[0]) as &[i32])` + +error: casts followed by index operators are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:20:18 + | +LL | let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0]; + | ---------------------------^^^ + | | + | help: try surrounding the expression with parentheses: `(&vec![1, 2, 3] as &Vec<i32>)` + +error: casts followed by method call expressions are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:33:8 + | +LL | if 5u64 as i32.max(0) == 0 { + | -----------^^^^^^^ + | | + | help: try surrounding the expression with parentheses: `(5u64 as i32)` + +error: casts followed by method call expressions are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:40:9 + | +LL | 5u64 as u32.max(0) == 0 + | -----------^^^^^^^ + | | + | help: try surrounding the expression with parentheses: `(5u64 as u32)` + +error: casts followed by index operators are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:45:24 + | +LL | static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]); + | ------------------^^^^^^ + | | + | help: try surrounding the expression with parentheses: `(&[1,2,3] as &[i32])` + +error: casts followed by awaits are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:49:5 + | +LL | Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await; + | -----------------------------------------------------^^^^^^ + | | + | help: try surrounding the expression with parentheses: `(Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>)` + +error: casts followed by field access expressions are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:61:5 + | +LL | Foo::default() as Foo.bar; + | ---------------------^^^^ + | | + | help: try surrounding the expression with parentheses: `(Foo::default() as Foo)` + +error: casts followed by method call expressions are not supported + --> $DIR/issue-35813-postfix-after-cast.rs:27:9 + | +LL | if true { 33 } else { 44 } as i32.max(0) + | ---------------------------------^^^^^^^ + | | + | help: try surrounding the expression with parentheses: `(if true { 33 } else { 44 } as i32)` + +error: aborting due to 9 previous errors + |
