Skip to content

Commit 3b6370b

Browse files
committed
resolve/expand: Move macro stability checking to an earlier point
1 parent 03ac053 commit 3b6370b

File tree

3 files changed

+23
-45
lines changed

3 files changed

+23
-45
lines changed

src/librustc_resolve/macros.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,27 @@ impl<'a> base::Resolver for Resolver<'a> {
229229
Err(determinacy) => return Err(determinacy),
230230
};
231231

232+
let span = invoc.span();
232233
let format = match kind {
233234
MacroKind::Derive => format!("derive({})", fast_print_path(path)),
234235
_ => fast_print_path(path),
235236
};
236-
invoc.expansion_data.mark.set_expn_info(ext.expn_info(invoc.span(), &format));
237+
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, &format));
237238

238239
if let Res::Def(_, def_id) = res {
239240
if after_derive {
240-
self.session.span_err(invoc.span(),
241-
"macro attributes must be placed before `#[derive]`");
241+
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
242+
}
243+
if let Some((feature, issue)) = ext.unstable_feature {
244+
// Do not stability-check macros in the same crate.
245+
let features = self.session.features_untracked();
246+
if !def_id.is_local() &&
247+
!span.allows_unstable(feature) &&
248+
features.declared_lib_features.iter().all(|(feat, _)| *feat != feature) {
249+
let msg = format!("macro {}! is unstable", path);
250+
emit_feature_err(&self.session.parse_sess, feature, span,
251+
GateIssue::Library(Some(issue)), &msg);
252+
}
242253
}
243254
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
244255
let normal_module_def_id =

src/libsyntax/ext/base.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,6 @@ pub struct ExpansionData {
738738
pub depth: usize,
739739
pub module: Rc<ModuleData>,
740740
pub directory_ownership: DirectoryOwnership,
741-
pub crate_span: Option<Span>,
742741
}
743742

744743
/// One of these is made during expansion and incrementally updated as we go;
@@ -768,7 +767,6 @@ impl<'a> ExtCtxt<'a> {
768767
depth: 0,
769768
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
770769
directory_ownership: DirectoryOwnership::Owned { relative: None },
771-
crate_span: None,
772770
},
773771
expansions: FxHashMap::default(),
774772
}

src/libsyntax/ext/expand.rs

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
243243
module.directory.pop();
244244
self.cx.root_path = module.directory.clone();
245245
self.cx.current_expansion.module = Rc::new(module);
246-
self.cx.current_expansion.crate_span = Some(krate.span);
247246

248247
let orig_mod_span = krate.module.inner;
249248

@@ -668,39 +667,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
668667
};
669668
let path = &mac.node.path;
670669

671-
let validate = |this: &mut Self| {
672-
// feature-gate the macro invocation
673-
if let Some((feature, issue)) = ext.unstable_feature {
674-
let crate_span = this.cx.current_expansion.crate_span.unwrap();
675-
// don't stability-check macros in the same crate
676-
if !crate_span.contains(ext.span)
677-
&& !span.allows_unstable(feature)
678-
&& this.cx.ecfg.features.map_or(true, |feats| {
679-
// macro features will count as lib features
680-
!feats.declared_lib_features.iter().any(|&(feat, _)| feat == feature)
681-
}) {
682-
let explain = format!("macro {}! is unstable", path);
683-
emit_feature_err(this.cx.parse_sess, feature, span,
684-
GateIssue::Library(Some(issue)), &explain);
685-
this.cx.trace_macros_diag();
686-
}
687-
}
688-
689-
Ok(())
690-
};
691-
692670
let opt_expanded = match &ext.kind {
671+
SyntaxExtensionKind::Bang(expander) => {
672+
self.gate_proc_macro_expansion_kind(span, kind);
673+
let tok_result = expander.expand(self.cx, span, mac.node.stream());
674+
let result = self.parse_ast_fragment(tok_result, kind, path, span);
675+
self.gate_proc_macro_expansion(span, &result);
676+
result
677+
}
693678
SyntaxExtensionKind::LegacyBang(expander) => {
694-
if let Err(dummy_span) = validate(self) {
695-
dummy_span
696-
} else {
697-
kind.make_from(expander.expand(
698-
self.cx,
699-
span,
700-
mac.node.stream(),
701-
Some(ext.span),
702-
))
703-
}
679+
let tok_result = expander.expand(self.cx, span, mac.node.stream(), Some(ext.span));
680+
kind.make_from(tok_result)
704681
}
705682

706683
SyntaxExtensionKind::Attr(..) |
@@ -717,14 +694,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
717694
self.cx.trace_macros_diag();
718695
kind.dummy(span)
719696
}
720-
721-
SyntaxExtensionKind::Bang(expander) => {
722-
self.gate_proc_macro_expansion_kind(span, kind);
723-
let tok_result = expander.expand(self.cx, span, mac.node.stream());
724-
let result = self.parse_ast_fragment(tok_result, kind, path, span);
725-
self.gate_proc_macro_expansion(span, &result);
726-
result
727-
}
728697
};
729698

730699
if opt_expanded.is_some() {

0 commit comments

Comments
 (0)