Skip to content

Commit d9aa80b

Browse files
committed
macro_rules: Remove NtIdent token kind
1 parent 19a1d2b commit d9aa80b

File tree

55 files changed

+269
-267
lines changed

Some content is hidden

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

55 files changed

+269
-267
lines changed

compiler/rustc_ast/src/mut_visit.rs

-3
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,6 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
766766
*span = ident.span;
767767
return; // Avoid visiting the span for the second time.
768768
}
769-
token::NtIdent(ident, _is_raw) => {
770-
vis.visit_ident(ident);
771-
}
772769
token::NtLifetime(ident) => {
773770
vis.visit_ident(ident);
774771
}

compiler/rustc_ast/src/token.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,7 @@ pub enum TokenKind {
317317
Literal(Lit),
318318

319319
/// Identifier token.
320-
/// Do not forget about `NtIdent` when you want to match on identifiers.
321-
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
322-
/// treat regular and interpolated identifiers in the same way.
323320
Ident(Symbol, IdentIsRaw),
324-
/// This identifier (and its span) is the identifier passed to the
325-
/// declarative macro. The span in the surrounding `Token` is the span of
326-
/// the `ident` metavariable in the macro's RHS.
327-
NtIdent(Ident, IdentIsRaw),
328321

329322
/// Lifetime identifier token.
330323
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
@@ -457,7 +450,7 @@ impl Token {
457450
/// if they keep spans or perform edition checks.
458451
pub fn uninterpolated_span(&self) -> Span {
459452
match self.kind {
460-
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
453+
NtLifetime(ident) => ident.span,
461454
Interpolated(ref nt) => nt.use_span(),
462455
_ => self.span,
463456
}
@@ -476,7 +469,7 @@ impl Token {
476469
}
477470

478471
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
479-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
472+
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
480473
}
481474
}
482475

@@ -644,7 +637,6 @@ impl Token {
644637
/// otherwise returns the original token.
645638
pub fn uninterpolate(&self) -> Cow<'_, Token> {
646639
match self.kind {
647-
NtIdent(ident, is_raw) => Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span)),
648640
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
649641
_ => Cow::Borrowed(self),
650642
}
@@ -656,7 +648,6 @@ impl Token {
656648
// We avoid using `Token::uninterpolate` here because it's slow.
657649
match self.kind {
658650
Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
659-
NtIdent(ident, is_raw) => Some((ident, is_raw)),
660651
_ => None,
661652
}
662653
}
@@ -856,7 +847,7 @@ impl Token {
856847

857848
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
858849
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
859-
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
850+
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..)
860851
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
861852
return None;
862853
}

