Skip to content

Commit 374a80a

Browse files
committed
expand: It's always possible to create a dummy AST fragment
Remove a bunch of `Option`s that assumed that dummy fragment creation could fail. The test output changed due to not performing the expansion in `fn expand_invoc` in case of the recursion limit hit.
1 parent eac900a commit 374a80a

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ ast_fragments! {
158158
}
159159

160160
impl AstFragmentKind {
161-
fn dummy(self, span: Span) -> Option<AstFragment> {
162-
self.make_from(DummyResult::any(span))
161+
fn dummy(self, span: Span) -> AstFragment {
162+
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
163163
}
164164

165165
fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I)
@@ -327,10 +327,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
327327

328328
// FIXME(jseyfried): Refactor out the following logic
329329
let (expanded_fragment, new_invocations) = if let Some(ext) = ext {
330-
let (invoc_fragment_kind, invoc_span) = (invoc.fragment_kind, invoc.span());
331-
let fragment = self.expand_invoc(invoc, &ext).unwrap_or_else(|| {
332-
invoc_fragment_kind.dummy(invoc_span).unwrap()
333-
});
330+
let fragment = self.expand_invoc(invoc, &ext);
334331
self.collect_invocations(fragment, &[])
335332
} else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind {
336333
if !item.derive_allowed() {
@@ -477,7 +474,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
477474
}
478475
}
479476

480-
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<AstFragment> {
477+
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> AstFragment {
481478
if invoc.fragment_kind == AstFragmentKind::ForeignItems &&
482479
!self.cx.ecfg.macros_in_extern() {
483480
if let SyntaxExtensionKind::NonMacroAttr { .. } = ext.kind {} else {
@@ -487,12 +484,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
487484
}
488485
}
489486

490-
let result = match invoc.kind {
491-
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?,
492-
InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext)?,
493-
InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext)?,
494-
};
495-
496487
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
497488
let info = self.cx.current_expansion.mark.expn_info().unwrap();
498489
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
@@ -507,15 +498,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
507498
FatalError.raise();
508499
}
509500

510-
Some(result)
501+
match invoc.kind {
502+
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext),
503+
InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext),
504+
InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext),
505+
}
511506
}
512507

513508
fn expand_attr_invoc(&mut self,
514509
invoc: Invocation,
515510
ext: &SyntaxExtension)
516-
-> Option<AstFragment> {
511+
-> AstFragment {
517512
let (attr, mut item) = match invoc.kind {
518-
InvocationKind::Attr { attr, item, .. } => (attr?, item),
513+
InvocationKind::Attr { attr: Some(attr), item, .. } => (attr, item),
519514
_ => unreachable!(),
520515
};
521516

@@ -526,13 +521,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
526521
attr::mark_used(&attr);
527522
}
528523
item.visit_attrs(|attrs| attrs.push(attr));
529-
Some(invoc.fragment_kind.expect_from_annotatables(iter::once(item)))
524+
invoc.fragment_kind.expect_from_annotatables(iter::once(item))
530525
}
531526
SyntaxExtensionKind::LegacyAttr(expander) => {
532-
let meta = attr.parse_meta(self.cx.parse_sess)
533-
.map_err(|mut e| { e.emit(); }).ok()?;
534-
let item = expander.expand(self.cx, attr.span, &meta, item);
535-
Some(invoc.fragment_kind.expect_from_annotatables(item))
527+
match attr.parse_meta(self.cx.parse_sess) {
528+
Ok(meta) => {
529+
let item = expander.expand(self.cx, attr.span, &meta, item);
530+
invoc.fragment_kind.expect_from_annotatables(item)
531+
}
532+
Err(mut err) => {
533+
err.emit();
534+
invoc.fragment_kind.dummy(attr.span)
535+
}
536+
}
536537
}
537538
SyntaxExtensionKind::Attr(expander) => {
538539
self.gate_proc_macro_attr_item(attr.span, &item);
@@ -598,14 +599,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
598599
);
599600
}
600601

