-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|