Skip to content

Commit 6b367a0

Browse files
committed
Avoid producing NoDelim values in MacArgs::delim().
1 parent f0bbc78 commit 6b367a0

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,10 +1542,10 @@ pub enum MacArgs {
15421542
}
15431543

15441544
impl MacArgs {
1545-
pub fn delim(&self) -> DelimToken {
1545+
pub fn delim(&self) -> Option<DelimToken> {
15461546
match self {
1547-
MacArgs::Delimited(_, delim, _) => delim.to_token(),
1548-
MacArgs::Empty | MacArgs::Eq(..) => token::NoDelim,
1547+
MacArgs::Delimited(_, delim, _) => Some(delim.to_token()),
1548+
MacArgs::Empty | MacArgs::Eq(..) => None,
15491549
}
15501550
}
15511551

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
464464
Some(MacHeader::Path(&item.path)),
465465
false,
466466
None,
467-
delim.to_token(),
467+
Some(delim.to_token()),
468468
tokens,
469469
true,
470470
span,
@@ -530,7 +530,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
530530
None,
531531
false,
532532
None,
533-
*delim,
533+
Some(*delim),
534534
tts,
535535
convert_dollar_crate,
536536
dspan.entire(),
@@ -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: DelimToken,
559+
delim: Option<DelimToken>,
560560
tts: &TokenStream,
561561
convert_dollar_crate: bool,
562562
span: Span,
563563
) {
564-
if delim == DelimToken::Brace {
564+
if delim == Some(DelimToken::Brace) {
565565
self.cbox(INDENT_UNIT);
566566
}
567567
match header {
@@ -577,31 +577,33 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
577577
self.print_ident(ident);
578578
}
579579
match delim {
580-
DelimToken::Brace => {
580+
Some(DelimToken::Brace) => {
581581
if header.is_some() || has_bang || ident.is_some() {
582582
self.nbsp();
583583
}
584584
self.word("{");
585585
if !tts.is_empty() {
586586
self.space();
587587
}
588-
}
589-
_ => {
590-
let token_str = self.token_kind_to_string(&token::OpenDelim(delim));
591-
self.word(token_str)
592-
}
593-
}
594-
self.ibox(0);
595-
self.print_tts(tts, convert_dollar_crate);
596-
self.end();
597-
match delim {
598-
DelimToken::Brace => {
588+
self.ibox(0);
589+
self.print_tts(tts, convert_dollar_crate);
590+
self.end();
599591
let empty = tts.is_empty();
600592
self.bclose(span, empty);
601593
}
602-
_ => {
594+
Some(delim) => {
595+
let token_str = self.token_kind_to_string(&token::OpenDelim(delim));
596+
self.word(token_str);
597+
self.ibox(0);
598+
self.print_tts(tts, convert_dollar_crate);
599+
self.end();
603600
let token_str = self.token_kind_to_string(&token::CloseDelim(delim));
604-
self.word(token_str)
601+
self.word(token_str);
602+
}
603+
None => {
604+
self.ibox(0);
605+
self.print_tts(tts, convert_dollar_crate);
606+
self.end();
605607
}
606608
}
607609
}

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,29 @@ impl<'a> Parser<'a> {
164164
let delim = args.delim();
165165
let hi = self.prev_token.span;
166166

167-
let style =
168-
if delim == token::Brace { MacStmtStyle::Braces } else { MacStmtStyle::NoBraces };
167+
let style = match delim {
168+
Some(token::Brace) => MacStmtStyle::Braces,
169+
Some(_) => MacStmtStyle::NoBraces,
170+
None => unreachable!(),
171+
};
169172

170173
let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription };
171174

172-
let kind =
173-
if (delim == token::Brace && self.token != token::Dot && self.token != token::Question)
174-
|| self.token == token::Semi
175-
|| self.token == token::Eof
176-
{
177-
StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None }))
178-
} else {
179-
// Since none of the above applied, this is an expression statement macro.
180-
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new());
181-
let e = self.maybe_recover_from_bad_qpath(e, true)?;
182-
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
183-
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
184-
StmtKind::Expr(e)
185-
};
175+
let kind = if (style == MacStmtStyle::Braces
176+
&& self.token != token::Dot
177+
&& self.token != token::Question)
178+
|| self.token == token::Semi
179+
|| self.token == token::Eof
180+
{
181+
StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None }))
182+
} else {
183+
// Since none of the above applied, this is an expression statement macro.
184+
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new());
185+
let e = self.maybe_recover_from_bad_qpath(e, true)?;
186+
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
187+
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
188+
StmtKind::Expr(e)
189+
};
186190
Ok(self.mk_stmt(lo.to(hi), kind))
187191
}
188192

src/tools/rustfmt/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ pub(crate) fn can_be_overflowed_expr(
13251325
}
13261326
ast::ExprKind::MacCall(ref mac) => {
13271327
match (
1328-
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim()),
1328+
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim().unwrap()),
13291329
context.config.overflow_delimited_expr(),
13301330
) {
13311331
(Some(ast::MacDelimiter::Bracket), true)

0 commit comments

Comments
 (0)