Skip to content

Commit 1343ffd

Browse files
committed
Split MacArgs in two.
`MacArgs` is an enum with three variants: `Empty`, `Delimited`, and `Eq`. It's used in two ways: - For representing attribute macro arguments (e.g. in `AttrItem`), where all three variants are used. - For representing function-like macros (e.g. in `MacCall` and `MacroDef`), where only the `Delimited` variant is used. In other words, `MacArgs` is used in two quite different places due to them having partial overlap. I find this makes the code hard to read. It also leads to various unreachable code paths, and allows invalid values (such as accidentally using `MacArgs::Empty` in a `MacCall`). This commit splits `MacArgs` in two: - `DelimArgs` is a new struct just for the "delimited arguments" case. It is now used in `MacCall` and `MacroDef`. - `AttrArgs` is a renaming of the old `MacArgs` enum for the attribute macro case. Its `Delimited` variant now contains a `DelimArgs`. Various other related things are renamed as well. These changes make the code clearer, avoids several unreachable paths, and disallows the invalid values.
1 parent 4a4addc commit 1343ffd

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ pub(crate) fn can_be_overflowed_expr(
13411341
}
13421342
ast::ExprKind::MacCall(ref mac) => {
13431343
match (
1344-
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim().unwrap()),
1344+
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim.to_token()),
13451345
context.config.overflow_delimited_expr(),
13461346
) {
13471347
(Some(ast::MacDelimiter::Bracket), true)

src/macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn rewrite_macro_inner(
208208
original_style
209209
};
210210

211-
let ts = mac.args.inner_tokens();
211+
let ts = mac.args.tokens.clone();
212212
let has_comment = contains_comment(context.snippet(mac.span()));
213213
if ts.is_empty() && !has_comment {
214214
return match style {
@@ -392,7 +392,7 @@ pub(crate) fn rewrite_macro_def(
392392
return snippet;
393393
}
394394

395-
let ts = def.body.inner_tokens();
395+
let ts = def.body.tokens.clone();
396396
let mut parser = MacroParser::new(ts.into_trees());
397397
let parsed_def = match parser.parse() {
398398
Some(def) => def,
@@ -1087,7 +1087,7 @@ pub(crate) fn convert_try_mac(
10871087
) -> Option<ast::Expr> {
10881088
let path = &pprust::path_to_string(&mac.path);
10891089
if path == "try" || path == "r#try" {
1090-
let ts = mac.args.inner_tokens();
1090+
let ts = mac.args.tokens.clone();
10911091

10921092
Some(ast::Expr {
10931093
id: ast::NodeId::root(), // dummy value

src/parse/macros/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::rewrite::RewriteContext;
55

66
#[allow(dead_code)]
77
pub(crate) fn parse_asm(context: &RewriteContext<'_>, mac: &ast::MacCall) -> Option<AsmArgs> {
8-
let ts = mac.args.inner_tokens();
8+
let ts = mac.args.tokens.clone();
99
let mut parser = super::build_parser(context, ts);
1010
parse_asm_args(&mut parser, context.parse_sess.inner(), mac.span(), false).ok()
1111
}

src/parse/macros/cfg_if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn parse_cfg_if_inner<'a>(
2323
sess: &'a ParseSess,
2424
mac: &'a ast::MacCall,
2525
) -> Result<Vec<ast::Item>, &'static str> {
26-
let ts = mac.args.inner_tokens();
26+
let ts = mac.args.tokens.clone();
2727
let mut parser = build_stream_parser(sess.inner(), ts);
2828

2929
let mut items = vec![];

0 commit comments

Comments
 (0)