compiler/rustc_ast/src/tokenstream.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,6 @@ impl TokenStream {
484484

485485
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
486486
match token.kind {
487-
token::NtIdent(ident, is_raw) => {
488-
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
489-
}
490487
token::NtLifetime(ident) => TokenTree::Delimited(
491488
DelimSpan::from_single(token.span),
492489
DelimSpacing::new(Spacing::JointHidden, spacing),
@@ -516,10 +513,9 @@ impl TokenStream {
516513
pub fn flattened(&self) -> TokenStream {
517514
fn can_skip(stream: &TokenStream) -> bool {
518515
stream.trees().all(|tree| match tree {
519-
TokenTree::Token(token, _) => !matches!(
520-
token.kind,
521-
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
522-
),
516+
TokenTree::Token(token, _) => {
517+
!matches!(token.kind, token::NtLifetime(..) | token::Interpolated(..))
518+
}
523519
TokenTree::Delimited(.., inner) => can_skip(inner),
524520
})
525521
}

compiler/rustc_ast_pretty/src/pprust/state.rs

-3
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
943943
token::Ident(name, is_raw) => {
944944
IdentPrinter::new(name, is_raw.into(), convert_dollar_crate).to_string().into()
945945
}
946-
token::NtIdent(ident, is_raw) => {
947-
IdentPrinter::for_ast_ident(ident, is_raw.into()).to_string().into()
948-
}
949946
token::Lifetime(name) => name.to_string().into(),
950947
token::NtLifetime(ident) => ident.name.to_string().into(),
951948

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
743743
// or
744744
// `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`
745745
let def_id = self.body.source.def_id();
746-
if let Some(local_def_id) = def_id.as_local()
747-
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
748-
&& let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(&body).break_value()
746+
let hir_id = def_id
747+
.as_local()
748+
.and_then(|def_id| self.infcx.tcx.hir().maybe_body_owned_by(def_id))
749+
.and_then(|body| (BindingFinder { span: pat_span }).visit_body(&body).break_value());
750+
if let Some(hir_id) = hir_id
749751
&& let node = self.infcx.tcx.hir_node(hir_id)
750752
&& let hir::Node::LetStmt(hir::LetStmt {
751753
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
@@ -767,8 +769,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
767769
return;
768770
}
769771

772+
let span = local_decl.source_info.span;
773+
let span =
774+
hir_id.map_or(span, |hir_id| span.with_neighbor(self.infcx.tcx.hir().span(hir_id)));
770775
err.span_suggestion_verbose(
771-
local_decl.source_info.span.shrink_to_lo(),
776+
span.shrink_to_lo(),
772777
"consider changing this to be mutable",
773778
"mut ",
774779
Applicability::MachineApplicable,

compiler/rustc_expand/src/config.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,7 @@ impl<'a> StripUnconfigured<'a> {
203203
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner)).into_iter()
204204
}
205205
AttrTokenTree::Token(
206-
Token {
207-
kind:
208-
TokenKind::NtIdent(..)
209-
| TokenKind::NtLifetime(..)
210-
| TokenKind::Interpolated(..),
211-
..
212-
},
206+
Token { kind: TokenKind::NtLifetime(..) | TokenKind::Interpolated(..), .. },
213207
_,
214208
) => {
215209
panic!("Nonterminal should have been flattened: {:?}", tree);

compiler/rustc_expand/src/mbe/transcribe.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,6 @@ pub(super) fn transcribe<'a>(
274274
// without wrapping them into groups.
275275
maybe_use_metavar_location(psess, &stack, sp, tt, &mut marker)
276276
}
277-
MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
278-
marker.visit_span(&mut sp);
279-
let kind = token::NtIdent(*ident, *is_raw);
280-
TokenTree::token_alone(kind, sp)
281-
}
282277
MatchedSingle(ParseNtResult::Lifetime(ident)) => {
283278
marker.visit_span(&mut sp);
284279
let kind = token::NtLifetime(*ident);
@@ -395,9 +390,22 @@ fn maybe_use_metavar_location(
395390
return orig_tt.clone();
396391
}
397392

398-
let insert = |mspans: &mut FxHashMap<_, _>, s, ms| match mspans.try_insert(s, ms) {
393+
let insert = |mspans: &mut FxHashMap<_, _>, s, ms: Span| match mspans.try_insert(s, ms) {
399394
Ok(_) => true,
400-
Err(err) => *err.entry.get() == ms, // Tried to insert the same span, still success
395+
Err(mut err) => {
396+
let old_ms = *err.entry.get();
397+
if ms == old_ms {
398+
// Tried to insert the same span, still success.
399+
return true;
400+
}
401+
if !ms.eq_ctxt(old_ms) && ms.ctxt().outer_expn_data().call_site.eq_ctxt(old_ms) {
402+
// This looks like a variable passed to an inner (possibly recursive) macro call.
403+
// The innermost metavar span is the most useful, so override.
404+
err.entry.insert(ms);
405+
return true;
406+
}
407+
false
408+
}
401409
};
402410
marker.visit_span(&mut metavar_span);
403411
let no_collision = match orig_tt {
@@ -411,16 +419,23 @@ fn maybe_use_metavar_location(
411419
}),
412420
};
413421
if no_collision || psess.source_map().is_imported(metavar_span) {
422+
// Add a whitespace for backward compatibility.
423+
// FIXME: assign spacing to tokens from metavars in a more systematic way (#127123).
424+
if let TokenTree::Token(token, _) = orig_tt {
425+
return TokenTree::Token(token.clone(), Spacing::Alone);
426+
}
414427
return orig_tt.clone();
415428
}
416429

417430
// Setting metavar spans for the heuristic spans gives better opportunities for combining them
418431
// with neighboring spans even despite their different syntactic contexts.
419432
match orig_tt {
420-
TokenTree::Token(Token { kind, span }, spacing) => {
433+
TokenTree::Token(Token { kind, span }, _spacing) => {
421434
let span = metavar_span.with_ctxt(span.ctxt());
422435
with_metavar_spans(|mspans| insert(mspans, span, metavar_span));
423-
TokenTree::Token(Token { kind: kind.clone(), span }, *spacing)
436+
// Add a whitespace for backward compatibility.
437+
// FIXME: assign spacing to tokens from metavars in a more systematic way (#127123).
438+
TokenTree::Token(Token { kind: kind.clone(), span }, Spacing::Alone)
424439
}
425440
TokenTree::Delimited(dspan, dspacing, delimiter, tts) => {
426441
let open = metavar_span.with_ctxt(dspan.open.ctxt());
@@ -735,12 +750,6 @@ fn extract_ident<'a>(
735750
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
736751
) -> PResult<'a, String> {
737752
if let NamedMatch::MatchedSingle(pnr) = matched_from_ident(dcx, ident, interp)? {
738-
if let ParseNtResult::Ident(nt_ident, is_raw) = pnr {
739-
if let IdentIsRaw::Yes = is_raw {
740-
return Err(dcx.struct_span_err(ident.span, RAW_IDENT_ERR));
741-
}
742-
return Ok(nt_ident.to_string());
743-
}
744753
if let ParseNtResult::Tt(TokenTree::Token(
745754
Token { kind: TokenKind::Ident(token_ident, is_raw), .. },
746755
_,

compiler/rustc_expand/src/proc_macro_server.rs

-5
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
221221
Ident(sym, is_raw) => {
222222
trees.push(TokenTree::Ident(Ident { sym, is_raw: is_raw.into(), span }))
223223
}
224-
NtIdent(ident, is_raw) => trees.push(TokenTree::Ident(Ident {
225-
sym: ident.name,
226-
is_raw: is_raw.into(),
227-
span: ident.span,
228-
})),
229224

230225
Lifetime(name) => {
231226
let ident = symbol::Ident::new(name, span).without_first_quote();

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5656
));
5757
}
5858

59-
if self_ty.span.edition().at_least_rust_2021() {
59+
if tcx.hir().span_in_context(self_ty.hir_id).edition().at_least_rust_2021() {
6060
let msg = "trait objects must include the `dyn` keyword";
6161
let label = "add `dyn` keyword before this trait";
6262
let mut diag =

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24542454
ExprKind::Path(QPath::Resolved(_, path)) => {
24552455
// local binding
24562456
if let hir::def::Res::Local(hir_id) = path.res {
2457-
let span = tcx.hir().span(hir_id);
2457+
let span = tcx.hir().span_in_context(hir_id);
24582458
let filename = tcx.sess.source_map().span_to_filename(span);
24592459

24602460
let parent_node = self.tcx.parent_hir_node(hir_id);

compiler/rustc_lint/src/types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,9 @@ fn lint_wide_pointer<'tcx>(
718718
return;
719719
};
720720

721+
let (l_span, r_span) = (l.span.with_neighbor(e.span), r.span.with_neighbor(e.span));
721722
let (Some(l_span), Some(r_span)) =
722-
(l.span.find_ancestor_inside(e.span), r.span.find_ancestor_inside(e.span))
723+
(l_span.find_ancestor_inside(e.span), r_span.find_ancestor_inside(e.span))
723724
else {
724725
return cx.emit_span_lint(
725726
AMBIGUOUS_WIDE_POINTER_COMPARISONS,

compiler/rustc_middle/src/hir/map/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,16 @@ impl<'hir> Map<'hir> {
935935
}
936936
}
937937

938+
/// Use regular `span` method when *showing* any diagnostics.
939+
/// Use this method when suggesting *inserting* any code to the left or right from this node.
940+
///
941+
/// If some piece of code is passed to a macro as an argument, then this method may move the
942+
/// span from the argument into the macro body where that argument is emitted into the output.
943+
pub fn span_in_context(self, hir_id: HirId) -> Span {
944+
let parent_span = self.span(self.tcx.parent_hir_id(hir_id));
945+
self.span(hir_id).with_neighbor(parent_span)
946+
}
947+
938948
/// Get a representation of this `id` for debugging purposes.
939949
/// NOTE: Do NOT use this in diagnostics!
940950
pub fn node_to_string(self, id: HirId) -> String {

compiler/rustc_parse/src/parser/attr_wrapper.rs

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

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

compiler/rustc_parse/src/parser/expr.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ impl<'a> Parser<'a> {
249249
continue;
250250
}
251251

252+
let op_span = op.span;
252253
let op = op.node;
253254
// Special cases:
254255
if op == AssocOp::As {
255-
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
256+
lhs = self.parse_assoc_op_cast(lhs, lhs_span, op_span, ExprKind::Cast)?;
256257
continue;
257258
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
258259
// If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
@@ -274,7 +275,7 @@ impl<'a> Parser<'a> {
274275
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::Unparsed { attrs })
275276
})?;
276277

277-
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
278+
let span = self.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span);
278279
lhs = match op {
279280
AssocOp::Add
280281
| AssocOp::Subtract
@@ -453,7 +454,7 @@ impl<'a> Parser<'a> {
453454
None
454455
};
455456
let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
456-
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
457+
let span = self.mk_expr_sp(&lhs, lhs.span, cur_op_span, rhs_span);
457458
let limits =
458459
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
459460
let range = self.mk_range(Some(lhs), rhs, limits);
@@ -663,9 +664,7 @@ impl<'a> Parser<'a> {
663664
/// Returns the span of expr if it was not interpolated, or the span of the interpolated token.
664665
fn interpolated_or_expr_span(&self, expr: &Expr) -> Span {
665666
match self.prev_token.kind {
666-
TokenKind::NtIdent(..) | TokenKind::NtLifetime(..) | TokenKind::Interpolated(..) => {
667-
self.prev_token.span
668-
}
667+
TokenKind::NtLifetime(..) | TokenKind::Interpolated(..) => self.prev_token.span,
669668
_ => expr.span,
670669
}
671670
}
@@ -674,10 +673,11 @@ impl<'a> Parser<'a> {
674673
&mut self,
675674
lhs: P<Expr>,
676675
lhs_span: Span,
676+
op_span: Span,
677677
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
678678
) -> PResult<'a, P<Expr>> {
679679
let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
680-
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs))
680+
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span), expr_kind(lhs, rhs))
681681
};
682682

683683
// Save the state of the parser before parsing type normally, in case there is a
@@ -3852,11 +3852,13 @@ impl<'a> Parser<'a> {
38523852

38533853
/// Create expression span ensuring the span of the parent node
38543854
/// is larger than the span of lhs and rhs, including the attributes.
3855-
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
3855+
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {
38563856
lhs.attrs
38573857
.iter()
38583858
.find(|a| a.style == AttrStyle::Outer)
38593859
.map_or(lhs_span, |a| a.span)
3860+
// An approximation to #126763.
3861+
.to(op_span)
38603862
.to(rhs_span)
38613863
}
38623864

compiler/rustc_parse/src/parser/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ pub(super) fn token_descr(token: &Token) -> String {
394394
(Some(TokenDescription::Keyword), _) => Some("keyword"),
395395
(Some(TokenDescription::ReservedKeyword), _) => Some("reserved keyword"),
396396
(Some(TokenDescription::DocComment), _) => Some("doc comment"),
397-
(None, TokenKind::NtIdent(..)) => Some("identifier"),
398397
(None, TokenKind::NtLifetime(..)) => Some("lifetime"),
399398
(None, TokenKind::Interpolated(node)) => Some(node.descr()),
400399
(None, _) => None,
@@ -687,9 +686,9 @@ impl<'a> Parser<'a> {
687686
self.is_keyword_ahead(0, &[kw::Const])
688687
&& self.look_ahead(1, |t| match &t.kind {
689688
// async closures do not work with const closures, so we do not parse that here.
690-
token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {
691-
true
692-
}
689+
token::Ident(kw::Move | kw::Static, IdentIsRaw::No)
690+
| token::OrOr
691+
| token::BinOp(token::Or) => true,
693692
_ => false,
694693
})
695694
}
@@ -1623,7 +1622,6 @@ enum FlatToken {
16231622
#[derive(Clone, Debug)]
16241623
pub enum ParseNtResult {
16251624
Tt(TokenTree),
1626-
Ident(Ident, IdentIsRaw),
16271625
Lifetime(Ident),
16281626

16291627
/// This case will eventually be removed, along with `Token::Interpolate`.

0 commit comments

Comments
 (0)