Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse & reject postfix operators after casts #68985

Merged
merged 12 commits into from
Mar 7, 2020
76 changes: 71 additions & 5 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ impl<'a> Parser<'a> {
// Save the state of the parser before parsing type normally, in case there is a
// LessThan comparison after this cast.
let parser_snapshot_before_type = self.clone();
match self.parse_ty_no_plus() {
Ok(rhs) => Ok(mk_expr(self, rhs)),
let cast_expr = match self.parse_ty_no_plus() {
Ok(rhs) => mk_expr(self, rhs),
Err(mut type_err) => {
// Rewind to before attempting to parse the type with generics, to recover
// from situations like `x as usize < y` in which we first tried to parse
Expand Down Expand Up @@ -606,17 +606,83 @@ impl<'a> Parser<'a> {
)
.emit();

Ok(expr)
expr
}
Err(mut path_err) => {
// Couldn't parse as a path, return original error and parser state.
path_err.cancel();
mem::replace(self, parser_snapshot_after_type);
Err(type_err)
return Err(type_err);
}
}
}
}
};

self.parse_and_disallow_postfix_after_cast(cast_expr)
}

/// Parses a postfix operators such as `.`, `?`, or index (`[]`) after a cast,
/// then emits an error and returns the newly parsed tree.
/// The resulting parse tree for `&x as T[0]` has a precedence of `((&x) as T)[0]`.
fn parse_and_disallow_postfix_after_cast(
&mut self,
cast_expr: P<Expr>,
) -> PResult<'a, P<Expr>> {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
// Hash the memory location of expr before parsing any following postfix operators.
// This will be compared with the hash of the output expression.
// If they different we can assume we parsed another expression because the existing expression is not reallocated.
let mut before_hasher = DefaultHasher::new();
std::ptr::hash(&*cast_expr, &mut before_hasher);
let before_hash = before_hasher.finish();
let span = cast_expr.span;
let with_postfix = self.parse_dot_or_call_expr_with_(cast_expr, span)?;

let mut after_hasher = DefaultHasher::new();
std::ptr::hash(&*with_postfix, &mut after_hasher);
let after_hash = after_hasher.finish();
Centril marked this conversation as resolved.
Show resolved Hide resolved

// Check if an illegal postfix operator has been added after the cast.
// If the resulting expression is not a cast, or has a different memory location, it is an illegal postfix operator.
if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _))
|| after_hash != before_hash
{
let expr_str = self.span_to_snippet(span);

let msg = format!(
"casts cannot be followed by {}",
match with_postfix.kind {
ExprKind::Index(_, _) => "indexing",
ExprKind::Try(_) => "?",
ExprKind::Field(_, _) => "a field access",
ExprKind::MethodCall(_, _) => "a method call",
ExprKind::Call(_, _) => "a function call",
ExprKind::Await(_) => "`.await`",
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
}
);
let mut err = self.struct_span_err(span, &msg);
let suggestion = "try surrounding the expression in parentheses";
// if type ascription is "likely an error", the user will already be getting a useful
Centril marked this conversation as resolved.
Show resolved Hide resolved
// help message, and doesn't need a second.
if self.last_type_ascription.map_or(false, |last_ascription| last_ascription.1) {
self.maybe_annotate_with_ascription(&mut err, false);
} else {
if let Ok(expr_str) = expr_str {
err.span_suggestion(
span,
suggestion,
format!("({})", expr_str),
Applicability::MachineApplicable,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the following instead:

Suggested change
);
let suggestions = vec![(span.shrink_to_lo(), "(".to_string()), (span.shrink_to_hi(), ")".to_string())];
err.span_suggestions(suggestion, suggestions, Applicability::MachineApplicable);

This will make the suggestion never to be inline, but it will point at the places where the braces get added. This makes it easier to see what the suggestion is at a glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had to use multipart_suggestion rather than span_suggestions here to accept a Vec<(Span, String)>, but besides that this is implemented!

} else {
err.span_help(span, suggestion);
}
}
err.emit();
};
Ok(with_postfix)
}

