Skip to content

Commit d6905ce

Browse files
authored
Rollup merge of rust-lang#96433 - petrochenkov:delim, r=nnethercote
rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter` Compiler cannot reuse `proc_macro::Delimiter` directly due to extra impls, but can at least use the same naming. After this PR the only difference between these two enums is that `proc_macro::Delimiter::None` is turned into `token::Delimiter::Invisible`. It's my mistake that the invisible delimiter is called `None` on stable, during the stabilization I audited the naming and wrote the docs, but missed the fact that the `None` naming gives a wrong and confusing impression about what this thing is. cc rust-lang#96421 r? `@nnethercote`
2 parents c9fb0b4 + 2733ec1 commit d6905ce

File tree

41 files changed

+433
-426
lines changed

Some content is hidden

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

41 files changed

+433
-426
lines changed

compiler/rustc_ast/src/ast.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use GenericArgs::*;
2323
pub use UnsafeSource::*;
2424

2525
use crate::ptr::P;
26-
use crate::token::{self, CommentKind, DelimToken, Token};
26+
use crate::token::{self, CommentKind, Delimiter, Token};
2727
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree};
2828

2929
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1542,7 +1542,7 @@ pub enum MacArgs {
15421542
}
15431543

15441544
impl MacArgs {
1545-
pub fn delim(&self) -> Option<DelimToken> {
1545+
pub fn delim(&self) -> Option<Delimiter> {
15461546
match self {
15471547
MacArgs::Delimited(_, delim, _) => Some(delim.to_token()),
15481548
MacArgs::Empty | MacArgs::Eq(..) => None,
@@ -1582,20 +1582,20 @@ pub enum MacDelimiter {
15821582
}
15831583

15841584
impl MacDelimiter {
1585-
pub fn to_token(self) -> DelimToken {
1585+
pub fn to_token(self) -> Delimiter {
15861586
match self {
1587-
MacDelimiter::Parenthesis => DelimToken::Paren,
1588-
MacDelimiter::Bracket => DelimToken::Bracket,
1589-
MacDelimiter::Brace => DelimToken::Brace,
1587+
MacDelimiter::Parenthesis => Delimiter::Parenthesis,
1588+
MacDelimiter::Bracket => Delimiter::Bracket,
1589+
MacDelimiter::Brace => Delimiter::Brace,
15901590
}
15911591
}
15921592

1593-
pub fn from_token(delim: DelimToken) -> Option<MacDelimiter> {
1593+
pub fn from_token(delim: Delimiter) -> Option<MacDelimiter> {
15941594
match delim {
1595-
token::Paren => Some(MacDelimiter::Parenthesis),
1596-
token::Bracket => Some(MacDelimiter::Bracket),
1597-
token::Brace => Some(MacDelimiter::Brace),
1598-
token::NoDelim => None,
1595+
Delimiter::Parenthesis => Some(MacDelimiter::Parenthesis),
1596+
Delimiter::Bracket => Some(MacDelimiter::Bracket),
1597+
Delimiter::Brace => Some(MacDelimiter::Brace),
1598+
Delimiter::Invisible => None,
15991599
}
16001600
}
16011601
}

compiler/rustc_ast/src/attr/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
55
use crate::ast::{Lit, LitKind};
66
use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
77
use crate::ast::{Path, PathSegment};
8-
use crate::token::{self, CommentKind, Token};
8+
use crate::token::{self, CommentKind, Delimiter, Token};
99
use crate::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
1010
use crate::tokenstream::{DelimSpan, Spacing, TokenTree, TreeAndSpacing};
1111
use crate::tokenstream::{LazyTokenStream, TokenStream};
@@ -513,7 +513,7 @@ impl MetaItemKind {
513513
vec![
514514
TokenTree::Delimited(
515515
DelimSpan::from_single(span),
516-
token::Paren,
516+
Delimiter::Parenthesis,
517517
TokenStream::new(tokens),
518518
)
519519
.into(),
@@ -540,7 +540,7 @@ impl MetaItemKind {
540540
tokens: &mut impl Iterator<Item = TokenTree>,
541541
) -> Option<MetaItemKind> {
542542
match tokens.next() {
543-
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
543+
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
544544
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
545545
}
546546
Some(TokenTree::Token(token)) => {
@@ -565,7 +565,7 @@ impl MetaItemKind {
565565
tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
566566
) -> Option<MetaItemKind> {
567567
match tokens.peek() {
568-
Some(TokenTree::Delimited(_, token::Paren, inner_tokens)) => {
568+
Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => {
569569
let inner_tokens = inner_tokens.clone();
570570
tokens.next();
571571
MetaItemKind::list_from_tokens(inner_tokens)
@@ -606,7 +606,7 @@ impl NestedMetaItem {
606606
tokens.next();
607607
return Some(NestedMetaItem::Literal(lit));
608608
}
609-
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
609+
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
610610
let inner_tokens = inner_tokens.clone();
611611
tokens.next();
612612
return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());

compiler/rustc_ast/src/token.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub use BinOpToken::*;
2-
pub use DelimToken::*;
32
pub use LitKind::*;
43
pub use Nonterminal::*;
54
pub use TokenKind::*;
@@ -37,18 +36,26 @@ pub enum BinOpToken {
3736
Shr,
3837
}
3938

40-
/// A delimiter token.
41-
#[derive(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Debug, Copy)]
42-
#[derive(HashStable_Generic)]
43-
pub enum DelimToken {
44-
/// A round parenthesis (i.e., `(` or `)`).
45-
Paren,
46-
/// A square bracket (i.e., `[` or `]`).
47-
Bracket,
48-
/// A curly brace (i.e., `{` or `}`).
39+
/// Describes how a sequence of token trees is delimited.
40+
/// Cannot use `proc_macro::Delimiter` directly because this
41+
/// structure should implement some additional traits.
42+
/// The `None` variant is also renamed to `Invisible` to be
43+
/// less confusing and better convey the semantics.
44+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45+
#[derive(Encodable, Decodable, Hash, HashStable_Generic)]
46+
pub enum Delimiter {
47+
/// `( ... )`
48+
Parenthesis,
49+
/// `{ ... }`
4950
Brace,
50-
/// An empty delimiter.
51-
NoDelim,
51+
/// `[ ... ]`
52+
Bracket,
53+
/// `Ø ... Ø`
54+
/// An invisible delimiter, that may, for example, appear around tokens coming from a
55+
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
56+
/// `$var * 3` where `$var` is `1 + 2`.
57+
/// Invisible delimiters might not survive roundtrip of a token stream through a string.
58+
Invisible,
5259
}
5360

5461
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
@@ -212,9 +219,9 @@ pub enum TokenKind {
212219
/// Used by proc macros for representing lifetimes, not generated by lexer right now.
213220
SingleQuote,
214221
/// An opening delimiter (e.g., `{`).
215-
OpenDelim(DelimToken),
222+
OpenDelim(Delimiter),
216223
/// A closing delimiter (e.g., `}`).
217-
CloseDelim(DelimToken),
224+
CloseDelim(Delimiter),
218225

219226
/* Literals */
220227
Literal(Lit),
@@ -387,8 +394,8 @@ impl Token {
387394
match self.uninterpolate().kind {
388395
Ident(name, is_raw) =>
389396
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
390-
OpenDelim(Paren) | // tuple
391-
OpenDelim(Bracket) | // array
397+
OpenDelim(Delimiter::Parenthesis) | // tuple
398+
OpenDelim(Delimiter::Bracket) | // array
392399
Not | // never
393400
BinOp(Star) | // raw pointer
394401
BinOp(And) | // reference
@@ -405,7 +412,7 @@ impl Token {
405412
/// Returns `true` if the token can appear at the start of a const param.
406413
pub fn can_begin_const_arg(&self) -> bool {
407414
match self.kind {
408-
OpenDelim(Brace) => true,
415+
OpenDelim(Delimiter::Brace) => true,
409416
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
410417
_ => self.can_begin_literal_maybe_minus(),
411418
}
@@ -417,7 +424,7 @@ impl Token {
417424
|| self.is_lifetime()
418425
|| self.is_keyword(kw::For)
419426
|| self == &Question
420-
|| self == &OpenDelim(Paren)
427+
|| self == &OpenDelim(Delimiter::Parenthesis)
421428
}
422429

423430
/// Returns `true` if the token is any literal.

compiler/rustc_ast/src/tokenstream.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
1414
//! ownership of the original.
1515
16-
use crate::token::{self, DelimToken, Token, TokenKind};
16+
use crate::token::{self, Delimiter, Token, TokenKind};
1717
use crate::AttrVec;
1818

1919
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -42,7 +42,7 @@ pub enum TokenTree {
4242
/// A single token.
4343
Token(Token),
4444
/// A delimited sequence of token trees.
45-
Delimited(DelimSpan, DelimToken, TokenStream),
45+
Delimited(DelimSpan, Delimiter, TokenStream),
4646
}
4747

4848
#[derive(Copy, Clone)]
@@ -57,7 +57,7 @@ fn _dummy()
5757
where
5858
Token: Send + Sync,
5959
DelimSpan: Send + Sync,
60-
DelimToken: Send + Sync,
60+
Delimiter: Send + Sync,
6161
TokenStream: Send + Sync,
6262
{
6363
}
@@ -175,7 +175,7 @@ pub struct AttrAnnotatedTokenStream(pub Lrc<Vec<(AttrAnnotatedTokenTree, Spacing
175175
#[derive(Clone, Debug, Encodable, Decodable)]
176176
pub enum AttrAnnotatedTokenTree {
177177
Token(Token),
178-
Delimited(DelimSpan, DelimToken, AttrAnnotatedTokenStream),
178+
Delimited(DelimSpan, Delimiter, AttrAnnotatedTokenStream),
179179
/// Stores the attributes for an attribute target,
180180
/// along with the tokens for that attribute target.
181181
/// See `AttributesData` for more information

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#![recursion_limit = "256"]
3939
#![allow(rustc::potential_query_instability)]
4040

41-
use rustc_ast::token::{self, Token};
41+
use rustc_ast::token::{Delimiter, Token};
4242
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
4343
use rustc_ast::visit;
4444
use rustc_ast::{self as ast, *};
@@ -886,7 +886,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
886886
match tokens.into_trees().next() {
887887
Some(TokenTree::Token(token)) => token,
888888
Some(TokenTree::Delimited(_, delim, tokens)) => {
889-
if delim != token::NoDelim {
889+
if delim != Delimiter::Invisible {
890890
sess.diagnostic().delay_span_bug(
891891
span,
892892
"unexpected delimiter in key-value attribute's value",

compiler/rustc_ast_pretty/src/pprust/state.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::pp::Breaks::{Consistent, Inconsistent};
66
use crate::pp::{self, Breaks};
77

88
use rustc_ast::ptr::P;
9-
use rustc_ast::token::{self, BinOpToken, CommentKind, DelimToken, Nonterminal, Token, TokenKind};
9+
use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind};
1010
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1111
use rustc_ast::util::classify;
1212
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
@@ -155,10 +155,10 @@ fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
155155
}
156156
match tt {
157157
TokenTree::Token(token) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
158-
TokenTree::Delimited(_, DelimToken::Paren, _) => {
158+
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
159159
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }))
160160
}
161-
TokenTree::Delimited(_, DelimToken::Bracket, _) => {
161+
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
162162
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }))
163163
}
164164
TokenTree::Delimited(..) => true,
@@ -556,12 +556,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
556556
header: Option<MacHeader<'_>>,
557557
has_bang: bool,
558558
ident: Option<Ident>,
559-
delim: Option<DelimToken>,
559+
delim: Option<Delimiter>,
560560
tts: &TokenStream,
561561
convert_dollar_crate: bool,
562562
span: Span,
563563
) {
564-
if delim == Some(DelimToken::Brace) {
564+
if delim == Some(Delimiter::Brace) {
565565
self.cbox(INDENT_UNIT);
566566
}
567567
match header {
@@ -577,7 +577,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
577577
self.print_ident(ident);
578578
}
579579
match delim {
580-
Some(DelimToken::Brace) => {
580+
Some(Delimiter::Brace) => {
581581
if header.is_some() || has_bang || ident.is_some() {
582582
self.nbsp();
583583
}
@@ -758,13 +758,15 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
758758
token::RArrow => "->".into(),
759759
token::LArrow => "<-".into(),
760760
token::FatArrow => "=>".into(),
761-
token::OpenDelim(token::Paren) => "(".into(),
762-
token::CloseDelim(token::Paren) => ")".into(),
763-
token::OpenDelim(token::Bracket) => "[".into(),
764-
token::CloseDelim(token::Bracket) => "]".into(),
765-
token::OpenDelim(token::Brace) => "{".into(),
766-
token::CloseDelim(token::Brace) => "}".into(),
767-
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".into(),
761+
token::OpenDelim(Delimiter::Parenthesis) => "(".into(),
762+
token::CloseDelim(Delimiter::Parenthesis) => ")".into(),
763+
token::OpenDelim(Delimiter::Bracket) => "[".into(),
764+
token::CloseDelim(Delimiter::Bracket) => "]".into(),
765+
token::OpenDelim(Delimiter::Brace) => "{".into(),
766+
token::CloseDelim(Delimiter::Brace) => "}".into(),
767+
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible) => {
768+
"".into()
769+
}
768770
token::Pound => "#".into(),
769771
token::Dollar => "$".into(),
770772
token::Question => "?".into(),

compiler/rustc_builtin_macros/src/asm.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast as ast;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::token;
3+
use rustc_ast::token::{self, Delimiter};
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_errors::{Applicability, PResult};
@@ -395,9 +395,9 @@ fn parse_options<'a>(
395395
) -> PResult<'a, ()> {
396396
let span_start = p.prev_token.span;
397397

398-
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
398+
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
399399

400-
while !p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
400+
while !p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
401401
if !is_global_asm && p.eat_keyword(sym::pure) {
402402
try_set_option(p, args, sym::pure, ast::InlineAsmOptions::PURE);
403403
} else if !is_global_asm && p.eat_keyword(sym::nomem) {
@@ -421,7 +421,7 @@ fn parse_options<'a>(
421421
}
422422

423423
// Allow trailing commas
424-
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
424+
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
425425
break;
426426
}
427427
p.expect(&token::Comma)?;
@@ -436,9 +436,9 @@ fn parse_options<'a>(
436436
fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a, ()> {
437437
let span_start = p.prev_token.span;
438438

439-
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
439+
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
440440

441-
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
441+
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
442442
let err = p.sess.span_diagnostic.struct_span_err(
443443
p.token.span,
444444
"at least one abi must be provided as an argument to `clobber_abi`",
@@ -454,7 +454,7 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,
454454
}
455455
Err(opt_lit) => {
456456
// If the non-string literal is a closing paren then it's the end of the list and is fine
457-
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
457+
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
458458
break;
459459
}
460460
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
@@ -466,7 +466,7 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,
466466
};
467467

468468
// Allow trailing commas
469-
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
469+
if p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
470470
break;
471471
}
472472
p.expect(&token::Comma)?;
@@ -501,7 +501,7 @@ fn parse_reg<'a>(
501501
p: &mut Parser<'a>,
502502
explicit_reg: &mut bool,
503503
) -> PResult<'a, ast::InlineAsmRegOrRegClass> {
504-
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
504+
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
505505
let result = match p.token.uninterpolate().kind {
506506
token::Ident(name, false) => ast::InlineAsmRegOrRegClass::RegClass(name),
507507
token::Literal(token::Lit { kind: token::LitKind::Str, symbol, suffix: _ }) => {
@@ -515,7 +515,7 @@ fn parse_reg<'a>(
515515
}
516516
};
517517
p.bump();
518-
p.expect(&token::CloseDelim(token::DelimToken::Paren))?;
518+
p.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
519519
Ok(result)
520520
}
521521

compiler/rustc_expand/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Conditional compilation stripping.
22
33
use rustc_ast::ptr::P;
4-
use rustc_ast::token::{DelimToken, Token, TokenKind};
4+
use rustc_ast::token::{Delimiter, Token, TokenKind};
55
use rustc_ast::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
66
use rustc_ast::tokenstream::{DelimSpan, Spacing};
77
use rustc_ast::tokenstream::{LazyTokenStream, TokenTree};
@@ -418,7 +418,7 @@ impl<'a> StripUnconfigured<'a> {
418418
// in `#[attr]`, so just use the span of the `#` token.
419419
let bracket_group = AttrAnnotatedTokenTree::Delimited(
420420
DelimSpan::from_single(pound_span),
421-
DelimToken::Bracket,
421+
Delimiter::Bracket,
422422
item.tokens
423423
.as_ref()
424424
.unwrap_or_else(|| panic!("Missing tokens for {:?}", item))

0 commit comments

Comments
 (0)