Skip to content

Commit d75ee2a

Browse files
committed
Remove MacDelimiter.
It's the same as `Delimiter`, minus the `Invisible` variant. I'm generally in favour of using types to make impossible states unrepresentable, but this one feels very low-value, and the conversions between the two types are annoying and confusing. Look at the change in `src/tools/rustfmt/src/expr.rs` for an example: the old code converted from `MacDelimiter` to `Delimiter` and back again, for no good reason. This suggests the author was confused about the types.
1 parent ba294a8 commit d75ee2a

File tree

13 files changed

+37
-67
lines changed

13 files changed

+37
-67
lines changed

compiler/rustc_ast/src/ast.rs

+3-29
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
1515
//! - [`EnumDef`] and [`Variant`]: Enum declaration.
1616
//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions.
17-
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation.
17+
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`]: Macro definition and invocation.
1818
//! - [`Attribute`]: Metadata associated with item.
1919
//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
2020
@@ -1693,15 +1693,15 @@ where
16931693
#[derive(Clone, Encodable, Decodable, Debug)]
16941694
pub struct DelimArgs {
16951695
pub dspan: DelimSpan,
1696-
pub delim: MacDelimiter,
1696+
pub delim: Delimiter, // Note: `Delimiter::Invisible` never occurs
16971697
pub tokens: TokenStream,
16981698
}
16991699

17001700
impl DelimArgs {
17011701
/// Whether a macro with these arguments needs a semicolon
17021702
/// when used as a standalone item or statement.
17031703
pub fn need_semicolon(&self) -> bool {
1704-
!matches!(self, DelimArgs { delim: MacDelimiter::Brace, .. })
1704+
!matches!(self, DelimArgs { delim: Delimiter::Brace, .. })
17051705
}
17061706
}
17071707

@@ -1717,32 +1717,6 @@ where
17171717
}
17181718
}
17191719

1720-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
1721-
pub enum MacDelimiter {
1722-
Parenthesis,
1723-
Bracket,
1724-
Brace,
1725-
}
1726-
1727-
impl MacDelimiter {
1728-
pub fn to_token(self) -> Delimiter {
1729-
match self {
1730-
MacDelimiter::Parenthesis => Delimiter::Parenthesis,
1731-
MacDelimiter::Bracket => Delimiter::Bracket,
1732-
MacDelimiter::Brace => Delimiter::Brace,
1733-
}
1734-
}
1735-
1736-
pub fn from_token(delim: Delimiter) -> Option<MacDelimiter> {
1737-
match delim {
1738-
Delimiter::Parenthesis => Some(MacDelimiter::Parenthesis),
1739-
Delimiter::Bracket => Some(MacDelimiter::Bracket),
1740-
Delimiter::Brace => Some(MacDelimiter::Brace),
1741-
Delimiter::Invisible => None,
1742-
}
1743-
}
1744-
}
1745-
17461720
/// Represents a macro definition.
17471721
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
17481722
pub struct MacroDef {

compiler/rustc_ast/src/attr/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
44
use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
5-
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
5+
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
66
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
77
use crate::ptr::P;
88
use crate::token::{self, CommentKind, Delimiter, Token};
@@ -196,7 +196,7 @@ impl AttrItem {
196196

197197
fn meta_item_list(&self) -> Option<ThinVec<NestedMetaItem>> {
198198
match &self.args {
199-
AttrArgs::Delimited(args) if args.delim == MacDelimiter::Parenthesis => {
199+
AttrArgs::Delimited(args) if args.delim == Delimiter::Parenthesis => {
200200
MetaItemKind::list_from_tokens(args.tokens.clone())
201201
}
202202
AttrArgs::Delimited(_) | AttrArgs::Eq(..) | AttrArgs::Empty => None,
@@ -402,11 +402,9 @@ impl MetaItemKind {
402402
fn from_attr_args(args: &AttrArgs) -> Option<MetaItemKind> {
403403
match args {
404404
AttrArgs::Empty => Some(MetaItemKind::Word),
405-
AttrArgs::Delimited(DelimArgs {
406-
dspan: _,
407-
delim: MacDelimiter::Parenthesis,
408-
tokens,
409-
}) => MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List),
405+
AttrArgs::Delimited(DelimArgs { dspan: _, delim: Delimiter::Parenthesis, tokens }) => {
406+
MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List)
407+
}
410408
AttrArgs::Delimited(..) => None,
411409
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
412410
ExprKind::Lit(token_lit) => {
@@ -578,7 +576,7 @@ pub fn mk_attr_nested_word(
578576
let path = Path::from_ident(outer_ident);
579577
let attr_args = AttrArgs::Delimited(DelimArgs {
580578
dspan: DelimSpan::from_single(span),
581-
delim: MacDelimiter::Parenthesis,
579+
delim: Delimiter::Parenthesis,
582580
tokens: inner_tokens,
583581
});
584582
mk_attr(g, style, path, attr_args, span)

compiler/rustc_ast_pretty/src/pprust/state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
476476
Some(MacHeader::Path(&item.path)),
477477
false,
478478
None,
479-
delim.to_token(),
479+
*delim,
480480
tokens,
481481
true,
482482
span,
@@ -640,7 +640,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
640640
Some(MacHeader::Keyword(kw)),
641641
has_bang,
642642
Some(*ident),
643-
macro_def.body.delim.to_token(),
643+
macro_def.body.delim,
644644
&macro_def.body.tokens.clone(),
645645
true,
646646
sp,
@@ -1240,7 +1240,7 @@ impl<'a> State<'a> {
12401240
Some(MacHeader::Path(&m.path)),
12411241
true,
12421242
None,
1243-
m.args.delim.to_token(),
1243+
m.args.delim,
12441244
&m.args.tokens.clone(),
12451245
true,
12461246
m.span(),

compiler/rustc_builtin_macros/src/assert.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::edition_panic::use_panic_2021;
44
use crate::errors;
55
use rustc_ast::ptr::P;
66
use rustc_ast::token;
7+
use rustc_ast::token::Delimiter;
78
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
8-
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, MacDelimiter, Path, PathSegment, UnOp};
9+
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment, UnOp};
910
use rustc_ast_pretty::pprust;
1011
use rustc_errors::PResult;
1112
use rustc_expand::base::{DummyResult, ExtCtxt, MacEager, MacResult};
@@ -58,7 +59,7 @@ pub fn expand_assert<'cx>(
5859
path: panic_path(),
5960
args: P(DelimArgs {
6061
dspan: DelimSpan::from_single(call_site_span),
61-
delim: MacDelimiter::Parenthesis,
62+
delim: Delimiter::Parenthesis,
6263
tokens,
6364
}),
6465
})),

compiler/rustc_builtin_macros/src/assert/context.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc_ast::{
22
ptr::P,
33
token,
4+
token::Delimiter,
45
tokenstream::{DelimSpan, TokenStream, TokenTree},
5-
BinOpKind, BorrowKind, DelimArgs, Expr, ExprKind, ItemKind, MacCall, MacDelimiter, MethodCall,
6-
Mutability, Path, PathSegment, Stmt, StructRest, UnOp, UseTree, UseTreeKind, DUMMY_NODE_ID,
6+
BinOpKind, BorrowKind, DelimArgs, Expr, ExprKind, ItemKind, MacCall, MethodCall, Mutability,
7+
Path, PathSegment, Stmt, StructRest, UnOp, UseTree, UseTreeKind, DUMMY_NODE_ID,
78
};
89
use rustc_ast_pretty::pprust;
910
use rustc_data_structures::fx::FxHashSet;
@@ -179,7 +180,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
179180
path: panic_path,
180181
args: P(DelimArgs {
181182
dspan: DelimSpan::from_single(self.span),
182-
delim: MacDelimiter::Parenthesis,
183+
delim: Delimiter::Parenthesis,
183184
tokens: initial.into_iter().chain(captures).collect::<TokenStream>(),
184185
}),
185186
})),

compiler/rustc_builtin_macros/src/edition_panic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::ptr::P;
2+
use rustc_ast::token::Delimiter;
23
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
34
use rustc_ast::*;
45
use rustc_expand::base::*;
@@ -60,7 +61,7 @@ fn expand<'cx>(
6061
},
6162
args: P(DelimArgs {
6263
dspan: DelimSpan::from_single(sp),
63-
delim: MacDelimiter::Parenthesis,
64+
delim: Delimiter::Parenthesis,
6465
tokens: tts,
6566
}),
6667
})),

compiler/rustc_expand/src/placeholders.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::expand::{AstFragment, AstFragmentKind};
22
use rustc_ast as ast;
33
use rustc_ast::mut_visit::*;
44
use rustc_ast::ptr::P;
5+
use rustc_ast::token::Delimiter;
56
use rustc_data_structures::fx::FxHashMap;
67
use rustc_span::source_map::DUMMY_SP;
78
use rustc_span::symbol::Ident;
@@ -18,7 +19,7 @@ pub fn placeholder(
1819
path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None },
1920
args: P(ast::DelimArgs {
2021
dspan: ast::tokenstream::DelimSpan::dummy(),
21-
delim: ast::MacDelimiter::Parenthesis,
22+
delim: Delimiter::Parenthesis,
2223
tokens: ast::tokenstream::TokenStream::new(Vec::new()),
2324
}),
2425
})

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,7 @@ impl<'a> Parser<'a> {
25992599

26002600
// Recover from missing expression in `for` loop
26012601
if matches!(expr.kind, ExprKind::Block(..))
2602-
&& !matches!(self.token.kind, token::OpenDelim(token::Delimiter::Brace))
2602+
&& !matches!(self.token.kind, token::OpenDelim(Delimiter::Brace))
26032603
&& self.may_recover()
26042604
{
26052605
self.sess

compiler/rustc_parse/src/parser/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use rustc_ast::ptr::P;
99
use rustc_ast::token::{self, Delimiter, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1111
use rustc_ast::util::case::Case;
12+
use rustc_ast::MacCall;
1213
use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
1314
use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
1415
use rustc_ast::{BindingAnnotation, Block, FnDecl, FnSig, Param, SelfKind};
1516
use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData};
1617
use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
17-
use rustc_ast::{MacCall, MacDelimiter};
1818
use rustc_ast_pretty::pprust;
1919
use rustc_errors::{
2020
struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, PResult,
@@ -1968,7 +1968,7 @@ impl<'a> Parser<'a> {
19681968
let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); // `=>`
19691969
let tokens = TokenStream::new(vec![params, arrow, body]);
19701970
let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1971-
P(DelimArgs { dspan, delim: MacDelimiter::Brace, tokens })
1971+
P(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
19721972
} else {
19731973
return self.unexpected();
19741974
};

compiler/rustc_parse/src/parser/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_ast::util::case::Case;
2525
use rustc_ast::AttrId;
2626
use rustc_ast::DUMMY_NODE_ID;
2727
use rustc_ast::{self as ast, AnonConst, Const, DelimArgs, Extern};
28-
use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, MacDelimiter, Mutability, StrLit};
28+
use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, Mutability, StrLit};
2929
use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind};
3030
use rustc_ast_pretty::pprust;
3131
use rustc_data_structures::fx::FxHashMap;
@@ -1216,12 +1216,10 @@ impl<'a> Parser<'a> {
12161216
|| self.check(&token::OpenDelim(Delimiter::Brace));
12171217

12181218
delimited.then(|| {
1219-
// We've confirmed above that there is a delimiter so unwrapping is OK.
12201219
let TokenTree::Delimited(dspan, delim, tokens) = self.parse_token_tree() else {
12211220
unreachable!()
12221221
};
1223-
1224-
DelimArgs { dspan, delim: MacDelimiter::from_token(delim).unwrap(), tokens }
1222+
DelimArgs { dspan, delim, tokens }
12251223
})
12261224
}
12271225

compiler/rustc_parse/src/parser/stmt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,9 @@ impl<'a> Parser<'a> {
193193
/// At this point, the `!` token after the path has already been eaten.
194194
fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResult<'a, Stmt> {
195195
let args = self.parse_delim_args()?;
196-
let delim = args.delim.to_token();
197196
let hi = self.prev_token.span;
198197

199-
let style = match delim {
198+
let style = match args.delim {
200199
Delimiter::Brace => MacStmtStyle::Braces,
201200
_ => MacStmtStyle::NoBraces,
202201
};

compiler/rustc_parse/src/validate_attr.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
33
use crate::{errors, parse_in};
44

5+
use rustc_ast::token::Delimiter;
56
use rustc_ast::tokenstream::DelimSpan;
67
use rustc_ast::MetaItemKind;
7-
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MacDelimiter, MetaItem};
8+
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MetaItem};
89
use rustc_ast_pretty::pprust;
910
use rustc_errors::{Applicability, FatalError, PResult};
1011
use rustc_feature::{AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
@@ -84,8 +85,8 @@ pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Meta
8485
})
8586
}
8687

87-
pub fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter) {
88-
if let ast::MacDelimiter::Parenthesis = delim {
88+
pub fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: Delimiter) {
89+
if let Delimiter::Parenthesis = delim {
8990
return;
9091
}
9192
sess.emit_err(errors::MetaBadDelim {
@@ -94,8 +95,8 @@ pub fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimit
9495
});
9596
}
9697

97-
pub fn check_cfg_attr_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter) {
98-
if let ast::MacDelimiter::Parenthesis = delim {
98+
pub fn check_cfg_attr_bad_delim(sess: &ParseSess, span: DelimSpan, delim: Delimiter) {
99+
if let Delimiter::Parenthesis = delim {
99100
return;
100101
}
101102
sess.emit_err(errors::CfgAttrBadDelim {

src/tools/rustfmt/src/expr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1382,12 +1382,8 @@ pub(crate) fn can_be_overflowed_expr(
13821382
|| (context.use_block_indent() && args_len == 1)
13831383
}
13841384
ast::ExprKind::MacCall(ref mac) => {
1385-
match (
1386-
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim.to_token()),
1387-
context.config.overflow_delimited_expr(),
1388-
) {
1389-
(Some(ast::MacDelimiter::Bracket), true)
1390-
| (Some(ast::MacDelimiter::Brace), true) => true,
1385+
match (mac.args.delim, context.config.overflow_delimited_expr()) {
1386+
(Delimiter::Bracket, true) | (Delimiter::Brace, true) => true,
13911387
_ => context.use_block_indent() && args_len == 1,
13921388
}
13931389
}

0 commit comments

Comments
 (0)