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.
This commit is contained in:
parent
1cbc45942d
commit
3e3a4192d8
33 changed files with 252 additions and 248 deletions
|
@ -11,9 +11,9 @@ use rustc_ast::ptr::P;
|
|||
use rustc_ast::token::{self, Delimiter};
|
||||
use rustc_ast::tokenstream::TokenStream;
|
||||
use rustc_ast::visit::{self, AssocCtxt, Visitor};
|
||||
use rustc_ast::{AssocItemKind, AstNodeWrapper, AttrStyle, AttrVec, ExprKind, ForeignItemKind};
|
||||
use rustc_ast::{HasAttrs, HasNodeId};
|
||||
use rustc_ast::{Inline, ItemKind, MacArgs, MacStmtStyle, MetaItemKind, ModKind};
|
||||
use rustc_ast::{AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, ExprKind};
|
||||
use rustc_ast::{ForeignItemKind, HasAttrs, HasNodeId};
|
||||
use rustc_ast::{Inline, ItemKind, MacStmtStyle, MetaItemKind, ModKind};
|
||||
use rustc_ast::{NestedMetaItem, NodeId, PatKind, StmtKind, TyKind};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_data_structures::map_in_place::MapInPlace;
|
||||
|
@ -654,7 +654,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||
ExpandResult::Ready(match invoc.kind {
|
||||
InvocationKind::Bang { mac, .. } => match ext {
|
||||
SyntaxExtensionKind::Bang(expander) => {
|
||||
let Ok(tok_result) = expander.expand(self.cx, span, mac.args.inner_tokens()) else {
|
||||
let Ok(tok_result) = expander.expand(self.cx, span, mac.args.tokens.clone()) else {
|
||||
return ExpandResult::Ready(fragment_kind.dummy(span));
|
||||
};
|
||||
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
|
||||
|
@ -662,7 +662,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||
SyntaxExtensionKind::LegacyBang(expander) => {
|
||||
let prev = self.cx.current_expansion.prior_type_ascription;
|
||||
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
|
||||
let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
|
||||
let tok_result = expander.expand(self.cx, span, mac.args.tokens.clone());
|
||||
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
|
||||
result
|
||||
} else {
|
||||
|
@ -706,7 +706,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
|||
_ => item.to_tokens(),
|
||||
};
|
||||
let attr_item = attr.unwrap_normal_item();
|
||||
if let MacArgs::Eq(..) = attr_item.args {
|
||||
if let AttrArgs::Eq(..) = attr_item.args {
|
||||
self.cx.span_err(span, "key-value macro attributes are not supported");
|
||||
}
|
||||
let inner_tokens = attr_item.args.inner_tokens();
|
||||
|
|
|
@ -577,7 +577,7 @@ pub fn compile_declarative_macro(
|
|||
|
||||
// Parse the macro_rules! invocation
|
||||
let (macro_rules, body) = match &def.kind {
|
||||
ast::ItemKind::MacroDef(def) => (def.macro_rules, def.body.inner_tokens()),
|
||||
ast::ItemKind::MacroDef(def) => (def.macro_rules, def.body.tokens.clone()),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
|
|
|
@ -291,7 +291,7 @@ fn ttdelim_span() {
|
|||
.unwrap();
|
||||
|
||||
let tts: Vec<_> = match expr.kind {
|
||||
ast::ExprKind::MacCall(ref mac) => mac.args.inner_tokens().into_trees().collect(),
|
||||
ast::ExprKind::MacCall(ref mac) => mac.args.tokens.clone().into_trees().collect(),
|
||||
_ => panic!("not a macro"),
|
||||
};
|
||||
|
||||
|
|
|
@ -16,7 +16,11 @@ pub fn placeholder(
|
|||
fn mac_placeholder() -> P<ast::MacCall> {
|
||||
P(ast::MacCall {
|
||||
path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None },
|
||||
args: P(ast::MacArgs::Empty),
|
||||
args: P(ast::DelimArgs {
|
||||
dspan: ast::tokenstream::DelimSpan::dummy(),
|
||||
delim: ast::MacDelimiter::Parenthesis,
|
||||
tokens: ast::tokenstream::TokenStream::new(Vec::new()),
|
||||
}),
|
||||
prior_type_ascription: None,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue