Rollup merge of #126851 - nnethercote:NtExprKind-NtPatKind, r=compiler-errors
Rework pattern and expression nonterminal kinds. Some tweaks to `NonterminalKind` that will assist with #124141. Details in the individual commits. r? compiler-errors cc ```@eholk```
This commit is contained in:
commit
0149bc47d8
5 changed files with 98 additions and 89 deletions
|
@ -10,7 +10,9 @@ use crate::mbe::transcribe::transcribe;
|
|||
|
||||
use ast::token::IdentIsRaw;
|
||||
use rustc_ast as ast;
|
||||
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind, TokenKind::*};
|
||||
use rustc_ast::token::{
|
||||
self, Delimiter, NonterminalKind, NtPatKind::*, Token, TokenKind, TokenKind::*,
|
||||
};
|
||||
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
|
||||
use rustc_ast::{NodeId, DUMMY_NODE_ID};
|
||||
use rustc_ast_pretty::pprust;
|
||||
|
@ -1145,14 +1147,17 @@ fn check_matcher_core<'tt>(
|
|||
// Macros defined in the current crate have a real node id,
|
||||
// whereas macros from an external crate have a dummy id.
|
||||
if def.id != DUMMY_NODE_ID
|
||||
&& matches!(kind, NonterminalKind::PatParam { inferred: true })
|
||||
&& matches!(next_token, TokenTree::Token(token) if token.kind == BinOp(token::BinOpToken::Or))
|
||||
&& matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
|
||||
&& matches!(
|
||||
next_token,
|
||||
TokenTree::Token(token) if token.kind == BinOp(token::BinOpToken::Or)
|
||||
)
|
||||
{
|
||||
// It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.
|
||||
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
|
||||
span,
|
||||
name,
|
||||
Some(NonterminalKind::PatParam { inferred: false }),
|
||||
Some(NonterminalKind::Pat(PatParam { inferred: false })),
|
||||
));
|
||||
sess.psess.buffer_lint(
|
||||
RUST_2021_INCOMPATIBLE_OR_PATTERNS,
|
||||
|
@ -1185,14 +1190,14 @@ fn check_matcher_core<'tt>(
|
|||
);
|
||||
err.span_label(sp, format!("not allowed after `{kind}` fragments"));
|
||||
|
||||
if kind == NonterminalKind::PatWithOr
|
||||
if kind == NonterminalKind::Pat(PatWithOr)
|
||||
&& sess.psess.edition.at_least_rust_2021()
|
||||
&& next_token.is_token(&BinOp(token::BinOpToken::Or))
|
||||
{
|
||||
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
|
||||
span,
|
||||
name,
|
||||
Some(NonterminalKind::PatParam { inferred: false }),
|
||||
Some(NonterminalKind::Pat(PatParam { inferred: false })),
|
||||
));
|
||||
err.span_suggestion(
|
||||
span,
|
||||
|
@ -1292,9 +1297,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
|
|||
// maintain
|
||||
IsInFollow::Yes
|
||||
}
|
||||
NonterminalKind::Stmt
|
||||
| NonterminalKind::Expr
|
||||
| NonterminalKind::Expr2021 { inferred: _ } => {
|
||||
NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
|
||||
const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
|
||||
match tok {
|
||||
TokenTree::Token(token) => match token.kind {
|
||||
|
@ -1304,7 +1307,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
|
|||
_ => IsInFollow::No(TOKENS),
|
||||
}
|
||||
}
|
||||
NonterminalKind::PatParam { .. } => {
|
||||
NonterminalKind::Pat(PatParam { .. }) => {
|
||||
const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"];
|
||||
match tok {
|
||||
TokenTree::Token(token) => match token.kind {
|
||||
|
@ -1317,7 +1320,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
|
|||
_ => IsInFollow::No(TOKENS),
|
||||
}
|
||||
}
|
||||
NonterminalKind::PatWithOr => {
|
||||
NonterminalKind::Pat(PatWithOr) => {
|
||||
const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`in`"];
|
||||
match tok {
|
||||
TokenTree::Token(token) => match token.kind {
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::errors;
|
|||
use crate::mbe::macro_parser::count_metavar_decls;
|
||||
use crate::mbe::{Delimited, KleeneOp, KleeneToken, MetaVarExpr, SequenceRepetition, TokenTree};
|
||||
|
||||
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Token};
|
||||
use rustc_ast::token::{self, Delimiter, IdentIsRaw, NonterminalKind, NtExprKind::*, Token};
|
||||
use rustc_ast::{tokenstream, NodeId};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_feature::Features;
|
||||
|
@ -85,36 +85,31 @@ pub(super) fn parse(
|
|||
span.edition()
|
||||
}
|
||||
};
|
||||
let kind =
|
||||
token::NonterminalKind::from_symbol(fragment.name, edition)
|
||||
.unwrap_or_else(|| {
|
||||
let help = match fragment.name {
|
||||
sym::expr_2021 => {
|
||||
format!(
|
||||
"fragment specifier `expr_2021` \
|
||||
requires Rust 2021 or later\n\
|
||||
{VALID_FRAGMENT_NAMES_MSG}"
|
||||
)
|
||||
}
|
||||
_ if edition().at_least_rust_2021()
|
||||
&& features
|
||||
.expr_fragment_specifier_2024 =>
|
||||
{
|
||||
VALID_FRAGMENT_NAMES_MSG_2021.into()
|
||||
}
|
||||
_ => VALID_FRAGMENT_NAMES_MSG.into(),
|
||||
};
|
||||
sess.dcx().emit_err(
|
||||
errors::InvalidFragmentSpecifier {
|
||||
span,
|
||||
fragment,
|
||||
help,
|
||||
},
|
||||
);
|
||||
token::NonterminalKind::Ident
|
||||
let kind = NonterminalKind::from_symbol(fragment.name, edition)
|
||||
.unwrap_or_else(|| {
|
||||
let help = match fragment.name {
|
||||
sym::expr_2021 => {
|
||||
format!(
|
||||
"fragment specifier `expr_2021` \
|
||||
requires Rust 2021 or later\n\
|
||||
{VALID_FRAGMENT_NAMES_MSG}"
|
||||
)
|
||||
}
|
||||
_ if edition().at_least_rust_2021()
|
||||
&& features.expr_fragment_specifier_2024 =>
|
||||
{
|
||||
VALID_FRAGMENT_NAMES_MSG_2021.into()
|
||||
}
|
||||
_ => VALID_FRAGMENT_NAMES_MSG.into(),
|
||||
};
|
||||
sess.dcx().emit_err(errors::InvalidFragmentSpecifier {
|
||||
span,
|
||||
fragment,
|
||||
help,
|
||||
});
|
||||
if kind
|
||||
== (token::NonterminalKind::Expr2021 { inferred: false })
|
||||
NonterminalKind::Ident
|
||||
});
|
||||
if kind == NonterminalKind::Expr(Expr2021 { inferred: false })
|
||||
&& !features.expr_fragment_specifier_2024
|
||||
{
|
||||
rustc_session::parse::feature_err(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue