Skip to content

Commit b2ad6b1

Browse files
committed
Auto merge of rust-lang#119412 - petrochenkov:dialoc3, r=<try>
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.
2 parents bea5beb + 8bbdaaa commit b2ad6b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+199
-295
lines changed

compiler/rustc_ast/src/ast_traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl HasTokens for Nonterminal {
240240
Nonterminal::NtPath(path) => path.tokens(),
241241
Nonterminal::NtVis(vis) => vis.tokens(),
242242
Nonterminal::NtBlock(block) => block.tokens(),
243-
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
243+
Nonterminal::NtLifetime(..) => None,
244244
}
245245
}
246246
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
@@ -254,7 +254,7 @@ impl HasTokens for Nonterminal {
254254
Nonterminal::NtPath(path) => path.tokens_mut(),
255255
Nonterminal::NtVis(vis) => vis.tokens_mut(),
256256
Nonterminal::NtBlock(block) => block.tokens_mut(),
257-
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
257+
Nonterminal::NtLifetime(..) => None,
258258
}
259259
}
260260
}

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
822822
token::NtPat(pat) => vis.visit_pat(pat),
823823
token::NtExpr(expr) => vis.visit_expr(expr),
824824
token::NtTy(ty) => vis.visit_ty(ty),
825-
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
826825
token::NtLifetime(ident) => vis.visit_ident(ident),
827826
token::NtLiteral(expr) => vis.visit_expr(expr),
828827
token::NtMeta(item) => {

compiler/rustc_ast/src/token.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,6 @@ pub enum TokenKind {
295295
Literal(Lit),
296296

297297
/// Identifier token.
298-
/// Do not forget about `NtIdent` when you want to match on identifiers.
299-
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
300-
/// treat regular and interpolated identifiers in the same way.
301298
Ident(Symbol, /* is_raw */ bool),
302299
/// Lifetime identifier token.
303300
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
@@ -590,9 +587,6 @@ impl Token {
590587
pub fn uninterpolate(&self) -> Cow<'_, Token> {
591588
match &self.kind {
592589
Interpolated(nt) => match &nt.0 {
593-
NtIdent(ident, is_raw) => {
594-
Cow::Owned(Token::new(Ident(ident.name, *is_raw), ident.span))
595-
}
596590
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
597591
_ => Cow::Borrowed(self),
598592
},
@@ -606,10 +600,6 @@ impl Token {
606600
// We avoid using `Token::uninterpolate` here because it's slow.
607601
match &self.kind {
608602
&Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
609-
Interpolated(nt) => match &nt.0 {
610-
NtIdent(ident, is_raw) => Some((*ident, *is_raw)),
611-
_ => None,
612-
},
613603
_ => None,
614604
}
615605
}
@@ -836,7 +826,6 @@ pub enum Nonterminal {
836826
NtPat(P<ast::Pat>),
837827
NtExpr(P<ast::Expr>),
838828
NtTy(P<ast::Ty>),
839-
NtIdent(Ident, /* is_raw */ bool),
840829
NtLifetime(Ident),
841830
NtLiteral(P<ast::Expr>),
842831
/// Stuff inside brackets for attributes
@@ -932,7 +921,7 @@ impl Nonterminal {
932921
NtPat(pat) => pat.span,
933922
NtExpr(expr) | NtLiteral(expr) => expr.span,
934923
NtTy(ty) => ty.span,
935-
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
924+
NtLifetime(ident) => ident.span,
936925
NtMeta(attr_item) => attr_item.span(),
937926
NtPath(path) => path.span,
938927
NtVis(vis) => vis.span,
@@ -948,7 +937,6 @@ impl Nonterminal {
948937
NtExpr(..) => "expression",
949938
NtLiteral(..) => "literal",
950939
NtTy(..) => "type",
951-
NtIdent(..) => "identifier",
952940
NtLifetime(..) => "lifetime",
953941
NtMeta(..) => "attribute",
954942
NtPath(..) => "path",
@@ -960,9 +948,6 @@ impl Nonterminal {
960948
impl PartialEq for Nonterminal {
961949
fn eq(&self, rhs: &Self) -> bool {
962950
match (self, rhs) {
963-
(NtIdent(ident_lhs, is_raw_lhs), NtIdent(ident_rhs, is_raw_rhs)) => {
964-
ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs
965-
}
966951
(NtLifetime(ident_lhs), NtLifetime(ident_rhs)) => ident_lhs == ident_rhs,
967952
// FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
968953
// correctly based on data from AST. This will prevent them from matching each other
@@ -982,7 +967,6 @@ impl fmt::Debug for Nonterminal {
982967
NtPat(..) => f.pad("NtPat(..)"),
983968
NtExpr(..) => f.pad("NtExpr(..)"),
984969
NtTy(..) => f.pad("NtTy(..)"),
985-
NtIdent(..) => f.pad("NtIdent(..)"),
986970
NtLiteral(..) => f.pad("NtLiteral(..)"),
987971
NtMeta(..) => f.pad("NtMeta(..)"),
988972
NtPath(..) => f.pad("NtPath(..)"),

compiler/rustc_ast/src/tokenstream.rs

-6
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,6 @@ impl TokenStream {
478478

479479
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
480480
match nt {
481-
Nonterminal::NtIdent(ident, is_raw) => {
482-
TokenStream::token_alone(token::Ident(ident.name, *is_raw), ident.span)
483-
}
484481
Nonterminal::NtLifetime(ident) => {
485482
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
486483
}
@@ -502,9 +499,6 @@ impl TokenStream {
502499

503500
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
504501
match &token.kind {
505-
token::Interpolated(nt) if let token::NtIdent(ident, is_raw) = nt.0 => {
506-
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
507-
}
508502
token::Interpolated(nt) => TokenTree::Delimited(
509503
DelimSpan::from_single(token.span),
510504
DelimSpacing::new(Spacing::JointHidden, spacing),

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
731731
token::NtBlock(e) => self.block_to_string(e),
732732
token::NtStmt(e) => self.stmt_to_string(e),
733733
token::NtPat(e) => self.pat_to_string(e),
734-
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
735734
token::NtLifetime(e) => e.to_string(),
736735
token::NtLiteral(e) => self.expr_to_string(e),
737736
token::NtVis(e) => self.vis_to_string(e),

compiler/rustc_expand/src/proc_macro_server.rs

-8
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
256256
}));
257257
}
258258

259-
Interpolated(ref nt) if let NtIdent(ident, is_raw) = &nt.0 => {
260-
trees.push(TokenTree::Ident(Ident {
261-
sym: ident.name,
262-
is_raw: *is_raw,
263-
span: ident.span,
264-
}))
265-
}
266-
267259
Interpolated(nt) => {
268260
let stream = TokenStream::from_nonterminal_ast(&nt.0);
269261
// A hack used to pass AST fragments to attribute and derive

compiler/rustc_parse/src/parser/attr_wrapper.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ impl<'a> Parser<'a> {
226226
let (mut ret, trailing) = ret?;
227227

228228
// When we're not in `capture-cfg` mode, then bail out early if:
229-
// 1. Our target doesn't support tokens at all (e.g we're parsing an `NtIdent`)
230-
// so there's nothing for us to do.
229+
// 1. Our target doesn't support tokens at all, so there's nothing for us to do.
231230
// 2. Our target already has tokens set (e.g. we've parsed something
232231
// like `#[my_attr] $item`. The actual parsing code takes care of prepending
233232
// any attributes to the nonterminal, so we don't need to modify the

compiler/rustc_parse/src/parser/nonterminal.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::token::{self, Delimiter, Nonterminal::*, NonterminalKind, Token};
33
use rustc_ast::HasTokens;
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::PResult;
6-
use rustc_span::symbol::{kw, Ident};
6+
use rustc_span::symbol::kw;
77

88
use crate::errors::UnexpectedNonterminal;
99
use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
@@ -24,7 +24,6 @@ impl<'a> Parser<'a> {
2424
| NtPat(_)
2525
| NtExpr(_)
2626
| NtTy(_)
27-
| NtIdent(..)
2827
| NtLiteral(_) // `true`, `false`
2928
| NtMeta(_)
3029
| NtPath(_) => true,
@@ -45,7 +44,7 @@ impl<'a> Parser<'a> {
4544
&& !token.is_keyword(kw::Const)
4645
}
4746
NonterminalKind::Ty => token.can_begin_type(),
48-
NonterminalKind::Ident => get_macro_ident(token).is_some(),
47+
NonterminalKind::Ident => is_macro_ident(token),
4948
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
5049
NonterminalKind::Vis => match token.kind {
5150
// The follow-set of :vis + "priv" keyword + interpolated
@@ -56,8 +55,7 @@ impl<'a> Parser<'a> {
5655
token::OpenDelim(Delimiter::Brace) => true,
5756
token::Interpolated(nt) => match &nt.0 {
5857
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
59-
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_)
60-
| NtVis(_) => false,
58+
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) | NtVis(_) => false,
6159
},
6260
_ => false,
6361
},
@@ -108,8 +106,17 @@ impl<'a> Parser<'a> {
108106
// in advance whether or not a proc-macro will be (transitively) invoked,
109107
// we always capture tokens for any `Nonterminal` which needs them.
110108
let mut nt = match kind {
111-
// Note that TT is treated differently to all the others.
109+
// Note that `tt` and `ident` are treated differently to all the others.
112110
NonterminalKind::TT => return Ok(ParseNtResult::Tt(self.parse_token_tree())),
111+
NonterminalKind::Ident if is_macro_ident(&self.token) => {
112+
return Ok(ParseNtResult::Tt(self.parse_token_tree()));
113+
}
114+
NonterminalKind::Ident => {
115+
return Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
116+
span: self.token.span,
117+
token: self.token.clone(),
118+
}));
119+
}
113120
NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
114121
Some(item) => NtItem(item),
115122
None => {
@@ -153,18 +160,6 @@ impl<'a> Parser<'a> {
153160
NonterminalKind::Ty => {
154161
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
155162
}
156-
157-
// this could be handled like a token, since it is one
158-
NonterminalKind::Ident if let Some((ident, is_raw)) = get_macro_ident(&self.token) => {
159-
self.bump();
160-
NtIdent(ident, is_raw)
161-
}
162-
NonterminalKind::Ident => {
163-
return Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
164-
span: self.token.span,
165-
token: self.token.clone(),
166-
}));
167-
}
168163
NonterminalKind::Path => {
169164
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
170165
}
@@ -201,6 +196,6 @@ impl<'a> Parser<'a> {
201196

202197
/// The token is an identifier, but not `_`.
203198
/// We prohibit passing `_` to macros expecting `ident` for now.
204-
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
205-
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
199+
fn is_macro_ident(token: &Token) -> bool {
200+
token.ident().map_or(false, |(ident, _)| ident.name != kw::Underscore)
206201
}

src/tools/clippy/tests/ui/manual_let_else.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ error: this could be rewritten as `let...else`
367367
--> $DIR/manual_let_else.rs:212:13
368368
|
369369
LL | let $n = if let Some(v) = $e { v } else { return };
370-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some($n) = g() else { return };`
370+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(w) = g() else { return };`
371371
...
372372
LL | create_binding_if_some!(w, g());
373373
| ------------------------------- in this macro invocation

src/tools/clippy/tests/ui/needless_late_init.fixed

+3-2
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ fn does_not_lint() {
199199
}
200200
y = 3;
201201

202-
let x;
203-
inline!($x = 1;);
202+
203+
inline!($let x = 1;);
204204

205205
let x;
206206
if true {
207+
#[cfg(FALSE)] // FIXME
207208
inline!($x = 1;);
208209
} else {
209210
x = 2;

src/tools/clippy/tests/ui/needless_late_init.rs

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ fn does_not_lint() {
204204

205205
let x;
206206
if true {
207+
#[cfg(FALSE)] // FIXME
207208
inline!($x = 1;);
208209
} else {
209210
x = 2;

src/tools/clippy/tests/ui/needless_late_init.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,19 @@ help: add a semicolon after the `match` expression
271271
LL | };
272272
| +
273273

274-
error: aborting due to 16 previous errors
274+
error: unneeded late initialization
275+
--> $DIR/needless_late_init.rs:202:5
276+
|
277+
LL | let x;
278+
| ^^^^^^ created here
279+
LL | inline!($x = 1;);
280+
| ^^^^^^ initialised here
281+
|
282+
= note: this error originates in the macro `__inline_mac_fn_does_not_lint` (in Nightly builds, run with -Z macro-backtrace for more info)
283+
help: declare `x` here
284+
|
285+
LL | inline!($let x = 1;);
286+
| ~~~~~
287+
288+
error: aborting due to 17 previous errors
275289

src/tools/clippy/tests/ui/ptr_as_ptr.fixed

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ fn main() {
4747
let _: *mut i32 = mut_ptr.cast();
4848

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

5253
// Do not lint inside macros from external crates
5354
let _ = external!($ptr as *const i32);

src/tools/clippy/tests/ui/ptr_as_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn main() {
4747
let _: *mut i32 = mut_ptr as _;
4848

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

5253
// Do not lint inside macros from external crates

0 commit comments

Comments
 (0)