Skip to content

Commit 180627e

Browse files
committed
Auto merge of rust-lang#124141 - nnethercote:rm-Nonterminal-and-TokenKind-Interpolated, r=<try>
Remove `Nonterminal` and `TokenKind::Interpolated` A third attempt at this; the first attempt was rust-lang#96724 and the second was rust-lang#114647. r? `@ghost`
2 parents d4cc01c + c745726 commit 180627e

File tree

102 files changed

+1623
-1401
lines changed

Some content is hidden

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

102 files changed

+1623
-1401
lines changed

compiler/rustc_ast/src/ast_traits.rs

-30
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! The traits are not implemented exhaustively, only when actually necessary.
44
55
use crate::ptr::P;
6-
use crate::token::Nonterminal;
76
use crate::tokenstream::LazyAttrTokenStream;
87
use crate::{Arm, Crate, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
98
use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
@@ -228,35 +227,6 @@ impl HasTokens for Attribute {
228227
}
229228
}
230229

231-
impl HasTokens for Nonterminal {
232-
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233-
match self {
234-
Nonterminal::NtItem(item) => item.tokens(),
235-
Nonterminal::NtStmt(stmt) => stmt.tokens(),
236-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
237-
Nonterminal::NtPat(pat) => pat.tokens(),
238-
Nonterminal::NtTy(ty) => ty.tokens(),
239-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
240-
Nonterminal::NtPath(path) => path.tokens(),
241-
Nonterminal::NtVis(vis) => vis.tokens(),
242-
Nonterminal::NtBlock(block) => block.tokens(),
243-
}
244-
}
245-
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
246-
match self {
247-
Nonterminal::NtItem(item) => item.tokens_mut(),
248-
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
249-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
250-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
251-
Nonterminal::NtTy(ty) => ty.tokens_mut(),
252-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
253-
Nonterminal::NtPath(path) => path.tokens_mut(),
254-
Nonterminal::NtVis(vis) => vis.tokens_mut(),
255-
Nonterminal::NtBlock(block) => block.tokens_mut(),
256-
}
257-
}
258-
}
259-
260230
/// A trait for AST nodes having (or not having) attributes.
261231
pub trait HasAttrs {
262232
/// This is `true` if this `HasAttrs` might support 'custom' (proc-macro) inner

compiler/rustc_ast/src/attr/mod.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
77
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
88
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
99
use crate::ptr::P;
10-
use crate::token::{self, CommentKind, Delimiter, Token};
10+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};
1111
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1212
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1313
use crate::util::comments;
@@ -327,7 +327,8 @@ impl MetaItem {
327327
I: Iterator<Item = &'a TokenTree>,
328328
{
329329
// FIXME: Share code with `parse_path`.
330-
let path = match tokens.next().map(|tt| TokenTree::uninterpolate(tt)).as_deref() {
330+
let tt = tokens.next().map(|tt| TokenTree::uninterpolate(tt));
331+
let path = match tt.as_deref() {
331332
Some(&TokenTree::Token(
332333
Token { kind: ref kind @ (token::Ident(..) | token::PathSep), span },
333334
_,
@@ -363,11 +364,23 @@ impl MetaItem {
363364
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
364365
Path { span, segments, tokens: None }
365366
}
366-
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
367-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
368-
token::Nonterminal::NtPath(path) => (**path).clone(),
369-
_ => return None,
370-
},
367+
Some(TokenTree::Delimited(
368+
_span,
369+
_spacing,
370+
Delimiter::Invisible(InvisibleOrigin::MetaVar(
371+
MetaVarKind::Meta | MetaVarKind::Path,
372+
)),
373+
_stream,
374+
)) => {
375+
// This path is currently unreachable in the test suite.
376+
unreachable!()
377+
}
378+
Some(TokenTree::Token(
379+
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
380+
_,
381+
)) => {
382+
panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tt);
383+
}
371384
_ => return None,
372385
};
373386
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
@@ -404,7 +417,7 @@ impl MetaItemKind {
404417
tokens: &mut impl Iterator<Item = &'a TokenTree>,
405418
) -> Option<MetaItemKind> {
406419
match tokens.next() {
407-
Some(TokenTree::Delimited(.., Delimiter::Invisible, inner_tokens)) => {
420+
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
408421
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
409422
}
410423
Some(TokenTree::Token(token, _)) => {
@@ -542,7 +555,7 @@ impl NestedMetaItem {
542555
tokens.next();
543556
return Some(NestedMetaItem::Lit(lit));
544557
}
545-
Some(TokenTree::Delimited(.., Delimiter::Invisible, inner_tokens)) => {
558+
Some(TokenTree::Delimited(.., Delimiter::Invisible(_), inner_tokens)) => {
546559
tokens.next();
547560
return NestedMetaItem::from_tokens(&mut inner_tokens.trees().peekable());
548561
}

compiler/rustc_ast/src/mut_visit.rs

+3-61
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,9 @@ fn visit_lazy_tts<T: MutVisitor>(lazy_tts: &mut Option<LazyAttrTokenStream>, vis
764764
visit_lazy_tts_opt_mut(lazy_tts.as_mut(), vis);
765765
}
766766

767-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
768-
/// In practice the ident part is not actually used by specific visitors right now,
769-
/// but there's a test below checking that it works.
767+
/// Applies ident visitor if it's an ident. In practice this is not actually
768+
/// used by specific visitors right now, but there's a test below checking that
769+
/// it works.
770770
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
771771
pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
772772
let Token { kind, span } = t;
@@ -784,69 +784,11 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
784784
token::NtLifetime(ident) => {
785785
vis.visit_ident(ident);
786786
}
787-
token::Interpolated(nt) => {
788-
let nt = Lrc::make_mut(nt);
789-
visit_nonterminal(nt, vis);
790-
}
791787
_ => {}
792788
}
793789
vis.visit_span(span);
794790
}
795791

796-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
797-
/// Applies the visitor to elements of interpolated nodes.
798-
//
799-
// N.B., this can occur only when applying a visitor to partially expanded
800-
// code, where parsed pieces have gotten implanted ito *other* macro
801-
// invocations. This is relevant for macro hygiene, but possibly not elsewhere.
802-
//
803-
// One problem here occurs because the types for flat_map_item, flat_map_stmt,
804-
// etc., allow the visitor to return *multiple* items; this is a problem for the
805-
// nodes here, because they insist on having exactly one piece. One solution
806-
// would be to mangle the MutVisitor trait to include one-to-many and
807-
// one-to-one versions of these entry points, but that would probably confuse a
808-
// lot of people and help very few. Instead, I'm just going to put in dynamic
809-
// checks. I think the performance impact of this will be pretty much
810-
// nonexistent. The danger is that someone will apply a `MutVisitor` to a
811-
// partially expanded node, and will be confused by the fact that their
812-
// `flat_map_item` or `flat_map_stmt` isn't getting called on `NtItem` or `NtStmt`
813-
// nodes. Hopefully they'll wind up reading this comment, and doing something
814-
// appropriate.
815-
//
816-
// BTW, design choice: I considered just changing the type of, e.g., `NtItem` to
817-
// contain multiple items, but decided against it when I looked at
818-
// `parse_item_or_view_item` and tried to figure out what I would do with
819-
// multiple items there....
820-
fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
821-
match nt {
822-
token::NtItem(item) => visit_clobber(item, |item| {
823-
// This is probably okay, because the only visitors likely to
824-
// peek inside interpolated nodes will be renamings/markings,
825-
// which map single items to single items.
826-
vis.flat_map_item(item).expect_one("expected visitor to produce exactly one item")
827-
}),
828-
token::NtBlock(block) => vis.visit_block(block),
829-
token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
830-
// See reasoning above.
831-
stmt.map(|stmt| {
832-
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
833-
})
834-
}),
835-
token::NtPat(pat) => vis.visit_pat(pat),
836-
token::NtExpr(expr) => vis.visit_expr(expr),
837-
token::NtTy(ty) => vis.visit_ty(ty),
838-
token::NtLiteral(expr) => vis.visit_expr(expr),
839-
token::NtMeta(item) => {
840-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
841-
vis.visit_path(path);
842-
visit_attr_args(args, vis);
843-
visit_lazy_tts(tokens, vis);
844-
}
845-
token::NtPath(path) => vis.visit_path(path),
846-
token::NtVis(visib) => vis.visit_vis(visib),
847-
}
848-
}
849-
850792
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
851793
fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
852794
match defaultness {

0 commit comments

Comments
 (0)