1 //! A pass that annotates every item and method with its stability level,
2 //! propagating default levels lexically from parent to children ast nodes.
4 pub use self::StabilityLevel::*;
6 use crate::ty::{self, TyCtxt};
7 use rustc_ast::ast::CRATE_NODE_ID;
8 use rustc_attr::{self as attr, ConstStability, Deprecation, Stability};
9 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10 use rustc_errors::{Applicability, DiagnosticBuilder};
11 use rustc_feature::GateIssue;
13 use rustc_hir::def::DefKind;
14 use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
15 use rustc_hir::{self, HirId};
16 use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
17 use rustc_session::lint::{BuiltinLintDiagnostics, Lint, LintBuffer};
18 use rustc_session::parse::feature_err_issue;
19 use rustc_session::{DiagnosticMessageId, Session};
20 use rustc_span::symbol::{sym, Symbol};
21 use rustc_span::{MultiSpan, Span};
23 use std::num::NonZeroU32;
25 #[derive(PartialEq, Clone, Copy, Debug)]
26 pub enum StabilityLevel {
32 pub fn from_attr_level(level: &attr::StabilityLevel) -> Self {
33 if level.is_stable() { Stable } else { Unstable }
37 /// An entry in the `depr_map`.
38 #[derive(Clone, HashStable)]
39 pub struct DeprecationEntry {
40 /// The metadata of the attribute associated with this entry.
41 pub attr: Deprecation,
42 /// The `DefId` where the attr was originally attached. `None` for non-local
44 origin: Option<HirId>,
47 impl DeprecationEntry {
48 pub fn local(attr: Deprecation, id: HirId) -> DeprecationEntry {
49 DeprecationEntry { attr, origin: Some(id) }
52 pub fn external(attr: Deprecation) -> DeprecationEntry {
53 DeprecationEntry { attr, origin: None }
56 pub fn same_origin(&self, other: &DeprecationEntry) -> bool {
57 match (self.origin, other.origin) {
58 (Some(o1), Some(o2)) => o1 == o2,
64 /// A stability index, giving the stability level for items and methods.
66 pub struct Index<'tcx> {
67 /// This is mostly a cache, except the stabilities of local items
68 /// are filled by the annotator.
69 pub stab_map: FxHashMap<HirId, &'tcx Stability>,
70 pub const_stab_map: FxHashMap<HirId, &'tcx ConstStability>,
71 pub depr_map: FxHashMap<HirId, DeprecationEntry>,
73 /// Maps for each crate whether it is part of the staged API.
74 pub staged_api: FxHashMap<CrateNum, bool>,
76 /// Features enabled for this crate.
77 pub active_features: FxHashSet<Symbol>,
80 impl<'tcx> Index<'tcx> {
81 pub fn local_stability(&self, id: HirId) -> Option<&'tcx Stability> {
82 self.stab_map.get(&id).cloned()
85 pub fn local_const_stability(&self, id: HirId) -> Option<&'tcx ConstStability> {
86 self.const_stab_map.get(&id).cloned()
89 pub fn local_deprecation_entry(&self, id: HirId) -> Option<DeprecationEntry> {
90 self.depr_map.get(&id).cloned()
94 pub fn report_unstable(
97 reason: Option<Symbol>,
98 issue: Option<NonZeroU32>,
101 soft_handler: impl FnOnce(&'static Lint, Span, &str),
103 let msg = match reason {
104 Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
105 None => format!("use of unstable library feature '{}'", &feature),
108 let msp: MultiSpan = span.into();
109 let sm = &sess.parse_sess.source_map();
110 let span_key = msp.primary_span().and_then(|sp: Span| {
112 let file = sm.lookup_char_pos(sp.lo()).file;
113 if file.is_imported() { None } else { Some(span) }
119 let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
120 let fresh = sess.one_time_diagnostics.borrow_mut().insert(error_id);
123 soft_handler(SOFT_UNSTABLE, span, &msg)
125 feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg)
131 /// Checks whether an item marked with `deprecated(since="X")` is currently
132 /// deprecated (i.e., whether X is not greater than the current rustc version).
133 pub fn deprecation_in_effect(is_since_rustc_version: bool, since: Option<&str>) -> bool {
134 let since = if let Some(since) = since {
135 if is_since_rustc_version {
138 // We assume that the deprecation is in effect if it's not a
143 // If since attribute is not set, then we're definitely in effect.
146 fn parse_version(ver: &str) -> Vec<u32> {
147 // We ignore non-integer components of the version (e.g., "nightly").
148 ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
151 if let Some(rustc) = option_env!("CFG_RELEASE") {
152 let since: Vec<u32> = parse_version(&since);
153 let rustc: Vec<u32> = parse_version(rustc);
154 // We simply treat invalid `since` attributes as relating to a previous
155 // Rust version, thus always displaying the warning.
156 if since.len() != 3 {
161 // By default, a deprecation warning applies to
162 // the current version of the compiler.
167 pub fn deprecation_suggestion(
168 diag: &mut DiagnosticBuilder<'_>,
170 suggestion: Option<Symbol>,
173 if let Some(suggestion) = suggestion {
174 diag.span_suggestion(
176 &format!("replace the use of the deprecated {}", kind),
177 suggestion.to_string(),
178 Applicability::MachineApplicable,
183 pub fn deprecation_message(depr: &Deprecation, kind: &str, path: &str) -> (String, &'static Lint) {
184 let (message, lint) = if deprecation_in_effect(
185 depr.is_since_rustc_version,
186 depr.since.map(Symbol::as_str).as_deref(),
188 (format!("use of deprecated {} `{}`", kind, path), DEPRECATED)
192 "use of {} `{}` that will be deprecated in future version {}",
197 DEPRECATED_IN_FUTURE,
200 let message = match depr.note {
201 Some(reason) => format!("{}: {}", message, reason),
207 pub fn early_report_deprecation(
208 lint_buffer: &'a mut LintBuffer,
210 suggestion: Option<Symbol>,
214 if span.in_derive_expansion() {
218 let diag = BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span);
219 lint_buffer.buffer_lint_with_diagnostic(lint, CRATE_NODE_ID, span, message, diag);
222 fn late_report_deprecation(
225 suggestion: Option<Symbol>,
231 if span.in_derive_expansion() {
235 tcx.struct_span_lint_hir(lint, hir_id, span, |lint| {
236 let mut diag = lint.build(message);
237 if let hir::Node::Expr(_) = tcx.hir().get(hir_id) {
238 let kind = tcx.def_kind(def_id).descr(def_id);
239 deprecation_suggestion(&mut diag, kind, suggestion, span);
245 /// Result of `TyCtxt::eval_stability`.
246 pub enum EvalResult {
247 /// We can use the item because it is stable or we provided the
248 /// corresponding feature gate.
250 /// We cannot use the item because it is unstable and we did not provide the
251 /// corresponding feature gate.
252 Deny { feature: Symbol, reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
253 /// The item does not have the `#[stable]` or `#[unstable]` marker assigned.
258 fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool {
259 // Check if `def_id` is a trait method.
260 match tcx.def_kind(def_id) {
261 DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
262 if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
263 // Trait methods do not declare visibility (even
264 // for visibility info in cstore). Use containing
265 // trait instead, so methods of `pub` traits are
266 // themselves considered `pub`.
267 def_id = trait_def_id;
273 let visibility = tcx.visibility(def_id);
276 // Must check stability for `pub` items.
277 ty::Visibility::Public => false,
279 // These are not visible outside crate; therefore
280 // stability markers are irrelevant, if even present.
281 ty::Visibility::Restricted(..) | ty::Visibility::Invisible => true,
285 impl<'tcx> TyCtxt<'tcx> {
286 /// Evaluates the stability of an item.
288 /// Returns `EvalResult::Allow` if the item is stable, or unstable but the corresponding
289 /// `#![feature]` has been provided. Returns `EvalResult::Deny` which describes the offending
290 /// unstable feature otherwise.
292 /// If `id` is `Some(_)`, this function will also check if the item at `def_id` has been
293 /// deprecated. If the item is indeed deprecated, we will emit a deprecation lint attached to
295 pub fn eval_stability(self, def_id: DefId, id: Option<HirId>, span: Span) -> EvalResult {
296 // Deprecated attributes apply in-crate and cross-crate.
297 if let Some(id) = id {
298 if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
299 let parent_def_id = self.hir().local_def_id(self.hir().get_parent_item(id));
301 .lookup_deprecation_entry(parent_def_id.to_def_id())
302 .map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry));
304 // #[deprecated] doesn't emit a notice if we're not on the
305 // topmost deprecation. For example, if a struct is deprecated,
306 // the use of a field won't be linted.
308 // #[rustc_deprecated] however wants to emit down the whole
310 if !skip || depr_entry.attr.is_since_rustc_version {
311 let path = &self.def_path_str(def_id);
312 let kind = self.def_kind(def_id).descr(def_id);
313 let (message, lint) = deprecation_message(&depr_entry.attr, kind, path);
314 late_report_deprecation(
317 depr_entry.attr.suggestion,
328 self.lookup_stability(DefId { index: CRATE_DEF_INDEX, ..def_id }).is_some();
330 return EvalResult::Allow;
333 let stability = self.lookup_stability(def_id);
336 inspecting def_id={:?} span={:?} of stability={:?}",
337 def_id, span, stability
340 // Only the cross-crate scenario matters when checking unstable APIs
341 let cross_crate = !def_id.is_local();
343 return EvalResult::Allow;
346 // Issue #38412: private items lack stability markers.
347 if skip_stability_check_due_to_privacy(self, def_id) {
348 return EvalResult::Allow;
353 level: attr::Unstable { reason, issue, is_soft }, feature, ..
355 if span.allows_unstable(feature) {
356 debug!("stability: skipping span={:?} since it is internal", span);
357 return EvalResult::Allow;
359 if self.stability().active_features.contains(&feature) {
360 return EvalResult::Allow;
363 // When we're compiling the compiler itself we may pull in
364 // crates from crates.io, but those crates may depend on other
365 // crates also pulled in from crates.io. We want to ideally be
366 // able to compile everything without requiring upstream
367 // modifications, so in the case that this looks like a
368 // `rustc_private` crate (e.g., a compiler crate) and we also have
369 // the `-Z force-unstable-if-unmarked` flag present (we're
370 // compiling a compiler crate), then let this missing feature
372 if feature == sym::rustc_private && issue == NonZeroU32::new(27812) {
373 if self.sess.opts.debugging_opts.force_unstable_if_unmarked {
374 return EvalResult::Allow;
378 EvalResult::Deny { feature, reason, issue, is_soft }
381 // Stable APIs are always ok to call and deprecated APIs are
382 // handled by the lint emitting logic above.
385 None => EvalResult::Unmarked,
389 /// Checks if an item is stable or error out.
391 /// If the item defined by `def_id` is unstable and the corresponding `#![feature]` does not
392 /// exist, emits an error.
394 /// Additionally, this function will also check if the item is deprecated. If so, and `id` is
395 /// not `None`, a deprecated lint attached to `id` will be emitted.
396 pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
397 let soft_handler = |lint, span, msg: &_| {
398 self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, |lint| {
399 lint.build(msg).emit()
402 match self.eval_stability(def_id, id, span) {
403 EvalResult::Allow => {}
404 EvalResult::Deny { feature, reason, issue, is_soft } => {
405 report_unstable(self.sess, feature, reason, issue, is_soft, span, soft_handler)
407 EvalResult::Unmarked => {
408 // The API could be uncallable for other reasons, for example when a private module
410 self.sess.delay_span_bug(span, &format!("encountered unmarked API: {:?}", def_id));
415 pub fn lookup_deprecation(self, id: DefId) -> Option<Deprecation> {
416 self.lookup_deprecation_entry(id).map(|depr| depr.attr)