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

or-patterns: enable :pat to match top_pat #78935

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ pub enum NonterminalKind {
TT,
}

/// Used when parsing a non-terminal (see `parse_nonterminal`) to determine if `:pat` should match
/// `top_pat` or `pat<no_top_alt>`. See issue <https://github.com/rust-lang/rust/pull/78935>.
pub enum OrPatNonterminalMode {
TopPat,
NoTopAlt,
}

impl NonterminalKind {
pub fn from_symbol(symbol: Symbol) -> Option<NonterminalKind> {
Some(match symbol {
Expand Down
32 changes: 29 additions & 3 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ use TokenTreeOrTokenTreeSlice::*;

use crate::mbe::{self, TokenTree};

use rustc_ast::token::{self, DocComment, Nonterminal, Token};
use rustc_ast::token::{
self, BinOpToken, DocComment, Nonterminal, OrPatNonterminalMode, Token, TokenKind,
};
use rustc_parse::parser::Parser;
use rustc_session::parse::ParseSess;
use rustc_span::symbol::MacroRulesNormalizedIdent;
Expand Down Expand Up @@ -414,6 +416,27 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
}
}

/// Check whether the next token in the matcher is `|` (i.e. if we are matching against `$foo:type
/// |`). The reason is that when we stabilized the `or_patterns` feature, we wanted `:pat` to match
/// top-level or-patterns too. But doing that would cause a lot of breakage, since `|` could
/// previously be a separator that follows `:pat`. Thus, we make a special case to parse `:pat` the
/// old way if it happens to be followed by `|` in the matcher.
///
/// See <https://github.com/rust-lang/rust/issues/54883> for more info.
fn or_pat_mode(item: &MatcherPosHandle<'_, '_>) -> OrPatNonterminalMode {
if item.idx < item.top_elts.len() - 1 {
// Look at the token after the current one to see if it is `|`.
let tt = item.top_elts.get_tt(item.idx + 1);
if let TokenTree::Token(Token { kind: TokenKind::BinOp(BinOpToken::Or), .. }) = tt {
OrPatNonterminalMode::NoTopAlt
} else {
OrPatNonterminalMode::TopPat
}
} else {
OrPatNonterminalMode::TopPat
}
}

/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
/// produce more items in `next_items`, `eof_items`, and `bb_items`.
///
Expand Down Expand Up @@ -518,6 +541,8 @@ fn inner_parse_loop<'root, 'tt>(
}
// We are in the middle of a matcher.
else {
let or_pat_mode = or_pat_mode(&item);

// Look at what token in the matcher we are trying to match the current token (`token`)
// against. Depending on that, we may generate new items.
match item.top_elts.get_tt(idx) {
Expand Down Expand Up @@ -559,7 +584,7 @@ fn inner_parse_loop<'root, 'tt>(
TokenTree::MetaVarDecl(_, _, kind) => {
// Built-in nonterminals never start with these tokens,
// so we can eliminate them from consideration.
if Parser::nonterminal_may_begin_with(kind, token) {
if Parser::nonterminal_may_begin_with(kind, token, or_pat_mode) {
bb_items.push(item);
}
}
Expand Down Expand Up @@ -718,9 +743,10 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
assert_eq!(bb_items.len(), 1);

let mut item = bb_items.pop().unwrap();
let or_pat_mode = or_pat_mode(&item);
if let TokenTree::MetaVarDecl(span, _, kind) = item.top_elts.get_tt(item.idx) {
let match_cur = item.match_cur;
let nt = match parser.to_mut().parse_nonterminal(kind) {
let nt = match parser.to_mut().parse_nonterminal(kind, or_pat_mode) {
Err(mut err) => {
err.span_label(
span,
Expand Down
26 changes: 22 additions & 4 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Nonterminal, NonterminalKind, Token};
use rustc_ast::token::{self, Nonterminal, NonterminalKind, OrPatNonterminalMode, Token};
use rustc_ast_pretty::pprust;
use rustc_errors::PResult;
use rustc_span::symbol::{kw, Ident};
Expand All @@ -11,7 +11,11 @@ impl<'a> Parser<'a> {
///
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
/// token. Be conservative (return true) if not sure.
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
pub fn nonterminal_may_begin_with(
kind: NonterminalKind,
token: &Token,
or_pat_mode: OrPatNonterminalMode,
) -> bool {
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
fn may_be_ident(nt: &token::Nonterminal) -> bool {
match *nt {
Expand Down Expand Up @@ -68,6 +72,8 @@ impl<'a> Parser<'a> {
token::ModSep | // path
token::Lt | // path (UFCS constant)
token::BinOp(token::Shl) => true, // path (double UFCS)
// leading vert `|` or-pattern
token::BinOp(token::Or) => matches!(or_pat_mode, OrPatNonterminalMode::TopPat),
token::Interpolated(ref nt) => may_be_ident(nt),
_ => false,
},
Expand All @@ -84,7 +90,12 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, Nonterminal> {
/// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
pub fn parse_nonterminal(
&mut self,
kind: NonterminalKind,
or_pat_mode: OrPatNonterminalMode,
) -> PResult<'a, Nonterminal> {
// Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
// needs to have them force-captured here.
// A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
Expand Down Expand Up @@ -128,7 +139,14 @@ impl<'a> Parser<'a> {
}
}
NonterminalKind::Pat => {
let (mut pat, tokens) = self.collect_tokens(|this| this.parse_pat(None))?;
let (mut pat, tokens) = match or_pat_mode {
OrPatNonterminalMode::TopPat => {
self.collect_tokens(|this| this.parse_top_pat_no_commas())?
}
OrPatNonterminalMode::NoTopAlt => {
self.collect_tokens(|this| this.parse_pat(None))?
}
};
// We have have eaten an NtPat, which could already have tokens
if pat.tokens.is_none() {
pat.tokens = tokens;
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ impl<'a> Parser<'a> {
Ok(pat)
}

pub(super) fn parse_top_pat_no_commas(&mut self) -> PResult<'a, P<Pat>> {
// Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
self.eat_or_separator(None);

// Parse the possibly-or-pattern, but don't recorver commas.
let pat = self.parse_pat_with_or(None, GateOr::No, RecoverComma::No)?;

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>> {
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/macros/macro-pat-follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,7 @@ macro_rules! pat_if {
}}
}

macro_rules! pat_bar {
($p:pat | $p2:pat) => {{
match Some(1u8) {
$p | $p2 => {},
_ => {}
}
}}
}

fn main() {
pat_in!(Some(_) in 0..10);
pat_if!(Some(x) if x > 0);
pat_bar!(Some(1u8) | None);
}
10 changes: 0 additions & 10 deletions src/test/ui/or-patterns/or-patterns-syntactic-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@

fn main() {}

// Test the `pat` macro fragment parser:
macro_rules! accept_pat {
($p:pat) => {}
}

accept_pat!(p | q); //~ ERROR no rules expected the token `|`
accept_pat!(| p | q); //~ ERROR no rules expected the token `|`

// Non-macro tests:

enum E { A, B }
use E::*;

Expand Down
48 changes: 15 additions & 33 deletions src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,103 +1,85 @@
error: an or-pattern parameter must be wrapped in parenthesis
--> $DIR/or-patterns-syntactic-fail.rs:27:13
--> $DIR/or-patterns-syntactic-fail.rs:17:13
|
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:29:13
--> $DIR/or-patterns-syntactic-fail.rs:19: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:29:15
--> $DIR/or-patterns-syntactic-fail.rs:19: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:40:11
--> $DIR/or-patterns-syntactic-fail.rs:30: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:41:11
--> $DIR/or-patterns-syntactic-fail.rs:31: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:42:11
--> $DIR/or-patterns-syntactic-fail.rs:32: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:43:13
--> $DIR/or-patterns-syntactic-fail.rs:33: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:44:17
--> $DIR/or-patterns-syntactic-fail.rs:34: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:46:11
--> $DIR/or-patterns-syntactic-fail.rs:36:11
|
LL | let ( || A | B) = E::A;
| ^^ help: remove the `||`
|
= note: alternatives in or-patterns are separated with `|`, not `||`

error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:47:11
--> $DIR/or-patterns-syntactic-fail.rs:37:11
|
LL | let [ || A | B ] = [E::A];
| ^^ help: remove the `||`
|
= note: alternatives in or-patterns are separated with `|`, not `||`

error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:48:13
--> $DIR/or-patterns-syntactic-fail.rs:38:13
|
LL | let TS( || A | B );
| ^^ help: remove the `||`
|
= note: alternatives in or-patterns are separated with `|`, not `||`

error: a leading `|` is only allowed in a top-level pattern
--> $DIR/or-patterns-syntactic-fail.rs:49:17
--> $DIR/or-patterns-syntactic-fail.rs:39:17
|
LL | let NS { f: || A | B };
| ^^ help: remove the `||`
|
= note: alternatives in or-patterns are separated with `|`, not `||`

error: no rules expected the token `|`
--> $DIR/or-patterns-syntactic-fail.rs:13:15
|
LL | macro_rules! accept_pat {
| ----------------------- when calling this macro
...
LL | accept_pat!(p | q);
| ^ no rules expected this token in macro call

error: no rules expected the token `|`
--> $DIR/or-patterns-syntactic-fail.rs:14:13
|
LL | macro_rules! accept_pat {
| ----------------------- when calling this macro
...
LL | accept_pat!(| p | q);
| ^ no rules expected this token in macro call

error[E0369]: no implementation for `E | ()`
--> $DIR/or-patterns-syntactic-fail.rs:23:22
--> $DIR/or-patterns-syntactic-fail.rs:13:22
|
LL | let _ = |A | B: E| ();
| ----^ -- ()
Expand All @@ -107,7 +89,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:51:36
--> $DIR/or-patterns-syntactic-fail.rs:41:36
|
LL | let recovery_witness: String = 0;
| ------ ^
Expand All @@ -116,7 +98,7 @@ LL | let recovery_witness: String = 0;
| | help: try using a conversion method: `0.to_string()`
| expected due to this

error: aborting due to 16 previous errors
error: aborting due to 14 previous errors

Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.
24 changes: 24 additions & 0 deletions src/test/ui/pattern/or-pattern-macro-pat-fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//#![feature(or_patterns)]

macro_rules! bar {
($nonor:pat |) => {};
}

macro_rules! baz {
($nonor:pat) => {};
}

fn main() {
// Leading vert not allowed in pat<no_top_alt>
bar!(|1| 2 | 3 |); //~ERROR: no rules expected

// Top-level or-patterns not allowed in pat<no_top_alt>
bar!(1 | 2 | 3 |); //~ERROR: no rules expected
bar!(1 | 2 | 3); //~ERROR: no rules expected
bar!((1 | 2 | 3)); //~ERROR: unexpected end
bar!((1 | 2 | 3) |); // ok

baz!(1 | 2 | 3); // ok
baz!(|1| 2 | 3); // ok
baz!(|1| 2 | 3 |); //~ERROR: expected pattern
}
Loading