parser: gracefully handle fn foo(A | B: type).

This commit is contained in:
Mazdak Farrokhzad 2019-08-25 04:39:28 +02:00
parent 083963e58c
commit 1caaa40768
23 changed files with 125 additions and 97 deletions

View file

@ -988,7 +988,7 @@ impl<'a> Parser<'a> {
let (pat, ty) = if is_name_required || self.is_named_argument() {
debug!("parse_arg_general parse_pat (is_name_required:{})", is_name_required);
let pat = self.parse_pat(Some("argument name"))?;
let pat = self.parse_fn_param_pat()?;
if let Err(mut err) = self.expect(&token::Colon) {
if let Some(ident) = self.argument_without_type(
&mut err,

View file

@ -1,7 +1,7 @@
use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle};
use super::{BlockMode, SemiColonMode};
use super::{SeqSep, TokenExpectType};
use super::pat::GateOr;
use super::pat::{GateOr, PARAM_EXPECTED};
use crate::maybe_recover_from_interpolated_ty_qpath;
use crate::ptr::P;
@ -1176,7 +1176,7 @@ impl<'a> Parser<'a> {
fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
let lo = self.token.span;
let attrs = self.parse_arg_attributes()?;
let pat = self.parse_pat(Some("argument name"))?;
let pat = self.parse_pat(PARAM_EXPECTED)?;
let t = if self.eat(&token::Colon) {
self.parse_ty()?
} else {

View file

@ -14,6 +14,9 @@ use errors::{Applicability, DiagnosticBuilder};
type Expected = Option<&'static str>;
/// `Expected` for function and lambda parameter patterns.
pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
/// Whether or not an or-pattern should be gated when occurring in the current context.
#[derive(PartialEq)]
pub enum GateOr { Yes, No }
@ -49,7 +52,7 @@ impl<'a> Parser<'a> {
let gated_leading_vert = self.eat_or_separator() && gate_or == GateOr::Yes;
// Parse the possibly-or-pattern.
let pat = self.parse_pat_with_or(gate_or, TopLevel::Yes)?;
let pat = self.parse_pat_with_or(None, gate_or, TopLevel::Yes)?;
// If we parsed a leading `|` which should be gated,
// and no other gated or-pattern has been parsed thus far,
@ -65,11 +68,38 @@ impl<'a> Parser<'a> {
Ok(pat)
}
/// Parse the pattern for a function or function pointer parameter.
/// Special recovery is provided for or-patterns and leading `|`.
pub(super) fn parse_fn_param_pat(&mut self) -> PResult<'a, P<Pat>> {
self.recover_leading_vert("not allowed in a parameter pattern");
let pat = self.parse_pat_with_or(PARAM_EXPECTED, GateOr::No, TopLevel::No)?;
if let PatKind::Or(..) = &pat.node {
self.ban_illegal_fn_param_or_pat(&pat);
}
Ok(pat)
}
/// Ban `A | B` immediately in a parameter pattern and suggest wrapping in parens.
fn ban_illegal_fn_param_or_pat(&self, pat: &Pat) {
let msg = "wrap the pattern in parenthesis";
let fix = format!("({})", pprust::pat_to_string(pat));
self.struct_span_err(pat.span, "an or-pattern parameter must be wrapped in parenthesis")
.span_suggestion(pat.span, msg, fix, Applicability::MachineApplicable)
.emit();
}
/// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
/// Corresponds to `pat<allow_top_alt>` in RFC 2535.
fn parse_pat_with_or(&mut self, gate_or: GateOr, top_level: TopLevel) -> PResult<'a, P<Pat>> {
fn parse_pat_with_or(
&mut self,
expected: Expected,
gate_or: GateOr,
top_level: TopLevel,
) -> PResult<'a, P<Pat>> {
// Parse the first pattern.
let first_pat = self.parse_pat(None)?;
let first_pat = self.parse_pat(expected)?;
self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
// If the next token is not a `|`,
@ -81,7 +111,7 @@ impl<'a> Parser<'a> {
let lo = first_pat.span;
let mut pats = vec![first_pat];
while self.eat_or_separator() {
let pat = self.parse_pat(None).map_err(|mut err| {
let pat = self.parse_pat(expected).map_err(|mut err| {
err.span_label(lo, "while parsing this or-pattern staring here");
err
})?;
@ -176,18 +206,18 @@ impl<'a> Parser<'a> {
/// Recursive possibly-or-pattern parser with recovery for an erroneous leading `|`.
/// See `parse_pat_with_or` for details on parsing or-patterns.
fn parse_pat_with_or_inner(&mut self) -> PResult<'a, P<Pat>> {
self.recover_inner_leading_vert();
self.parse_pat_with_or(GateOr::Yes, TopLevel::No)
self.recover_leading_vert("only allowed in a top-level pattern");
self.parse_pat_with_or(None, GateOr::Yes, TopLevel::No)
}
/// Recover if `|` or `||` is here.
/// The user is thinking that a leading `|` is allowed in this position.
fn recover_inner_leading_vert(&mut self) {
fn recover_leading_vert(&mut self, ctx: &str) {
if let token::BinOp(token::Or) | token::OrOr = self.token.kind {
let span = self.token.span;
let rm_msg = format!("remove the `{}`", pprust::token_to_string(&self.token));
self.struct_span_err(span, "a leading `|` is only allowed in a top-level pattern")
self.struct_span_err(span, &format!("a leading `|` is {}", ctx))
.span_suggestion(span, &rm_msg, String::new(), Applicability::MachineApplicable)
.emit();

View file

@ -3,7 +3,7 @@
// edition:2018
trait T {
fn foo(i32); //~ expected one of `:` or `@`, found `)`
fn foo(i32); //~ expected one of `:`, `@`, or `|`, found `)`
fn bar_with_default_impl(String, String) {}
//~^ ERROR expected one of `:`

View file

@ -1,8 +1,8 @@
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/anon-params-denied-2018.rs:6:15
|
LL | fn foo(i32);
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
@ -14,11 +14,11 @@ help: if this is a type, explicitly ignore the parameter name
LL | fn foo(_: i32);
| ^^^^^^
error: expected one of `:` or `@`, found `,`
error: expected one of `:`, `@`, or `|`, found `,`
--> $DIR/anon-params-denied-2018.rs:8:36
|
LL | fn bar_with_default_impl(String, String) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
@ -30,11 +30,11 @@ help: if this is a type, explicitly ignore the parameter name
LL | fn bar_with_default_impl(_: String, String) {}
| ^^^^^^^^^
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/anon-params-denied-2018.rs:8:44
|
LL | fn bar_with_default_impl(String, String) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
@ -46,11 +46,11 @@ help: if this is a type, explicitly ignore the parameter name
LL | fn bar_with_default_impl(String, _: String) {}
| ^^^^^^^^^
error: expected one of `:` or `@`, found `,`
error: expected one of `:`, `@`, or `|`, found `,`
--> $DIR/anon-params-denied-2018.rs:13:22
|
LL | fn baz(a:usize, b, c: usize) -> usize {
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type

View file

@ -1,12 +0,0 @@
error[E0658]: or-patterns syntax is experimental
--> $DIR/feature-gate-or_patterns-leading.rs:7:11
|
LL | let | A;
| ^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -25,7 +25,11 @@ fn no_top_level_or_patterns() {
// -------- This looks like an or-pattern but is in fact `|A| (B: E | ())`.
// ...and for now neither do we allow or-patterns at the top level of functions.
fn fun(A | B: E) {} //~ ERROR expected one of `:` or `@`, found `|`
fn fun1(A | B: E) {} //~ ERROR an or-pattern parameter must be wrapped in parenthesis
fn fun2(| A | B: E) {}
//~^ ERROR a leading `|` is not allowed in a parameter pattern
//~| ERROR an or-pattern parameter must be wrapped in parenthesis
}
// We also do not allow a leading `|` when not in a top level position:

View file

@ -1,59 +1,71 @@
error: expected one of `:` or `@`, found `|`
--> $DIR/or-patterns-syntactic-fail.rs:28:14
error: an or-pattern parameter must be wrapped in parenthesis
--> $DIR/or-patterns-syntactic-fail.rs:28:13
|
LL | fn fun(A | B: E) {}
| ^ expected one of `:` or `@` here
LL | fn fun1(A | B: E) {}
| ^^^^^ help: wrap the pattern in parenthesis: `(A | B)`
error: a leading `|` is not allowed in a parameter pattern
--> $DIR/or-patterns-syntactic-fail.rs:30:13
|
LL | fn fun2(| A | B: E) {}
| ^ help: remove the `|`
error: an or-pattern parameter must be wrapped in parenthesis
--> $DIR/or-patterns-syntactic-fail.rs:30:15
|
LL | fn fun2(| A | B: E) {}
| ^^^^^ help: wrap the pattern in parenthesis: `(A | B)`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:37:11
--> $DIR/or-patterns-syntactic-fail.rs:41:11
|
LL | let ( | A | B) = E::A;
| ^ help: remove the `|`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:38:11
--> $DIR/or-patterns-syntactic-fail.rs:42:11
|
LL | let ( | A | B,) = (E::B,);
| ^ help: remove the `|`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:39:11
--> $DIR/or-patterns-syntactic-fail.rs:43:11
|
LL | let [ | A | B ] = [E::A];
| ^ help: remove the `|`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:40:13
--> $DIR/or-patterns-syntactic-fail.rs:44:13
|
LL | let TS( | A | B );
| ^ help: remove the `|`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:41:17
--> $DIR/or-patterns-syntactic-fail.rs:45:17
|
LL | let NS { f: | A | B };
| ^ help: remove the `|`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:43:11
--> $DIR/or-patterns-syntactic-fail.rs:47:11
|
LL | let ( || A | B) = E::A;
| ^^ help: remove the `||`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:44:11
--> $DIR/or-patterns-syntactic-fail.rs:48:11
|
LL | let [ || A | B ] = [E::A];
| ^^ help: remove the `||`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:45:13
--> $DIR/or-patterns-syntactic-fail.rs:49:13
|
LL | let TS( || A | B );
| ^^ help: remove the `||`
error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:46:17
--> $DIR/or-patterns-syntactic-fail.rs:50:17
|
LL | let NS { f: || A | B };
| ^^ help: remove the `||`
@ -95,7 +107,7 @@ LL | let _ = |A | B: E| ();
= note: an implementation of `std::ops::BitOr` might be missing for `E`
error[E0308]: mismatched types
--> $DIR/or-patterns-syntactic-fail.rs:48:36
--> $DIR/or-patterns-syntactic-fail.rs:52:36
|
LL | let recovery_witness: String = 0;
| ^
@ -106,7 +118,7 @@ LL | let recovery_witness: String = 0;
= note: expected type `std::string::String`
found type `{integer}`
error: aborting due to 14 previous errors
error: aborting due to 16 previous errors
Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.

View file

@ -2,29 +2,29 @@ struct S;
impl S {
fn foo(&self, &str bar) {}
//~^ ERROR expected one of `:` or `@`
//~^ ERROR expected one of `:`, `@`
//~| HELP declare the type after the parameter binding
//~| SUGGESTION <identifier>: <type>
}
fn baz(S quux, xyzzy: i32) {}
//~^ ERROR expected one of `:` or `@`
//~^ ERROR expected one of `:`, `@`
//~| HELP declare the type after the parameter binding
//~| SUGGESTION <identifier>: <type>
fn one(i32 a b) {}
//~^ ERROR expected one of `:` or `@`
//~^ ERROR expected one of `:`, `@`
fn pattern((i32, i32) (a, b)) {}
//~^ ERROR expected `:`
//~^ ERROR expected one of `:`
fn fizz(i32) {}
//~^ ERROR expected one of `:` or `@`
//~^ ERROR expected one of `:`, `@`
//~| HELP if this was a parameter name, give it a type
//~| HELP if this is a type, explicitly ignore the parameter name
fn missing_colon(quux S) {}
//~^ ERROR expected one of `:` or `@`
//~^ ERROR expected one of `:`, `@`
//~| HELP declare the type after the parameter binding
//~| SUGGESTION <identifier>: <type>

View file

@ -1,38 +1,38 @@
error: expected one of `:` or `@`, found `bar`
error: expected one of `:`, `@`, or `|`, found `bar`
--> $DIR/inverted-parameters.rs:4:24
|
LL | fn foo(&self, &str bar) {}
| -----^^^
| | |
| | expected one of `:` or `@` here
| | expected one of `:`, `@`, or `|` here
| help: declare the type after the parameter binding: `<identifier>: <type>`
error: expected one of `:` or `@`, found `quux`
error: expected one of `:`, `@`, or `|`, found `quux`
--> $DIR/inverted-parameters.rs:10:10
|
LL | fn baz(S quux, xyzzy: i32) {}
| --^^^^
| | |
| | expected one of `:` or `@` here
| | expected one of `:`, `@`, or `|` here
| help: declare the type after the parameter binding: `<identifier>: <type>`
error: expected one of `:` or `@`, found `a`
error: expected one of `:`, `@`, or `|`, found `a`
--> $DIR/inverted-parameters.rs:15:12
|
LL | fn one(i32 a b) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
error: expected `:`, found `(`
error: expected one of `:` or `|`, found `(`
--> $DIR/inverted-parameters.rs:18:23
|
LL | fn pattern((i32, i32) (a, b)) {}
| ^ expected `:`
| ^ expected one of `:` or `|` here
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/inverted-parameters.rs:21:12
|
LL | fn fizz(i32) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
@ -44,13 +44,13 @@ help: if this is a type, explicitly ignore the parameter name
LL | fn fizz(_: i32) {}
| ^^^^^^
error: expected one of `:` or `@`, found `S`
error: expected one of `:`, `@`, or `|`, found `S`
--> $DIR/inverted-parameters.rs:26:23
|
LL | fn missing_colon(quux S) {}
| -----^
| | |
| | expected one of `:` or `@` here
| | expected one of `:`, `@`, or `|` here
| help: declare the type after the parameter binding: `<identifier>: <type>`
error: aborting due to 6 previous errors

View file

@ -2,7 +2,7 @@ struct S;
impl S {
fn f(*, a: u8) -> u8 {}
//~^ ERROR expected argument name, found `*`
//~^ ERROR expected parameter name, found `*`
}
fn main() {}

View file

@ -1,8 +1,8 @@
error: expected argument name, found `*`
error: expected parameter name, found `*`
--> $DIR/issue-33413.rs:4:10
|
LL | fn f(*, a: u8) -> u8 {}
| ^ expected argument name
| ^ expected parameter name
error: aborting due to previous error

View file

@ -1,3 +1,3 @@
// error-pattern: aborting due to 6 previous errors
// error-pattern: aborting due to 5 previous errors
fn i(n{...,f #

View file

@ -28,17 +28,11 @@ error: expected `[`, found `}`
LL | fn i(n{...,f #
| ^ expected `[`
error: expected `:`, found `)`
error: expected one of `:` or `|`, found `)`
--> $DIR/issue-63135.rs:3:15
|
LL | fn i(n{...,f #
| ^ expected `:`
| ^ expected one of `:` or `|` here
error: expected one of `->`, `where`, or `{`, found `<eof>`
--> $DIR/issue-63135.rs:3:15
|
LL | fn i(n{...,f #
| ^ expected one of `->`, `where`, or `{` here
error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

View file

@ -1,4 +1,4 @@
fn foo(x) { //~ ERROR expected one of `:` or `@`, found `)`
fn foo(x) { //~ ERROR expected one of `:`, `@`, or `|`, found `)`
}
fn main() {}

View file

@ -1,8 +1,8 @@
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/omitted-arg-in-item-fn.rs:1:9
|
LL | fn foo(x) {
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type

View file

@ -1,4 +1,4 @@
fn a(B<) {}
//~^ error: expected one of `:` or `@`, found `<`
//~^ error: expected one of `:`, `@`, or `|`, found `<`
fn main() {}

View file

@ -1,8 +1,8 @@
error: expected one of `:` or `@`, found `<`
error: expected one of `:`, `@`, or `|`, found `<`
--> $DIR/pat-lt-bracket-2.rs:1:7
|
LL | fn a(B<) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
error: aborting due to previous error

View file

@ -1,4 +1,4 @@
fn f(+x: isize) {}
//~^ ERROR expected argument name, found `+`
//~^ ERROR expected parameter name, found `+`
fn main() {}

View file

@ -1,8 +1,8 @@
error: expected argument name, found `+`
error: expected parameter name, found `+`
--> $DIR/removed-syntax-mode.rs:1:6
|
LL | fn f(+x: isize) {}
| ^ expected argument name
| ^ expected parameter name
error: aborting due to previous error

View file

@ -3,6 +3,6 @@
#![feature(param_attrs)]
trait Trait2015 { fn foo(#[allow(C)] i32); }
//~^ ERROR expected one of `:` or `@`, found `)`
//~^ ERROR expected one of `:`, `@`, or `|`, found `)`
fn main() {}

View file

@ -1,8 +1,8 @@
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/param-attrs-2018.rs:5:41
|
LL | trait Trait2015 { fn foo(#[allow(C)] i32); }
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type

View file

@ -1,14 +1,14 @@
error: expected one of `:` or `@`, found `<`
error: expected one of `:`, `@`, or `|`, found `<`
--> $DIR/issue-34264.rs:1:14
|
LL | fn foo(Option<i32>, String) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
error: expected one of `:` or `@`, found `)`
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/issue-34264.rs:1:27
|
LL | fn foo(Option<i32>, String) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
@ -20,11 +20,11 @@ help: if this is a type, explicitly ignore the parameter name
LL | fn foo(Option<i32>, _: String) {}
| ^^^^^^^^^
error: expected one of `:` or `@`, found `,`
error: expected one of `:`, `@`, or `|`, found `,`
--> $DIR/issue-34264.rs:3:9
|
LL | fn bar(x, y: usize) {}
| ^ expected one of `:` or `@` here
| ^ expected one of `:`, `@`, or `|` here
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type