From cb594cf3730c35fd6c514e98d9c7a0d78a00a02d Mon Sep 17 00:00:00 2001 From: varkor Date: Sat, 15 Sep 2018 18:18:49 +0100 Subject: Treat `dyn` as a keyword in the 2018 edition --- src/libsyntax/parse/parser.rs | 5 +++-- src/libsyntax/parse/token.rs | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f57fca2cfcf..ab1f46c4bb9 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1562,8 +1562,9 @@ impl<'a> Parser<'a> { impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds) } else if self.check_keyword(keywords::Dyn) && - self.look_ahead(1, |t| t.can_begin_bound() && - !can_continue_type_after_non_fn_ident(t)) { + (self.span.edition() == Edition::Edition2018 || + self.look_ahead(1, |t| t.can_begin_bound() && + !can_continue_type_after_non_fn_ident(t))) { self.bump(); // `dyn` // Always parse bounds greedily for better error recovery. let bounds = self.parse_generic_bounds()?; diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 6e8014284ec..01bc7f6ad30 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -136,6 +136,7 @@ fn ident_can_begin_type(ident: ast::Ident, is_raw: bool) -> bool { keywords::Unsafe.name(), keywords::Extern.name(), keywords::Typeof.name(), + keywords::Dyn.name(), ].contains(&ident.name) } -- cgit 1.4.1-3-g733a5 From 9784d3543f524f142aae52da9d2a6ce1eb9d702a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 21 Sep 2018 04:26:36 +0300 Subject: parser: Tweak function parameter parsing to avoid rollback on succesfull path --- src/libsyntax/parse/parser.rs | 62 ++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6ec1ad969ee..963a7206bd6 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1780,27 +1780,32 @@ impl<'a> Parser<'a> { (pat, self.parse_ty()?) } else { debug!("parse_arg_general ident_to_pat"); + let parser_snapshot_before_ty = self.clone(); + let mut ty = self.parse_ty(); + if ty.is_ok() && self.token == token::Colon { + // This wasn't actually a type, but a pattern looking like a type, + // so we are going to rollback and re-parse for recovery. + ty = self.unexpected(); + } + match ty { + Ok(ty) => { + let ident = Ident::new(keywords::Invalid.name(), self.prev_span); + let pat = P(Pat { + id: ast::DUMMY_NODE_ID, + node: PatKind::Ident( + BindingMode::ByValue(Mutability::Immutable), ident, None), + span: ty.span, + }); + (pat, ty) + } + Err(mut err) => { + // Recover from attempting to parse the argument as a type without pattern. + err.cancel(); + mem::replace(self, parser_snapshot_before_ty); + let pat = self.parse_pat()?; + self.expect(&token::Colon)?; + let ty = self.parse_ty()?; - let parser_snapshot_before_pat = self.clone(); - - // Once we can use edition 2018 in the compiler, - // replace this with real try blocks. - macro_rules! try_block { - ($($inside:tt)*) => ( - (||{ ::std::ops::Try::from_ok({ $($inside)* }) })() - ) - } - - // We're going to try parsing the argument as a pattern (even though it's not - // allowed). This way we can provide better errors to the user. - let pat_arg: PResult<'a, _> = try_block! { - let pat = self.parse_pat()?; - self.expect(&token::Colon)?; - (pat, self.parse_ty()?) - }; - - match pat_arg { - Ok((pat, ty)) => { let mut err = self.diagnostic().struct_span_err_with_code( pat.span, "patterns aren't allowed in methods without bodies", @@ -1813,6 +1818,7 @@ impl<'a> Parser<'a> { Applicability::MachineApplicable, ); err.emit(); + // Pretend the pattern is `_`, to avoid duplicate errors from AST validation. let pat = P(Pat { node: PatKind::Wild, @@ -1821,22 +1827,6 @@ impl<'a> Parser<'a> { }); (pat, ty) } - Err(mut err) => { - err.cancel(); - // Recover from attempting to parse the argument as a pattern. This means - // the type is alone, with no name, e.g. `fn foo(u32)`. - mem::replace(self, parser_snapshot_before_pat); - debug!("parse_arg_general ident_to_pat"); - let ident = Ident::new(keywords::Invalid.name(), self.prev_span); - let ty = self.parse_ty()?; - let pat = P(Pat { - id: ast::DUMMY_NODE_ID, - node: PatKind::Ident( - BindingMode::ByValue(Mutability::Immutable), ident, None), - span: ty.span, - }); - (pat, ty) - } } }; -- cgit 1.4.1-3-g733a5 From 06d577d8b25b5f2e131005a6bcc2e142748016e5 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 19 Sep 2018 16:23:21 -0700 Subject: Detect `for _ in in bar {}` typo --- src/libsyntax/parse/parser.rs | 34 ++++++++++++++++------ .../issue-54109-and_instead_of_ampersands.stderr | 12 +++++--- src/test/ui/parser/if-in-in.rs | 5 ++++ src/test/ui/parser/if-in-in.stderr | 13 +++++++++ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 src/test/ui/parser/if-in-in.rs create mode 100644 src/test/ui/parser/if-in-in.stderr (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 48e034b117f..daf1f0857d9 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2702,8 +2702,8 @@ impl<'a> Parser<'a> { token::Literal(token::Float(n), _suf) => { self.bump(); let fstr = n.as_str(); - let mut err = self.diagnostic().struct_span_err(self.prev_span, - &format!("unexpected token: `{}`", n)); + let mut err = self.diagnostic() + .struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n)); err.span_label(self.prev_span, "unexpected token"); if fstr.chars().all(|x| "0123456789.".contains(x)) { let float = match fstr.parse::().ok() { @@ -2863,8 +2863,8 @@ impl<'a> Parser<'a> { let e = self.parse_prefix_expr(None); let (span, e) = self.interpolated_or_expr_span(e)?; let span_of_tilde = lo; - let mut err = self.diagnostic().struct_span_err(span_of_tilde, - "`~` cannot be used as a unary operator"); + let mut err = self.diagnostic() + .struct_span_err(span_of_tilde, "`~` cannot be used as a unary operator"); err.span_suggestion_short_with_applicability( span_of_tilde, "use `!` to perform bitwise negation", @@ -3422,6 +3422,24 @@ impl<'a> Parser<'a> { ); err.emit(); } + let in_span = self.prev_span; + if self.eat_keyword(keywords::In) { + // a common typo: `for _ in in bar {}` + let mut err = self.sess.span_diagnostic.struct_span_err( + self.prev_span, + "expected iterable, found keyword `in`", + ); + err.span_suggestion_short_with_applicability( + in_span.until(self.prev_span), + "remove the duplicated `in`", + String::new(), + Applicability::MachineApplicable, + ); + err.note("if you meant to use emplacement syntax, it is obsolete (for now, anyway)"); + err.note("for more information on the status of emplacement syntax, see <\ + https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>"); + err.emit(); + } let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?; attrs.extend(iattrs); @@ -4760,12 +4778,9 @@ impl<'a> Parser<'a> { if !self.eat(&token::OpenDelim(token::Brace)) { let sp = self.span; let tok = self.this_token_to_string(); - let mut do_not_suggest_help = false; let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok)); - if self.token.is_keyword(keywords::In) || self.token == token::Colon { - do_not_suggest_help = true; - e.span_label(sp, "expected `{`"); - } + let do_not_suggest_help = + self.token.is_keyword(keywords::In) || self.token == token::Colon; if self.token.is_ident_named("and") { e.span_suggestion_short_with_applicability( @@ -4796,6 +4811,7 @@ impl<'a> Parser<'a> { || do_not_suggest_help { // if the next token is an open brace (e.g., `if a b {`), the place- // inside-a-block suggestion would be more likely wrong than right + e.span_label(sp, "expected `{`"); return Err(e); } let mut stmt_span = stmt.span; diff --git a/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr index 22845775aed..aa54425efa3 100644 --- a/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr +++ b/src/test/ui/did_you_mean/issue-54109-and_instead_of_ampersands.stderr @@ -2,16 +2,20 @@ error: expected `{`, found `and` --> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10 | LL | if a and b { - | -- ^^^ help: use `&&` instead of `and` for the boolean operator - | | + | -- ^^^ + | | | + | | expected `{` + | | help: use `&&` instead of `and` for the boolean operator | this `if` statement has a condition, but no block error: expected `{`, found `or` --> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10 | LL | if a or b { - | -- ^^ help: use `||` instead of `or` for the boolean operator - | | + | -- ^^ + | | | + | | expected `{` + | | help: use `||` instead of `or` for the boolean operator | this `if` statement has a condition, but no block error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and` diff --git a/src/test/ui/parser/if-in-in.rs b/src/test/ui/parser/if-in-in.rs new file mode 100644 index 00000000000..9bc9aaff298 --- /dev/null +++ b/src/test/ui/parser/if-in-in.rs @@ -0,0 +1,5 @@ +fn main() { + for i in in 1..2 { + println!("{}", i); + } +} diff --git a/src/test/ui/parser/if-in-in.stderr b/src/test/ui/parser/if-in-in.stderr new file mode 100644 index 00000000000..9926fcc0858 --- /dev/null +++ b/src/test/ui/parser/if-in-in.stderr @@ -0,0 +1,13 @@ +error: expected iterable, found keyword `in` + --> $DIR/if-in-in.rs:2:14 + | +LL | for i in in 1..2 { + | ---^^ + | | + | help: remove the duplicated `in` + | + = note: if you meant to use emplacement syntax, it is obsolete (for now, anyway) + = note: for more information on the status of emplacement syntax, see + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5