Skip to content

Commit 269cf50

Browse files
committed
Auto merge of #45540 - virgil-palanciuc:master, r=estebank
Avoid repetition on “use of unstable library feature 'rustc_private'” This PR fixes the error by only emitting it when the span contains a real file (is not inside a macro) - and making sure it's emitted only once per span. The first check was needed because spans-within-macros seem to differ a lot and "fixing" them to the real location is not trivial (and the method that does this is private to another module). It also feels like there always will be an error on import, with the real file name, so not sure there's a point to re-emit the same error at macro use. Fix #44953.
2 parents 2e6a1a9 + bb0049b commit 269cf50

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/librustc/middle/stability.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use hir::def::Def;
1818
use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
1919
use ty::{self, TyCtxt};
2020
use middle::privacy::AccessLevels;
21+
use session::DiagnosticMessageId;
2122
use syntax::symbol::Symbol;
22-
use syntax_pos::{Span, DUMMY_SP};
23+
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
2324
use syntax::ast;
2425
use syntax::ast::{NodeId, Attribute};
2526
use syntax::feature_gate::{GateIssue, emit_feature_err, find_lang_feature_accepted_version};
@@ -597,8 +598,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
597598
feature.as_str(), &r),
598599
None => format!("use of unstable library feature '{}'", &feature)
599600
};
600-
emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span,
601-
GateIssue::Library(Some(issue)), &msg);
601+
602+
603+
let msp: MultiSpan = span.into();
604+
let cm = &self.sess.parse_sess.codemap();
605+
let span_key = msp.primary_span().and_then(|sp: Span|
606+
if sp != DUMMY_SP {
607+
let file = cm.lookup_char_pos(sp.lo()).file;
608+
if file.name.starts_with("<") && file.name.ends_with(" macros>") {
609+
None
610+
} else {
611+
Some(span)
612+
}
613+
} else {
614+
None
615+
}
616+
);
617+
618+
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
619+
let fresh = self.sess.one_time_diagnostics.borrow_mut().insert(error_id);
620+
if fresh {
621+
emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span,
622+
GateIssue::Library(Some(issue)), &msg);
623+
}
602624
}
603625
Some(_) => {
604626
// Stable APIs are always ok to call and deprecated APIs are

src/librustc/session/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub struct Session {
7575
pub working_dir: (String, bool),
7676
pub lint_store: RefCell<lint::LintStore>,
7777
pub buffered_lints: RefCell<Option<lint::LintBuffer>>,
78-
/// Set of (LintId, Option<Span>, message) tuples tracking lint
78+
/// Set of (DiagnosticId, Option<Span>, message) tuples tracking
7979
/// (sub)diagnostics that have been set once, but should not be set again,
80-
/// in order to avoid redundantly verbose output (Issue #24690).
81-
pub one_time_diagnostics: RefCell<FxHashSet<(lint::LintId, Option<Span>, String)>>,
80+
/// in order to avoid redundantly verbose output (Issue #24690, #44953).
81+
pub one_time_diagnostics: RefCell<FxHashSet<(DiagnosticMessageId, Option<Span>, String)>>,
8282
pub plugin_llvm_passes: RefCell<Vec<String>>,
8383
pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
8484
pub crate_types: RefCell<Vec<config::CrateType>>,
@@ -164,6 +164,13 @@ enum DiagnosticBuilderMethod {
164164
// add more variants as needed to support one-time diagnostics
165165
}
166166

167+
/// Diagnostic message id - used in order to avoid emitting the same message more than once
168+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
169+
pub enum DiagnosticMessageId {
170+
LintId(lint::LintId),
171+
StabilityId(u32)
172+
}
173+
167174
impl Session {
168175
pub fn local_crate_disambiguator(&self) -> CrateDisambiguator {
169176
match *self.crate_disambiguator.borrow() {
@@ -360,7 +367,7 @@ impl Session {
360367
do_method()
361368
},
362369
_ => {
363-
let lint_id = lint::LintId::of(lint);
370+
let lint_id = DiagnosticMessageId::LintId(lint::LintId::of(lint));
364371
let id_span_message = (lint_id, span, message.to_owned());
365372
let fresh = self.one_time_diagnostics.borrow_mut().insert(id_span_message);
366373
if fresh {

0 commit comments

Comments
 (0)