Skip to content

Commit

Permalink
Remove NtPath.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Sep 24, 2024
1 parent de54855 commit 6675d90
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 50 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_ast/src/ast_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,12 @@ impl HasTokens for Nonterminal {
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
match self {
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
Nonterminal::NtPath(path) => path.tokens(),
Nonterminal::NtBlock(block) => block.tokens(),
}
}
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
match self {
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
Nonterminal::NtPath(path) => path.tokens_mut(),
Nonterminal::NtBlock(block) => block.tokens_mut(),
}
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,12 @@ impl MetaItem {
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
Path { span, segments, tokens: None }
}
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
token::Nonterminal::NtPath(path) => (**path).clone(),
_ => return None,
},
Some(TokenTree::Delimited(
_span,
_spacing,
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta)),
Delimiter::Invisible(InvisibleOrigin::MetaVar(
MetaVarKind::Meta | MetaVarKind::Path,
)),
_stream,
)) => {
// This path is currently unreachable in the test suite.
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 @@ -803,7 +803,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
token::NtBlock(block) => vis.visit_block(block),
token::NtExpr(expr) => vis.visit_expr(expr),
token::NtLiteral(expr) => vis.visit_expr(expr),
token::NtPath(path) => vis.visit_path(path),
}
}

Expand Down
32 changes: 8 additions & 24 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ impl Token {
matches!(&**nt,
NtBlock(..) |
NtExpr(..) |
NtLiteral(..) |
NtPath(..)
NtLiteral(..)
),
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
MetaVarKind::Block |
Expand Down Expand Up @@ -657,7 +656,6 @@ impl Token {
matches!(&**nt,
| NtExpr(..)
| NtLiteral(..)
| NtPath(..)
),
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
MetaVarKind::Expr { .. } |
Expand Down Expand Up @@ -686,7 +684,6 @@ impl Token {
Lifetime(..) | // lifetime bound in trait object
Lt | BinOp(Shl) | // associated path
PathSep => true, // global path
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
MetaVarKind::Ty |
MetaVarKind::Path
Expand Down Expand Up @@ -845,28 +842,19 @@ impl Token {
self.ident().is_some_and(|(ident, _)| ident.name == name)
}

/// Returns `true` if the token is an interpolated path.
fn is_whole_path(&self) -> bool {
if let Interpolated(nt) = &self.kind
&& let NtPath(..) = &**nt
{
return true;
}

false
}

/// Is this a pre-parsed expression dropped into the token stream
/// (which happens while parsing the result of macro expansion)?
pub fn is_whole_expr(&self) -> bool {
#[allow(irrefutable_let_patterns)] // FIXME: temporary
if let Interpolated(nt) = &self.kind
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
{
return true;
true
} else if matches!(self.is_metavar_seq(), Some(MetaVarKind::Path)) {
true
} else {
false
}

false
}

/// Is the token an interpolated block (`$b:block`)?
Expand All @@ -892,7 +880,7 @@ impl Token {
pub fn is_path_start(&self) -> bool {
self == &PathSep
|| self.is_qpath_start()
|| self.is_whole_path()
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
|| self.is_path_segment_keyword()
|| self.is_ident() && !self.is_reserved_ident()
}
Expand Down Expand Up @@ -1066,7 +1054,6 @@ pub enum Nonterminal {
NtBlock(P<ast::Block>),
NtExpr(P<ast::Expr>),
NtLiteral(P<ast::Expr>),
NtPath(P<ast::Path>),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
Expand Down Expand Up @@ -1157,7 +1144,6 @@ impl Nonterminal {
match self {
NtBlock(block) => block.span,
NtExpr(expr) | NtLiteral(expr) => expr.span,
NtPath(path) => path.span,
}
}

Expand All @@ -1166,7 +1152,6 @@ impl Nonterminal {
NtBlock(..) => "block",
NtExpr(..) => "expression",
NtLiteral(..) => "literal",
NtPath(..) => "path",
}
}
}
Expand All @@ -1187,7 +1172,6 @@ impl fmt::Debug for Nonterminal {
NtBlock(..) => f.pad("NtBlock(..)"),
NtExpr(..) => f.pad("NtExpr(..)"),
NtLiteral(..) => f.pad("NtLiteral(..)"),
NtPath(..) => f.pad("NtPath(..)"),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ impl TokenStream {
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
match nt {
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ pub(super) fn transcribe<'a>(
MatchedSingle(ParseNtResult::Meta(meta)) => {
mk_delimited(MetaVarKind::Meta, TokenStream::from_ast(meta))
}
MatchedSingle(ParseNtResult::Path(path)) => {
mk_delimited(MetaVarKind::Path, TokenStream::from_ast(path))
}
MatchedSingle(ParseNtResult::Vis(vis)) => {
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::mem;
use core::ops::ControlFlow;

use ast::mut_visit::{self, MutVisitor};
use ast::token::IdentIsRaw;
use ast::token::{IdentIsRaw, MetaVarKind};
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, MatchKind, Pat, Path, PathSegment, Recovered};
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
Expand Down Expand Up @@ -1382,24 +1382,24 @@ impl<'a> Parser<'a> {
fn parse_expr_bottom(&mut self) -> PResult<'a, P<Expr>> {
maybe_recover_from_interpolated_ty_qpath!(self, true);

let span = self.token.span;
if let token::Interpolated(nt) = &self.token.kind {
match &**nt {
token::NtExpr(e) | token::NtLiteral(e) => {
let e = e.clone();
self.bump();
return Ok(e);
}
token::NtPath(path) => {
let path = (**path).clone();
self.bump();
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Path(None, path)));
}
token::NtBlock(block) => {
let block = block.clone();
self.bump();
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
}
};
} else if let Some(path) = self.eat_metavar_seq(MetaVarKind::Path, |this| {
this.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))
}) {
return Ok(self.mk_expr(span, ExprKind::Path(None, path)));
}

// Outer attributes are already parsed and will be
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,7 @@ pub enum ParseNtResult {
Pat(P<ast::Pat>, NtPatKind),
Ty(P<ast::Ty>),
Meta(P<ast::AttrItem>),
Path(P<ast::Path>),
Vis(P<ast::Visibility>),

/// This variant will eventually be removed, along with `Token::Interpolate`.
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'a> Parser<'a> {
match nt {
NtExpr(_)
| NtLiteral(_) // `true`, `false`
| NtPath(_) => true,
=> true,

NtBlock(_) => false,
}
Expand Down Expand Up @@ -96,7 +96,6 @@ impl<'a> Parser<'a> {
token::NtLifetime(..) => true,
token::Interpolated(nt) => match &**nt {
NtBlock(_) | NtExpr(_) | NtLiteral(_) => true,
NtPath(_) => false,
},
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
MetaVarKind::Block
Expand Down Expand Up @@ -203,7 +202,9 @@ impl<'a> Parser<'a> {
};
}
NonterminalKind::Path => {
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
return Ok(ParseNtResult::Path(P(
self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?
)));
}
NonterminalKind::Meta => {
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(ForceCollect::Yes)?)));
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use tracing::debug;

use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{Parser, Restrictions, TokenType};
use crate::errors::{PathSingleColon, PathTripleColon};
use crate::errors::{self, PathSingleColon, PathTripleColon};
use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
use crate::{errors, maybe_whole};

/// Specifies how to parse a path.
#[derive(Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -196,7 +195,11 @@ impl<'a> Parser<'a> {
}
};

maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));
if let Some(path) =
self.eat_metavar_seq(MetaVarKind::Path, |this| this.parse_path(PathStyle::Type))
{
return Ok(reject_generics_if_mod_style(self, path));
}