601-
fn gate_proc_macro_expansion(&self, span: Span, fragment: &Option<AstFragment>) {
602+
fn gate_proc_macro_expansion(&self, span: Span, fragment: &AstFragment) {
602603
if self.cx.ecfg.proc_macro_hygiene() {
603604
return
604605
}
605-
let fragment = match fragment {
606-
Some(fragment) => fragment,
607-
None => return,
608-
};
609606

610607
fragment.visit_with(&mut DisallowMacros {
611608
span,
@@ -641,15 +638,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
641638
fn expand_bang_invoc(&mut self,
642639
invoc: Invocation,
643640
ext: &SyntaxExtension)
644-
-> Option<AstFragment> {
641+
-> AstFragment {
645642
let kind = invoc.fragment_kind;
646643
let (mac, span) = match invoc.kind {
647644
InvocationKind::Bang { mac, span } => (mac, span),
648645
_ => unreachable!(),
649646
};
650647
let path = &mac.node.path;
651648

652-
let opt_expanded = match &ext.kind {
649+
match &ext.kind {
653650
SyntaxExtensionKind::Bang(expander) => {
654651
self.gate_proc_macro_expansion_kind(span, kind);
655652
let tok_result = expander.expand(self.cx, span, mac.node.stream());
@@ -659,19 +656,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
659656
}
660657
SyntaxExtensionKind::LegacyBang(expander) => {
661658
let tok_result = expander.expand(self.cx, span, mac.node.stream());
662-
kind.make_from(tok_result)
659+
if let Some(result) = kind.make_from(tok_result) {
660+
result
661+
} else {
662+
let msg = format!("non-{kind} macro in {kind} position: {name}",
663+
name = path.segments[0].ident.name, kind = kind.name());
664+
self.cx.span_err(path.span, &msg);
665+
self.cx.trace_macros_diag();
666+
kind.dummy(span)
667+
}
663668
}
664669
_ => unreachable!()
665-
};
666-
667-
if opt_expanded.is_some() {
668-
opt_expanded
669-
} else {
670-
let msg = format!("non-{kind} macro in {kind} position: {name}",
671-
name = path.segments[0].ident.name, kind = kind.name());
672-
self.cx.span_err(path.span, &msg);
673-
self.cx.trace_macros_diag();
674-
kind.dummy(span)
675670
}
676671
}
677672

@@ -703,7 +698,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
703698
fn expand_derive_invoc(&mut self,
704699
invoc: Invocation,
705700
ext: &SyntaxExtension)
706-
-> Option<AstFragment> {
701+
-> AstFragment {
707702
let (path, item) = match invoc.kind {
708703
InvocationKind::Derive { path, item, item_with_markers } => match ext.kind {
709704
SyntaxExtensionKind::LegacyDerive(..) => (path, item_with_markers),
@@ -712,7 +707,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
712707
_ => unreachable!(),
713708
};
714709
if !item.derive_allowed() {
715-
return None;
710+
return invoc.fragment_kind.dummy(path.span);
716711
}
717712

718713
match &ext.kind {
@@ -721,7 +716,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
721716
let meta = ast::MetaItem { node: ast::MetaItemKind::Word, span: path.span, path };
722717
let span = meta.span.with_ctxt(self.cx.backtrace());
723718
let items = expander.expand(self.cx, span, &meta, item);
724-
Some(invoc.fragment_kind.expect_from_annotatables(items))
719+
invoc.fragment_kind.expect_from_annotatables(items)
725720
}
726721
_ => unreachable!()
727722
}
@@ -732,12 +727,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
732727
kind: AstFragmentKind,
733728
path: &Path,
734729
span: Span)
735-
-> Option<AstFragment> {
730+
-> AstFragment {
736731
let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::<Vec<_>>());
737732
match parser.parse_ast_fragment(kind, false) {
738733
Ok(fragment) => {
739734
parser.ensure_complete_parse(path, kind.name(), span);
740-
Some(fragment)
735+
fragment
741736
}
742737
Err(mut err) => {
743738
err.set_span(span);

src/test/ui/macros/trace_faulty_macros.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ LL | my_recursive_macro!();
4545
= note: to `my_recursive_macro ! ( ) ;`
4646
= note: expanding `my_recursive_macro! { }`
4747
= note: to `my_recursive_macro ! ( ) ;`
48-
= note: expanding `my_recursive_macro! { }`
49-
= note: to `my_recursive_macro ! ( ) ;`
5048

5149
error: aborting due to 2 previous errors
5250

0 commit comments

Comments
 (0)