fn parse_assoc_op_ascribe(&mut self, lhs: P<Expr>, lhs_span: Span) -> PResult<'a, P<Expr>> {
Expand Down
141 changes: 141 additions & 0 deletions src/test/ui/parser/issue-35813-postfix-after-cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// edition:2018
daboross marked this conversation as resolved.
Show resolved Hide resolved
#![crate_type = "lib"]
#![feature(type_ascription)]
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 cannot be followed by indexing
vec![1, 2, 3]: Vec<i32>[0];
//~^ ERROR: casts cannot be followed by indexing
}

pub fn index_after_cast_to_index() {
(&[0]) as &[i32][0];
//~^ ERROR: casts cannot be followed by indexing
(&[0i32]): &[i32; 1][0];
//~^ ERROR: casts cannot be followed by indexing
}

pub fn cast_after_cast() {
if 5u64 as i32 as u16 == 0u16 {

}
if 5u64: u64: u64 == 0u64 {

}
let _ = 5u64: u64: u64 as u8 as i8 == 9i8;
let _ = 0i32: i32: i32;
let _ = 0 as i32: i32;
let _ = 0i32: i32 as i32;
let _ = 0 as i32 as i32;
let _ = 0i32: i32: i32 as u32 as i32;
Centril marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 cannot be followed by indexing
}

pub fn method_calls() {
0 as i32.max(0);
//~^ ERROR: casts cannot be followed by a method call
0: i32.max(0);
//~^ ERROR: casts cannot be followed by a method call
}

pub fn complex() {
let _ = format!(
"{} and {}",
if true { 33 } else { 44 } as i32.max(0),
//~^ ERROR: casts cannot be followed by a method call
if true { 33 } else { 44 }: i32.max(0)
//~^ ERROR: casts cannot be followed by a method call
);
}

pub fn in_condition() {
if 5u64 as i32.max(0) == 0 {
//~^ ERROR: casts cannot be followed by a method call
}
if 5u64: u64.max(0) == 0 {
//~^ ERROR: casts cannot be followed by a method call
}
}

pub fn inside_block() {
let _ = if true {
5u64 as u32.max(0) == 0
//~^ ERROR: casts cannot be followed by a method call
} else { false };
let _ = if true {
5u64: u64.max(0) == 0
//~^ ERROR: casts cannot be followed by a method call
} else { false };
}

static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
//~^ ERROR: casts cannot be followed by indexing

static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
//~^ ERROR: casts cannot be followed by indexing


pub fn cast_then_try() -> Result<u64,u64> {
Err(0u64) as Result<u64,u64>?;
//~^ ERROR: casts cannot be followed by ?
Err(0u64): Result<u64,u64>?;
//~^ ERROR: casts cannot be followed by ?
Ok(1)
}


pub fn cast_then_call() {
type F = fn(u8);
// type ascription won't actually do [unique drop fn type] -> fn(u8) casts.
let drop_ptr = drop as fn(u8);
drop as F();
//~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
drop_ptr: F();
//~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
}

pub fn cast_to_fn_should_work() {
let drop_ptr = drop as fn(u8);
drop as fn(u8);
drop_ptr: fn(u8);
}

pub fn parens_after_cast_error() {
let drop_ptr = drop as fn(u8);
drop as fn(u8)(0);
//~^ ERROR: casts cannot be followed by a function call
drop_ptr: fn(u8)(0);
//~^ ERROR: casts cannot be followed by a function call
}

pub async fn cast_then_await() {
Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
//~^ ERROR: casts cannot be followed by `.await`

Box::pin(noop()): Pin<Box<_>>.await;
//~^ ERROR: casts cannot be followed by `.await`
}

pub async fn noop() {}

#[derive(Default)]
pub struct Foo {
pub bar: u32,
}

pub fn struct_field() {
Foo::default() as Foo.bar;
//~^ ERROR: cannot be followed by a field access
Foo::default(): Foo.bar;
//~^ ERROR: cannot be followed by a field access
}
Loading