Skip to content

Commit 70067e3

Browse files
committed
rustc_ast: Harmonize delimiter naming with proc_macro::Delimiter
1 parent f300792 commit 70067e3

File tree

6 files changed

+51
-51
lines changed

6 files changed

+51
-51
lines changed

src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22
use std::cmp::min;
33

44
use itertools::Itertools;
5-
use rustc_ast::token::{DelimToken, LitKind};
5+
use rustc_ast::token::{Delimiter, LitKind};
66
use rustc_ast::{ast, ptr};
77
use rustc_span::{BytePos, Span};
88

@@ -412,7 +412,7 @@ pub(crate) fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
412412
context: &'a RewriteContext<'_>,
413413
shape: Shape,
414414
force_separator_tactic: Option<SeparatorTactic>,
415-
delim_token: Option<DelimToken>,
415+
delim_token: Option<Delimiter>,
416416
) -> Option<String> {
417417
overflow::rewrite_with_square_brackets(
418418
context,

src/macros.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use std::collections::HashMap;
1313
use std::panic::{catch_unwind, AssertUnwindSafe};
1414

15-
use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind};
15+
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
1616
use rustc_ast::tokenstream::{Cursor, Spacing, TokenStream, TokenTree};
1717
use rustc_ast::{ast, ptr};
1818
use rustc_ast_pretty::pprust;
@@ -203,7 +203,7 @@ fn rewrite_macro_inner(
203203
let is_forced_bracket = FORCED_BRACKET_MACROS.contains(&&macro_name[..]);
204204

205205
let style = if is_forced_bracket && !is_nested_macro {
206-
DelimToken::Bracket
206+
Delimiter::Bracket
207207
} else {
208208
original_style
209209
};
@@ -212,15 +212,15 @@ fn rewrite_macro_inner(
212212
let has_comment = contains_comment(context.snippet(mac.span()));
213213
if ts.is_empty() && !has_comment {
214214
return match style {
215-
DelimToken::Paren if position == MacroPosition::Item => {
215+
Delimiter::Parenthesis if position == MacroPosition::Item => {
216216
Some(format!("{}();", macro_name))
217217
}
218-
DelimToken::Bracket if position == MacroPosition::Item => {
218+
Delimiter::Bracket if position == MacroPosition::Item => {
219219
Some(format!("{}[];", macro_name))
220220
}
221-
DelimToken::Paren => Some(format!("{}()", macro_name)),
222-
DelimToken::Bracket => Some(format!("{}[]", macro_name)),
223-
DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
221+
Delimiter::Parenthesis => Some(format!("{}()", macro_name)),
222+
Delimiter::Bracket => Some(format!("{}[]", macro_name)),
223+
Delimiter::Brace => Some(format!("{} {{}}", macro_name)),
224224
_ => unreachable!(),
225225
};
226226
}
@@ -260,7 +260,7 @@ fn rewrite_macro_inner(
260260
}
261261

262262
match style {
263-
DelimToken::Paren => {
263+
Delimiter::Parenthesis => {
264264
// Handle special case: `vec!(expr; expr)`
265265
if vec_with_semi {
266266
handle_vec_semi(context, shape, arg_vec, macro_name, style)
@@ -286,7 +286,7 @@ fn rewrite_macro_inner(
286286
})
287287
}
288288
}
289-
DelimToken::Bracket => {
289+
Delimiter::Bracket => {
290290
// Handle special case: `vec![expr; expr]`
291291
if vec_with_semi {
292292
handle_vec_semi(context, shape, arg_vec, macro_name, style)
@@ -323,7 +323,7 @@ fn rewrite_macro_inner(
323323
Some(format!("{}{}", rewrite, comma))
324324
}
325325
}
326-
DelimToken::Brace => {
326+
Delimiter::Brace => {
327327
// For macro invocations with braces, always put a space between
328328
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
329329
// anything in between the braces (for now).
@@ -342,11 +342,11 @@ fn handle_vec_semi(
342342
shape: Shape,
343343
arg_vec: Vec<MacroArg>,
344344
macro_name: String,
345-
delim_token: DelimToken,
345+
delim_token: Delimiter,
346346
) -> Option<String> {
347347
let (left, right) = match delim_token {
348-
DelimToken::Paren => ("(", ")"),
349-
DelimToken::Bracket => ("[", "]"),
348+
Delimiter::Parenthesis => ("(", ")"),
349+
Delimiter::Bracket => ("[", "]"),
350350
_ => unreachable!(),
351351
};
352352

@@ -528,7 +528,7 @@ enum MacroArgKind {
528528
/// e.g., `$($foo: expr),*`
529529
Repeat(
530530
/// `()`, `[]` or `{}`.
531-
DelimToken,
531+
Delimiter,
532532
/// Inner arguments inside delimiters.
533533
Vec<ParsedMacroArg>,
534534
/// Something after the closing delimiter and the repeat token, if available.
@@ -537,7 +537,7 @@ enum MacroArgKind {
537537
Token,
538538
),
539539
/// e.g., `[derive(Debug)]`
540-
Delimited(DelimToken, Vec<ParsedMacroArg>),
540+
Delimited(Delimiter, Vec<ParsedMacroArg>),
541541
/// A possible separator. e.g., `,` or `;`.
542542
Separator(String, String),
543543
/// Other random stuff that does not fit to other kinds.
@@ -547,22 +547,22 @@ enum MacroArgKind {
547547

548548
fn delim_token_to_str(
549549
context: &RewriteContext<'_>,
550-
delim_token: DelimToken,
550+
delim_token: Delimiter,
551551
shape: Shape,
552552
use_multiple_lines: bool,
553553
inner_is_empty: bool,
554554
) -> (String, String) {
555555
let (lhs, rhs) = match delim_token {
556-
DelimToken::Paren => ("(", ")"),
557-
DelimToken::Bracket => ("[", "]"),
558-
DelimToken::Brace => {
556+
Delimiter::Parenthesis => ("(", ")"),
557+
Delimiter::Bracket => ("[", "]"),
558+
Delimiter::Brace => {
559559
if inner_is_empty || use_multiple_lines {
560560
("{", "}")
561561
} else {
562562
("{ ", " }")
563563
}
564564
}
565-
DelimToken::NoDelim => unreachable!(),
565+
Delimiter::Invisible => unreachable!(),
566566
};
567567
if use_multiple_lines {
568568
let indent_str = shape.indent.to_string_with_newline(context.config);
@@ -583,8 +583,8 @@ impl MacroArgKind {
583583
fn starts_with_brace(&self) -> bool {
584584
matches!(
585585
*self,
586-
MacroArgKind::Repeat(DelimToken::Brace, _, _, _)
587-
| MacroArgKind::Delimited(DelimToken::Brace, _)
586+
MacroArgKind::Repeat(Delimiter::Brace, _, _, _)
587+
| MacroArgKind::Delimited(Delimiter::Brace, _)
588588
)
589589
}
590590

@@ -753,7 +753,7 @@ impl MacroArgParser {
753753
}
754754
}
755755

756-
fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: DelimToken) {
756+
fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: Delimiter) {
757757
self.result.push(ParsedMacroArg {
758758
kind: MacroArgKind::Delimited(delim, inner),
759759
});
@@ -763,7 +763,7 @@ impl MacroArgParser {
763763
fn add_repeat(
764764
&mut self,
765765
inner: Vec<ParsedMacroArg>,
766-
delim: DelimToken,
766+
delim: Delimiter,
767767
iter: &mut Cursor,
768768
) -> Option<()> {
769769
let mut buffer = String::new();
@@ -1083,18 +1083,18 @@ pub(crate) fn convert_try_mac(
10831083
}
10841084
}
10851085

1086-
pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> DelimToken {
1086+
pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> Delimiter {
10871087
let snippet = context.snippet(mac.span());
10881088
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
10891089
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
10901090
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
10911091

10921092
if paren_pos < bracket_pos && paren_pos < brace_pos {
1093-
DelimToken::Paren
1093+
Delimiter::Parenthesis
10941094
} else if bracket_pos < brace_pos {
1095-
DelimToken::Bracket
1095+
Delimiter::Bracket
10961096
} else {
1097-
DelimToken::Brace
1097+
Delimiter::Brace
10981098
}
10991099
}
11001100

@@ -1174,7 +1174,7 @@ struct Macro {
11741174
// rather than clone them, if we can make the borrowing work out.
11751175
struct MacroBranch {
11761176
span: Span,
1177-
args_paren_kind: DelimToken,
1177+
args_paren_kind: Delimiter,
11781178
args: TokenStream,
11791179
body: Span,
11801180
whole_body: Span,
@@ -1188,7 +1188,7 @@ impl MacroBranch {
11881188
multi_branch_style: bool,
11891189
) -> Option<String> {
11901190
// Only attempt to format function-like macros.
1191-
if self.args_paren_kind != DelimToken::Paren {
1191+
if self.args_paren_kind != Delimiter::Parenthesis {
11921192
// FIXME(#1539): implement for non-sugared macros.
11931193
return None;
11941194
}
@@ -1350,18 +1350,18 @@ fn rewrite_macro_with_items(
13501350
items: &[MacroArg],
13511351
macro_name: &str,
13521352
shape: Shape,
1353-
style: DelimToken,
1353+
style: Delimiter,
13541354
position: MacroPosition,
13551355
span: Span,
13561356
) -> Option<String> {
13571357
let (opener, closer) = match style {
1358-
DelimToken::Paren => ("(", ")"),
1359-
DelimToken::Bracket => ("[", "]"),
1360-
DelimToken::Brace => (" {", "}"),
1358+
Delimiter::Parenthesis => ("(", ")"),
1359+
Delimiter::Bracket => ("[", "]"),
1360+
Delimiter::Brace => (" {", "}"),
13611361
_ => return None,
13621362
};
13631363
let trailing_semicolon = match style {
1364-
DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
1364+
Delimiter::Parenthesis | Delimiter::Bracket if position == MacroPosition::Item => ";",
13651365
_ => "",
13661366
};
13671367

src/overflow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::cmp::min;
44

55
use itertools::Itertools;
6-
use rustc_ast::token::DelimToken;
6+
use rustc_ast::token::Delimiter;
77
use rustc_ast::{ast, ptr};
88
use rustc_span::Span;
99

@@ -297,11 +297,11 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>
297297
shape: Shape,
298298
span: Span,
299299
force_separator_tactic: Option<SeparatorTactic>,
300-
delim_token: Option<DelimToken>,
300+
delim_token: Option<Delimiter>,
301301
) -> Option<String> {
302302
let (lhs, rhs) = match delim_token {
303-
Some(DelimToken::Paren) => ("(", ")"),
304-
Some(DelimToken::Brace) => ("{", "}"),
303+
Some(Delimiter::Parenthesis) => ("(", ")"),
304+
Some(Delimiter::Brace) => ("{", "}"),
305305
_ => ("[", "]"),
306306
};
307307
Context::new(

src/parse/macros/cfg_if.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::panic::{catch_unwind, AssertUnwindSafe};
22

33
use rustc_ast::ast;
4-
use rustc_ast::token::{DelimToken, TokenKind};
4+
use rustc_ast::token::{Delimiter, TokenKind};
55
use rustc_parse::parser::ForceCollect;
66
use rustc_span::symbol::kw;
77

@@ -47,11 +47,11 @@ fn parse_cfg_if_inner<'a>(
4747
.map_err(|_| "Failed to parse attributes")?;
4848
}
4949

50-
if !parser.eat(&TokenKind::OpenDelim(DelimToken::Brace)) {
50+
if !parser.eat(&TokenKind::OpenDelim(Delimiter::Brace)) {
5151
return Err("Expected an opening brace");
5252
}
5353

54-
while parser.token != TokenKind::CloseDelim(DelimToken::Brace)
54+
while parser.token != TokenKind::CloseDelim(Delimiter::Brace)
5555
&& parser.token.kind != TokenKind::Eof
5656
{
5757
let item = match parser.parse_item(ForceCollect::No) {
@@ -70,7 +70,7 @@ fn parse_cfg_if_inner<'a>(
7070
}
7171
}
7272

73-
if !parser.eat(&TokenKind::CloseDelim(DelimToken::Brace)) {
73+
if !parser.eat(&TokenKind::CloseDelim(Delimiter::Brace)) {
7474
return Err("Expected a closing brace");
7575
}
7676

src/parse/macros/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::token::{DelimToken, TokenKind};
1+
use rustc_ast::token::{Delimiter, TokenKind};
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{ast, ptr};
44
use rustc_parse::parser::{ForceCollect, Parser};
@@ -81,7 +81,7 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
8181
&& parser.look_ahead(1, |t| {
8282
t.kind == TokenKind::Eof
8383
|| t.kind == TokenKind::Comma
84-
|| t.kind == TokenKind::CloseDelim(DelimToken::NoDelim)
84+
|| t.kind == TokenKind::CloseDelim(Delimiter::Invisible)
8585
})
8686
{
8787
parser.bump();
@@ -97,15 +97,15 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
9797
pub(crate) fn parse_macro_args(
9898
context: &RewriteContext<'_>,
9999
tokens: TokenStream,
100-
style: DelimToken,
100+
style: Delimiter,
101101
forced_bracket: bool,
102102
) -> Option<ParsedMacroArgs> {
103103
let mut parser = build_parser(context, tokens);
104104
let mut args = Vec::new();
105105
let mut vec_with_semi = false;
106106
let mut trailing_comma = false;
107107

108-
if DelimToken::Brace != style {
108+
if Delimiter::Brace != style {
109109
loop {
110110
if let Some(arg) = check_keyword(&mut parser) {
111111
args.push(arg);

src/visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::{Cell, RefCell};
22
use std::rc::Rc;
33

4-
use rustc_ast::{ast, token::DelimToken, visit, AstLike};
4+
use rustc_ast::{ast, token::Delimiter, visit, AstLike};
55
use rustc_data_structures::sync::Lrc;
66
use rustc_span::{symbol, BytePos, Pos, Span};
77

@@ -689,7 +689,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
689689
// with whitespace between the delimiters and trailing semi (i.e. `foo!(abc) ;`)
690690
// are formatted correctly.
691691
let (span, rewrite) = match macro_style(mac, &self.get_context()) {
692-
DelimToken::Bracket | DelimToken::Paren if MacroPosition::Item == pos => {
692+
Delimiter::Bracket | Delimiter::Parenthesis if MacroPosition::Item == pos => {
693693
let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
694694
let hi = self.snippet_provider.span_before(search_span, ";");
695695
let target_span = mk_sp(mac.span().lo(), hi + BytePos(1));

0 commit comments

Comments
 (0)