Skip to content

Commit

Permalink
Auto merge of rust-lang#119412 - petrochenkov:dialoc3, r=<try>
Browse files Browse the repository at this point in the history
macro_rules: Remove `NtIdent` nonterminal token

It only exists for keeping the second span (the metavariable span), but after rust-lang#119673 such spans can be kept using a more general mechanism.
  • Loading branch information
bors committed Feb 19, 2024
2 parents bea5beb + 8bbdaaa commit b2ad6b1
Show file tree
Hide file tree
Showing 46 changed files with 199 additions and 295 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/ast_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl HasTokens for Nonterminal {
Nonterminal::NtPath(path) => path.tokens(),
Nonterminal::NtVis(vis) => vis.tokens(),
Nonterminal::NtBlock(block) => block.tokens(),
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
Nonterminal::NtLifetime(..) => None,
}
}
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
Expand All @@ -254,7 +254,7 @@ impl HasTokens for Nonterminal {
Nonterminal::NtPath(path) => path.tokens_mut(),
Nonterminal::NtVis(vis) => vis.tokens_mut(),
Nonterminal::NtBlock(block) => block.tokens_mut(),
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
Nonterminal::NtLifetime(..) => None,
}
}
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
token::NtPat(pat) => vis.visit_pat(pat),
token::NtExpr(expr) => vis.visit_expr(expr),
token::NtTy(ty) => vis.visit_ty(ty),
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
token::NtLifetime(ident) => vis.visit_ident(ident),
token::NtLiteral(expr) => vis.visit_expr(expr),
token::NtMeta(item) => {
Expand Down
18 changes: 1 addition & 17 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ pub enum TokenKind {
Literal(Lit),

/// Identifier token.
/// Do not forget about `NtIdent` when you want to match on identifiers.
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
/// treat regular and interpolated identifiers in the same way.
Ident(Symbol, /* is_raw */ bool),
/// Lifetime identifier token.
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
Expand Down Expand Up @@ -590,9 +587,6 @@ impl Token {
pub fn uninterpolate(&self) -> Cow<'_, Token> {
match &self.kind {
Interpolated(nt) => match &nt.0 {
NtIdent(ident, is_raw) => {
Cow::Owned(Token::new(Ident(ident.name, *is_raw), ident.span))
}
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
_ => Cow::Borrowed(self),
},
Expand All @@ -606,10 +600,6 @@ impl Token {
// We avoid using `Token::uninterpolate` here because it's slow.
match &self.kind {
&Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
Interpolated(nt) => match &nt.0 {
NtIdent(ident, is_raw) => Some((*ident, *is_raw)),
_ => None,
},
_ => None,
}
}
Expand Down Expand Up @@ -836,7 +826,6 @@ pub enum Nonterminal {
NtPat(P<ast::Pat>),
NtExpr(P<ast::Expr>),
NtTy(P<ast::Ty>),
NtIdent(Ident, /* is_raw */ bool),
NtLifetime(Ident),
NtLiteral(P<ast::Expr>),
/// Stuff inside brackets for attributes
Expand Down Expand Up @@ -932,7 +921,7 @@ impl Nonterminal {
NtPat(pat) => pat.span,
NtExpr(expr) | NtLiteral(expr) => expr.span,
NtTy(ty) => ty.span,
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
NtLifetime(ident) => ident.span,
NtMeta(attr_item) => attr_item.span(),
NtPath(path) => path.span,
NtVis(vis) => vis.span,
Expand All @@ -948,7 +937,6 @@ impl Nonterminal {
NtExpr(..) => "expression",
NtLiteral(..) => "literal",
NtTy(..) => "type",
NtIdent(..) => "identifier",
NtLifetime(..) => "lifetime",
NtMeta(..) => "attribute",
NtPath(..) => "path",
Expand All @@ -960,9 +948,6 @@ impl Nonterminal {
impl PartialEq for Nonterminal {
fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(NtIdent(ident_lhs, is_raw_lhs), NtIdent(ident_rhs, is_raw_rhs)) => {
ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs
}
(NtLifetime(ident_lhs), NtLifetime(ident_rhs)) => ident_lhs == ident_rhs,
// FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
// correctly based on data from AST. This will prevent them from matching each other
Expand All @@ -982,7 +967,6 @@ impl fmt::Debug for Nonterminal {
NtPat(..) => f.pad("NtPat(..)"),
NtExpr(..) => f.pad("NtExpr(..)"),
NtTy(..) => f.pad("NtTy(..)"),
NtIdent(..) => f.pad("NtIdent(..)"),
NtLiteral(..) => f.pad("NtLiteral(..)"),
NtMeta(..) => f.pad("NtMeta(..)"),
NtPath(..) => f.pad("NtPath(..)"),
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,6 @@ impl TokenStream {

pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
match nt {
Nonterminal::NtIdent(ident, is_raw) => {
TokenStream::token_alone(token::Ident(ident.name, *is_raw), ident.span)
}
Nonterminal::NtLifetime(ident) => {
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
}
Expand All @@ -502,9 +499,6 @@ impl TokenStream {

fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
match &token.kind {
token::Interpolated(nt) if let token::NtIdent(ident, is_raw) = nt.0 => {
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
}
token::Interpolated(nt) => TokenTree::Delimited(
DelimSpan::from_single(token.span),
DelimSpacing::new(Spacing::JointHidden, spacing),
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
token::NtBlock(e) => self.block_to_string(e),
token::NtStmt(e) => self.stmt_to_string(e),
token::NtPat(e) => self.pat_to_string(e),
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(e) => self.expr_to_string(e),
token::NtVis(e) => self.vis_to_string(e),
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
}));
}

Interpolated(ref nt) if let NtIdent(ident, is_raw) = &nt.0 => {
trees.push(TokenTree::Ident(Ident {
sym: ident.name,
is_raw: *is_raw,
span: ident.span,
}))
}

Interpolated(nt) => {
let stream = TokenStream::from_nonterminal_ast(&nt.0);
// A hack used to pass AST fragments to attribute and derive
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ impl<'a> Parser<'a> {
let (mut ret, trailing) = ret?;

// When we're not in `capture-cfg` mode, then bail out early if:
// 1. Our target doesn't support tokens at all (e.g we're parsing an `NtIdent`)
// so there's nothing for us to do.
// 1. Our target doesn't support tokens at all, so there's nothing for us to do.
// 2. Our target already has tokens set (e.g. we've parsed something
// like `#[my_attr] $item`. The actual parsing code takes care of prepending
// any attributes to the nonterminal, so we don't need to modify the
Expand Down
35 changes: 15 additions & 20 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_ast::token::{self, Delimiter, Nonterminal::*, NonterminalKind, Token};
use rustc_ast::HasTokens;
use rustc_ast_pretty::pprust;
use rustc_errors::PResult;
use rustc_span::symbol::{kw, Ident};
use rustc_span::symbol::kw;

use crate::errors::UnexpectedNonterminal;
use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
Expand All @@ -24,7 +24,6 @@ impl<'a> Parser<'a> {
| NtPat(_)
| NtExpr(_)
| NtTy(_)
| NtIdent(..)
| NtLiteral(_) // `true`, `false`
| NtMeta(_)
| NtPath(_) => true,
Expand All @@ -45,7 +44,7 @@ impl<'a> Parser<'a> {
&& !token.is_keyword(kw::Const)
}
NonterminalKind::Ty => token.can_begin_type(),
NonterminalKind::Ident => get_macro_ident(token).is_some(),
NonterminalKind::Ident => is_macro_ident(token),
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
NonterminalKind::Vis => match token.kind {
// The follow-set of :vis + "priv" keyword + interpolated
Expand All @@ -56,8 +55,7 @@ impl<'a> Parser<'a> {
token::OpenDelim(Delimiter::Brace) => true,
token::Interpolated(nt) => match &nt.0 {
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_)
| NtVis(_) => false,
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) | NtVis(_) => false,
},
_ => false,
},
Expand Down Expand Up @@ -108,8 +106,17 @@ impl<'a> Parser<'a> {
// in advance whether or not a proc-macro will be (transitively) invoked,
// we always capture tokens for any `Nonterminal` which needs them.
let mut nt = match kind {
// Note that TT is treated differently to all the others.
// Note that `tt` and `ident` are treated differently to all the others.
NonterminalKind::TT => return Ok(ParseNtResult::Tt(self.parse_token_tree())),
NonterminalKind::Ident if is_macro_ident(&self.token) => {
return Ok(ParseNtResult::Tt(self.parse_token_tree()));
}
NonterminalKind::Ident => {
return Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
span: self.token.span,
token: self.token.clone(),
}));
}
NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
Some(item) => NtItem(item),
None => {
Expand Down Expand Up @@ -153,18 +160,6 @@ impl<'a> Parser<'a> {
NonterminalKind::Ty => {
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
}

// this could be handled like a token, since it is one
NonterminalKind::Ident if let Some((ident, is_raw)) = get_macro_ident(&self.token) => {
self.bump();
NtIdent(ident, is_raw)
}
NonterminalKind::Ident => {
return Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
span: self.token.span,
token: self.token.clone(),
}));
}
NonterminalKind::Path => {
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
}
Expand Down Expand Up @@ -201,6 +196,6 @@ impl<'a> Parser<'a> {

/// The token is an identifier, but not `_`.
/// We prohibit passing `_` to macros expecting `ident` for now.
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
fn is_macro_ident(token: &Token) -> bool {
token.ident().map_or(false, |(ident, _)| ident.name != kw::Underscore)
}
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/manual_let_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:212:13
|
LL | let $n = if let Some(v) = $e { v } else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some($n) = g() else { return };`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(w) = g() else { return };`
...
LL | create_binding_if_some!(w, g());
| ------------------------------- in this macro invocation
Expand Down
5 changes: 3 additions & 2 deletions src/tools/clippy/tests/ui/needless_late_init.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ fn does_not_lint() {
}
y = 3;

let x;
inline!($x = 1;);

inline!($let x = 1;);

let x;
if true {
#[cfg(FALSE)] // FIXME
inline!($x = 1;);
} else {
x = 2;
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn does_not_lint() {

let x;
if true {
#[cfg(FALSE)] // FIXME
inline!($x = 1;);
} else {
x = 2;
Expand Down
16 changes: 15 additions & 1 deletion src/tools/clippy/tests/ui/needless_late_init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,19 @@ help: add a semicolon after the `match` expression
LL | };
| +

error: aborting due to 16 previous errors
error: unneeded late initialization
--> $DIR/needless_late_init.rs:202:5
|
LL | let x;
| ^^^^^^ created here
LL | inline!($x = 1;);
| ^^^^^^ initialised here
|
= note: this error originates in the macro `__inline_mac_fn_does_not_lint` (in Nightly builds, run with -Z macro-backtrace for more info)
help: declare `x` here
|
LL | inline!($let x = 1;);
| ~~~~~

error: aborting due to 17 previous errors

3 changes: 2 additions & 1 deletion src/tools/clippy/tests/ui/ptr_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn main() {
let _: *mut i32 = mut_ptr.cast();

// Make sure the lint is triggered inside a macro
let _ = inline!($ptr.cast::<i32>());
#[cfg(FALSE)] // FIXME
let _ = inline!($ptr as *const i32);

// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn main() {
let _: *mut i32 = mut_ptr as _;

// Make sure the lint is triggered inside a macro
#[cfg(FALSE)] // FIXME
let _ = inline!($ptr as *const i32);

// Do not lint inside macros from external crates
Expand Down
Loading

0 comments on commit b2ad6b1

Please sign in to comment.