summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_ast/src/token.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
commit018c4950b9406055dec02ef0fb52f132e2bb1e2c (patch)
treea835ebdf2088ef88fa681f8fad45f09922c1ae9a /compiler/rustc_ast/src/token.rs
parentAdding debian version 1.75.0+dfsg1-5. (diff)
downloadrustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.tar.xz
rustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast/src/token.rs')
-rw-r--r--compiler/rustc_ast/src/token.rs67
1 files changed, 45 insertions, 22 deletions
diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs
index 914c97a14..b0cd2ec98 100644
--- a/compiler/rustc_ast/src/token.rs
+++ b/compiler/rustc_ast/src/token.rs
@@ -13,7 +13,7 @@ use rustc_macros::HashStable_Generic;
use rustc_span::symbol::{kw, sym};
#[allow(hidden_glob_reexports)]
use rustc_span::symbol::{Ident, Symbol};
-use rustc_span::{self, edition::Edition, Span, DUMMY_SP};
+use rustc_span::{edition::Edition, Span, DUMMY_SP};
use std::borrow::Cow;
use std::fmt;
@@ -110,7 +110,7 @@ impl Lit {
Ident(name, false) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
Literal(token_lit) => Some(token_lit),
Interpolated(ref nt)
- if let NtExpr(expr) | NtLiteral(expr) = &**nt
+ if let NtExpr(expr) | NtLiteral(expr) = &nt.0
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
{
Some(token_lit)
@@ -238,9 +238,9 @@ pub enum TokenKind {
EqEq,
/// `!=`
Ne,
- /// `>`
- Ge,
/// `>=`
+ Ge,
+ /// `>`
Gt,
/// `&&`
AndAnd,
@@ -314,7 +314,7 @@ pub enum TokenKind {
/// - It prevents `Token` from implementing `Copy`.
/// It adds complexity and likely slows things down. Please don't add new
/// occurrences of this token kind!
- Interpolated(Lrc<Nonterminal>),
+ Interpolated(Lrc<(Nonterminal, Span)>),
/// A doc comment token.
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
@@ -388,7 +388,8 @@ impl TokenKind {
match *self {
Comma => Some(vec![Dot, Lt, Semi]),
Semi => Some(vec![Colon, Comma]),
- FatArrow => Some(vec![Eq, RArrow]),
+ Colon => Some(vec![Semi]),
+ FatArrow => Some(vec![Eq, RArrow, Ge, Gt]),
_ => None,
}
}
@@ -421,7 +422,7 @@ impl Token {
/// if they keep spans or perform edition checks.
pub fn uninterpolated_span(&self) -> Span {
match &self.kind {
- Interpolated(nt) => nt.span(),
+ Interpolated(nt) => nt.0.use_span(),
_ => self.span,
}
}
@@ -464,7 +465,7 @@ impl Token {
ModSep | // global path
Lifetime(..) | // labeled loop
Pound => true, // expression attributes
- Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
+ Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
NtExpr(..) |
NtBlock(..) |
NtPath(..)),
@@ -488,7 +489,7 @@ impl Token {
| DotDot | DotDotDot | DotDotEq // ranges
| Lt | BinOp(Shl) // associated path
| ModSep => true, // global path
- Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
+ Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
NtPat(..) |
NtBlock(..) |
NtPath(..)),
@@ -511,7 +512,7 @@ impl Token {
Lifetime(..) | // lifetime bound in trait object
Lt | BinOp(Shl) | // associated path
ModSep => true, // global path
- Interpolated(ref nt) => matches!(**nt, NtTy(..) | NtPath(..)),
+ Interpolated(ref nt) => matches!(&nt.0, NtTy(..) | NtPath(..)),
// For anonymous structs or unions, which only appear in specific positions
// (type of struct fields or union fields), we don't consider them as regular types
_ => false,
@@ -522,7 +523,7 @@ impl Token {
pub fn can_begin_const_arg(&self) -> bool {
match self.kind {
OpenDelim(Delimiter::Brace) => true,
- Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
+ Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
_ => self.can_begin_literal_maybe_minus(),
}
}
@@ -576,7 +577,7 @@ impl Token {
match self.uninterpolate().kind {
Literal(..) | BinOp(Minus) => true,
Ident(name, false) if name.is_bool_lit() => true,
- Interpolated(ref nt) => match &**nt {
+ Interpolated(ref nt) => match &nt.0 {
NtLiteral(_) => true,
NtExpr(e) => match &e.kind {
ast::ExprKind::Lit(_) => true,
@@ -597,9 +598,9 @@ impl Token {
/// otherwise returns the original token.
pub fn uninterpolate(&self) -> Cow<'_, Token> {
match &self.kind {
- Interpolated(nt) => match **nt {
+ Interpolated(nt) => match &nt.0 {
NtIdent(ident, is_raw) => {
- Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span))
+ 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),
@@ -614,8 +615,8 @@ 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 {
- NtIdent(ident, is_raw) => Some((ident, is_raw)),
+ Interpolated(nt) => match &nt.0 {
+ NtIdent(ident, is_raw) => Some((*ident, *is_raw)),
_ => None,
},
_ => None,
@@ -628,8 +629,8 @@ impl Token {
// We avoid using `Token::uninterpolate` here because it's slow.
match &self.kind {
&Lifetime(name) => Some(Ident::new(name, self.span)),
- Interpolated(nt) => match **nt {
- NtLifetime(ident) => Some(ident),
+ Interpolated(nt) => match &nt.0 {
+ NtLifetime(ident) => Some(*ident),
_ => None,
},
_ => None,
@@ -655,7 +656,7 @@ impl Token {
/// Returns `true` if the token is an interpolated path.
fn is_path(&self) -> bool {
if let Interpolated(nt) = &self.kind
- && let NtPath(..) = **nt
+ && let NtPath(..) = &nt.0
{
return true;
}
@@ -668,7 +669,7 @@ impl Token {
/// (which happens while parsing the result of macro expansion)?
pub fn is_whole_expr(&self) -> bool {
if let Interpolated(nt) = &self.kind
- && let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt
+ && let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &nt.0
{
return true;
}
@@ -679,7 +680,7 @@ impl Token {
/// Is the token an interpolated block (`$b:block`)?
pub fn is_whole_block(&self) -> bool {
if let Interpolated(nt) = &self.kind
- && let NtBlock(..) = **nt
+ && let NtBlock(..) = &nt.0
{
return true;
}
@@ -755,6 +756,11 @@ impl Token {
)
}
+ /// Returns `true` if the token is the integer literal.
+ pub fn is_integer_lit(&self) -> bool {
+ matches!(self.kind, Literal(Lit { kind: LitKind::Integer, .. }))
+ }
+
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
match self.ident() {
@@ -927,7 +933,7 @@ impl fmt::Display for NonterminalKind {
}
impl Nonterminal {
- pub fn span(&self) -> Span {
+ pub fn use_span(&self) -> Span {
match self {
NtItem(item) => item.span,
NtBlock(block) => block.span,
@@ -941,6 +947,23 @@ impl Nonterminal {
NtVis(vis) => vis.span,
}
}
+
+ pub fn descr(&self) -> &'static str {
+ match self {
+ NtItem(..) => "item",
+ NtBlock(..) => "block",
+ NtStmt(..) => "statement",
+ NtPat(..) => "pattern",
+ NtExpr(..) => "expression",
+ NtLiteral(..) => "literal",
+ NtTy(..) => "type",
+ NtIdent(..) => "identifier",
+ NtLifetime(..) => "lifetime",
+ NtMeta(..) => "attribute",
+ NtPath(..) => "path",
+ NtVis(..) => "visibility",
+ }
+ }
}
impl PartialEq for Nonterminal {