diff options
| -rw-r--r-- | src/librustc_resolve/macros.rs | 17 | ||||
| -rw-r--r-- | src/libsyntax/ext/base.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 49 |
3 files changed, 23 insertions, 45 deletions
diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index e9c7a3add62..dcdf0be41c2 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -229,16 +229,27 @@ impl<'a> base::Resolver for Resolver<'a> { Err(determinacy) => return Err(determinacy), }; + let span = invoc.span(); let format = match kind { MacroKind::Derive => format!("derive({})", fast_print_path(path)), _ => fast_print_path(path), }; - invoc.expansion_data.mark.set_expn_info(ext.expn_info(invoc.span(), &format)); + invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, &format)); if let Res::Def(_, def_id) = res { if after_derive { - self.session.span_err(invoc.span(), - "macro attributes must be placed before `#[derive]`"); + self.session.span_err(span, "macro attributes must be placed before `#[derive]`"); + } + if let Some((feature, issue)) = ext.unstable_feature { + // Do not stability-check macros in the same crate. + let features = self.session.features_untracked(); + if !def_id.is_local() && + !span.allows_unstable(feature) && + features.declared_lib_features.iter().all(|(feat, _)| *feat != feature) { + let msg = format!("macro {}! is unstable", path); + emit_feature_err(&self.session.parse_sess, feature, span, + GateIssue::Library(Some(issue)), &msg); + } } self.macro_defs.insert(invoc.expansion_data.mark, def_id); let normal_module_def_id = diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 57dabf98d2c..bde989a464b 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -738,7 +738,6 @@ pub struct ExpansionData { pub depth: usize, pub module: Rc<ModuleData>, pub directory_ownership: DirectoryOwnership, - pub crate_span: Option<Span>, } /// One of these is made during expansion and incrementally updated as we go; @@ -768,7 +767,6 @@ impl<'a> ExtCtxt<'a> { depth: 0, module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }), directory_ownership: DirectoryOwnership::Owned { relative: None }, - crate_span: None, }, expansions: FxHashMap::default(), } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index da965eda61e..6be5988d03b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -243,7 +243,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { module.directory.pop(); self.cx.root_path = module.directory.clone(); self.cx.current_expansion.module = Rc::new(module); - self.cx.current_expansion.crate_span = Some(krate.span); let orig_mod_span = krate.module.inner; @@ -668,39 +667,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }; let path = &mac.node.path; - let validate = |this: &mut Self| { - // feature-gate the macro invocation - if let Some((feature, issue)) = ext.unstable_feature { - let crate_span = this.cx.current_expansion.crate_span.unwrap(); - // don't stability-check macros in the same crate - if !crate_span.contains(ext.span) - && !span.allows_unstable(feature) - && this.cx.ecfg.features.map_or(true, |feats| { - // macro features will count as lib features - !feats.declared_lib_features.iter().any(|&(feat, _)| feat == feature) - }) { - let explain = format!("macro {}! is unstable", path); - emit_feature_err(this.cx.parse_sess, feature, span, - GateIssue::Library(Some(issue)), &explain); - this.cx.trace_macros_diag(); - } - } - - Ok(()) - }; - let opt_expanded = match &ext.kind { + SyntaxExtensionKind::Bang(expander) => { + self.gate_proc_macro_expansion_kind(span, kind); + let tok_result = expander.expand(self.cx, span, mac.node.stream()); + let result = self.parse_ast_fragment(tok_result, kind, path, span); + self.gate_proc_macro_expansion(span, &result); + result + } SyntaxExtensionKind::LegacyBang(expander) => { - if let Err(dummy_span) = validate(self) { - dummy_span - } else { - kind.make_from(expander.expand( - self.cx, - span, - mac.node.stream(), - Some(ext.span), - )) - } + let tok_result = expander.expand(self.cx, span, mac.node.stream(), Some(ext.span)); + kind.make_from(tok_result) } SyntaxExtensionKind::Attr(..) | @@ -717,14 +694,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { self.cx.trace_macros_diag(); kind.dummy(span) } - - SyntaxExtensionKind::Bang(expander) => { - self.gate_proc_macro_expansion_kind(span, kind); - let tok_result = expander.expand(self.cx, span, mac.node.stream()); - let result = self.parse_ast_fragment(tok_result, kind, path, span); - self.gate_proc_macro_expansion(span, &result); - result - } }; if opt_expanded.is_some() { |
