Skip to content

Commit d9ee97e

Browse files
committed
resolve: Use standard stability diagnostics for macros
1 parent 3542995 commit d9ee97e

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

src/librustc/middle/stability.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,36 @@ pub fn provide(providers: &mut Providers<'_>) {
477477
};
478478
}
479479

480+
pub fn report_unstable(
481+
sess: &Session, feature: Symbol, reason: Option<Symbol>, issue: u32, span: Span
482+
) {
483+
let msg = match reason {
484+
Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
485+
None => format!("use of unstable library feature '{}'", &feature)
486+
};
487+
488+
let msp: MultiSpan = span.into();
489+
let cm = &sess.parse_sess.source_map();
490+
let span_key = msp.primary_span().and_then(|sp: Span|
491+
if !sp.is_dummy() {
492+
let file = cm.lookup_char_pos(sp.lo()).file;
493+
if file.name.is_macros() {
494+
None
495+
} else {
496+
Some(span)
497+
}
498+
} else {
499+
None
500+
}
501+
);
502+
503+
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
504+
let fresh = sess.one_time_diagnostics.borrow_mut().insert(error_id);
505+
if fresh {
506+
emit_feature_err(&sess.parse_sess, feature, span, GateIssue::Library(Some(issue)), &msg);
507+
}
508+
}
509+
480510
/// Checks whether an item marked with `deprecated(since="X")` is currently
481511
/// deprecated (i.e., whether X is not greater than the current rustc version).
482512
pub fn deprecation_in_effect(since: &str) -> bool {
@@ -715,34 +745,8 @@ impl<'tcx> TyCtxt<'tcx> {
715745
pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
716746
match self.eval_stability(def_id, id, span) {
717747
EvalResult::Allow => {}
718-
EvalResult::Deny { feature, reason, issue } => {
719-
let msg = match reason {
720-
Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
721-
None => format!("use of unstable library feature '{}'", &feature)
722-
};
723-
724-
let msp: MultiSpan = span.into();
725-
let cm = &self.sess.parse_sess.source_map();
726-
let span_key = msp.primary_span().and_then(|sp: Span|
727-
if !sp.is_dummy() {
728-
let file = cm.lookup_char_pos(sp.lo()).file;
729-
if file.name.is_macros() {
730-
None
731-
} else {
732-
Some(span)
733-
}
734-
} else {
735-
None
736-
}
737-
);
738-
739-
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
740-
let fresh = self.sess.one_time_diagnostics.borrow_mut().insert(error_id);
741-
if fresh {
742-
emit_feature_err(&self.sess.parse_sess, feature, span,
743-
GateIssue::Library(Some(issue)), &msg);
744-
}
745-
}
748+
EvalResult::Deny { feature, reason, issue } =>
749+
report_unstable(self.sess, feature, reason, issue, span),
746750
EvalResult::Unmarked => {
747751
// The API could be uncallable for other reasons, for example when a private module
748752
// was referenced.

src/librustc_resolve/macros.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::resolve_imports::ImportResolver;
99
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
1010
use rustc::hir::def::{self, DefKind, NonMacroAttrKind};
1111
use rustc::hir::map::{self, DefCollector};
12+
use rustc::middle::stability;
1213
use rustc::{ty, lint, span_bug};
1314
use syntax::ast::{self, Ident};
1415
use syntax::attr::{self, StabilityLevel};
@@ -18,7 +19,7 @@ use syntax::ext::base::{MacroKind, SyntaxExtension};
1819
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
1920
use syntax::ext::hygiene::Mark;
2021
use syntax::ext::tt::macro_rules;
21-
use syntax::feature_gate::{feature_err, emit_feature_err, is_builtin_attr_name};
22+
use syntax::feature_gate::{feature_err, is_builtin_attr_name};
2223
use syntax::feature_gate::{AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES};
2324
use syntax::symbol::{Symbol, kw, sym};
2425
use syntax::visit::Visitor;
@@ -237,13 +238,11 @@ impl<'a> base::Resolver for Resolver<'a> {
237238
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, &format));
238239

239240
if let Some(stability) = ext.stability {
240-
if let StabilityLevel::Unstable { issue, .. } = stability.level {
241-
let features = self.session.features_untracked();
242-
if !span.allows_unstable(stability.feature) &&
243-
features.declared_lib_features.iter().all(|(feat, _)| *feat != stability.feature) {
244-
let msg = format!("macro {}! is unstable", path);
245-
emit_feature_err(&self.session.parse_sess, stability.feature, span,
246-
GateIssue::Library(Some(issue)), &msg);
241+
if let StabilityLevel::Unstable { reason, issue } = stability.level {
242+
let (feature, features) = (stability.feature, self.session.features_untracked());
243+
if !span.allows_unstable(feature) &&
244+
features.declared_lib_features.iter().all(|(feat, _)| *feat != feature) {
245+
stability::report_unstable(self.session, feature, reason, issue, span);
247246
}
248247
}
249248
}

src/test/ui/macros/macro-stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
macro_rules! local_unstable { () => () }
88

99
fn main() {
10-
local_unstable!(); //~ ERROR: macro local_unstable! is unstable
11-
unstable_macro!(); //~ ERROR: macro unstable_macro! is unstable
10+
local_unstable!(); //~ ERROR use of unstable library feature 'local_unstable'
11+
unstable_macro!(); //~ ERROR use of unstable library feature 'unstable_macros'
1212
}

src/test/ui/macros/macro-stability.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0658]: macro local_unstable! is unstable
1+
error[E0658]: use of unstable library feature 'local_unstable'
22
--> $DIR/macro-stability.rs:10:5
33
|
44
LL | local_unstable!();
55
| ^^^^^^^^^^^^^^^^^^
66
|
77
= help: add #![feature(local_unstable)] to the crate attributes to enable
88

9-
error[E0658]: macro unstable_macro! is unstable
9+
error[E0658]: use of unstable library feature 'unstable_macros'
1010
--> $DIR/macro-stability.rs:11:5
1111
|
1212
LL | unstable_macro!();

0 commit comments

Comments
 (0)