if let Some(MetaVarKind::Ty) = self.token.is_metavar_seq() {
let mut snapshot = self.create_snapshot_for_diagnostic();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/import-prefix-macro-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod a {
}

macro_rules! import {
($p: path) => (use ::$p {S, Z}); //~ERROR expected identifier, found `a::b::c`
($p: path) => (use ::$p {S, Z}); //~ERROR expected identifier, found metavariable
}

import! { a::b::c }
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/import-prefix-macro-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected identifier, found `a::b::c`
error: expected identifier, found metavariable
--> $DIR/import-prefix-macro-2.rs:11:26
|
LL | ($p: path) => (use ::$p {S, Z});
| ^^ expected identifier
| ^^ expected identifier, found metavariable
...
LL | import! { a::b::c }
| ------------------- in this macro invocation
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/nonterminal-matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! foo {
(tt $x:tt) => { bar!(tt $x); };
(expr $x:expr) => { bar!(expr $x); }; //~ ERROR: no rules expected expression `3`
(literal $x:literal) => { bar!(literal $x); }; //~ ERROR: no rules expected literal `4`
(path $x:path) => { bar!(path $x); }; //~ ERROR: no rules expected path `a::b::c`
(path $x:path) => { bar!(path $x); }; //~ ERROR: no rules expected `path` metavariable
(stmt $x:stmt) => { bar!(stmt $x); }; //~ ERROR: no rules expected `stmt` metavariable
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/nonterminal-matching.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ LL | (literal 4) => {};
= help: try using `:tt` instead in the macro definition
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: no rules expected path `a::b::c`
error: no rules expected `path` metavariable
--> $DIR/nonterminal-matching.rs:34:35
|
LL | (path $x:path) => { bar!(path $x); };
Expand Down

0 comments on commit 6675d90

Please sign in to comment.