Skip to content

Commit baddce5

Browse files
committed
expand: Move "derive containers" into a separate InvocationKind variant
`InvocationKind::Attr { attr: None, .. }` meaning something entirely different from a regular attribute was confusing as hell.
1 parent b003dd6 commit baddce5

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/librustc_resolve/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ impl<'a> base::Resolver for Resolver<'a> {
221221
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
222222
-> Result<Option<Lrc<SyntaxExtension>>, Indeterminate> {
223223
let (path, kind, derives_in_scope, after_derive) = match invoc.kind {
224-
InvocationKind::Attr { attr: None, .. } =>
225-
return Ok(None),
226-
InvocationKind::Attr { attr: Some(ref attr), ref traits, after_derive, .. } =>
227-
(&attr.path, MacroKind::Attr, traits.clone(), after_derive),
224+
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
225+
(&attr.path, MacroKind::Attr, derives.clone(), after_derive),
228226
InvocationKind::Bang { ref mac, .. } =>
229227
(&mac.node.path, MacroKind::Bang, Vec::new(), false),
230228
InvocationKind::Derive { ref path, .. } =>
231229
(path, MacroKind::Derive, Vec::new(), false),
230+
InvocationKind::DeriveContainer { .. } =>
231+
return Ok(None),
232232
};
233233

234234
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);

src/libsyntax/ext/expand.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ pub enum InvocationKind {
199199
span: Span,
200200
},
201201
Attr {
202-
attr: Option<ast::Attribute>,
203-
traits: Vec<Path>,
202+
attr: ast::Attribute,
204203
item: Annotatable,
204+
// Required for resolving derive helper attributes.
205+
derives: Vec<Path>,
205206
// We temporarily report errors for attribute macros placed after derives
206207
after_derive: bool,
207208
},
@@ -210,15 +211,22 @@ pub enum InvocationKind {
210211
item: Annotatable,
211212
item_with_markers: Annotatable,
212213
},
214+
/// "Invocation" that contains all derives from an item,
215+
/// broken into multiple `Derive` invocations when expanded.
216+
/// FIXME: Find a way to remove it.
217+
DeriveContainer {
218+
derives: Vec<Path>,
219+
item: Annotatable,
220+
},
213221
}
214222

215223
impl Invocation {
216224
pub fn span(&self) -> Span {
217-
match self.kind {
218-
InvocationKind::Bang { span, .. } => span,
219-
InvocationKind::Attr { attr: Some(ref attr), .. } => attr.span,
220-
InvocationKind::Attr { attr: None, .. } => DUMMY_SP,
221-
InvocationKind::Derive { ref path, .. } => path.span,
225+
match &self.kind {
226+
InvocationKind::Bang { span, .. } => *span,
227+
InvocationKind::Attr { attr, .. } => attr.span,
228+
InvocationKind::Derive { path, .. } => path.span,
229+
InvocationKind::DeriveContainer { item, .. } => item.span(),
222230
}
223231
}
224232
}
@@ -329,7 +337,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
329337
let (expanded_fragment, new_invocations) = if let Some(ext) = ext {
330338
let fragment = self.expand_invoc(invoc, &ext.kind);
331339
self.collect_invocations(fragment, &[])
332-
} else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind {
340+
} else if let InvocationKind::DeriveContainer { derives: traits, item } = invoc.kind {
333341
if !item.derive_allowed() {
334342
let attr = attr::find_by_name(item.attrs(), sym::derive)
335343
.expect("`derive` attribute should exist");
@@ -522,7 +530,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
522530
}
523531
_ => unreachable!()
524532
}
525-
InvocationKind::Attr { attr: Some(attr), mut item, .. } => match ext {
533+
InvocationKind::Attr { attr, mut item, .. } => match ext {
526534
SyntaxExtensionKind::Attr(expander) => {
527535
self.gate_proc_macro_attr_item(span, &item);
528536
let item_tok = TokenTree::token(token::Interpolated(Lrc::new(match item {
@@ -578,7 +586,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
578586
}
579587
_ => unreachable!()
580588
}
581-
_ => unreachable!()
589+
InvocationKind::DeriveContainer { .. } => unreachable!()
582590
}
583591
}
584592

@@ -805,10 +813,10 @@ struct InvocationCollector<'a, 'b> {
805813
impl<'a, 'b> InvocationCollector<'a, 'b> {
806814
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
807815
// Expansion info for all the collected invocations is set upon their resolution,
808-
// with exception of the "derive container" case which is not resolved and can get
816+
// with exception of the derive container case which is not resolved and can get
809817
// its expansion info immediately.
810818
let expn_info = match &kind {
811-
InvocationKind::Attr { attr: None, item, .. } => Some(ExpnInfo::default(
819+
InvocationKind::DeriveContainer { item, .. } => Some(ExpnInfo::default(
812820
ExpnKind::Macro(MacroKind::Attr, sym::derive),
813821
item.span(), self.cx.parse_sess.edition,
814822
)),
@@ -833,12 +841,15 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
833841

834842
fn collect_attr(&mut self,
835843
attr: Option<ast::Attribute>,
836-
traits: Vec<Path>,
844+
derives: Vec<Path>,
837845
item: Annotatable,
838846
kind: AstFragmentKind,
839847
after_derive: bool)
840848
-> AstFragment {
841-
self.collect(kind, InvocationKind::Attr { attr, traits, item, after_derive })
849+
self.collect(kind, match attr {
850+
Some(attr) => InvocationKind::Attr { attr, item, derives, after_derive },
851+
None => InvocationKind::DeriveContainer { derives, item },
852+
})
842853
}
843854

844855
fn find_attr_invoc(&self, attrs: &mut Vec<ast::Attribute>, after_derive: &mut bool)

0 commit comments

Comments
 (0)