From d98ac573a4d6d81a47d708daf85930462cecfbc2 Mon Sep 17 00:00:00 2001 From: Alik Aslanyan Date: Thu, 30 Sep 2021 15:15:10 +0400 Subject: [PATCH] Remove visible path calculation from allowed deprecation lint --- compiler/rustc_middle/src/middle/stability.rs | 114 ++++++++++++------ compiler/rustc_resolve/src/macros.rs | 2 +- src/librustdoc/html/render/mod.rs | 4 +- src/librustdoc/html/render/print_item.rs | 5 +- 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index f0b4b6b5a0c..597622b2ebf 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -15,12 +15,11 @@ use rustc_hir::{self, HirId}; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE}; -use rustc_session::lint::{BuiltinLintDiagnostics, Lint, LintBuffer}; +use rustc_session::lint::{BuiltinLintDiagnostics, Level, Lint, LintBuffer}; use rustc_session::parse::feature_err_issue; use rustc_session::{DiagnosticMessageId, Session}; use rustc_span::symbol::{sym, Symbol}; use rustc_span::{MultiSpan, Span}; - use std::num::NonZeroU32; #[derive(PartialEq, Clone, Copy, Debug)] @@ -125,7 +124,11 @@ pub fn report_unstable( /// Checks whether an item marked with `deprecated(since="X")` is currently /// deprecated (i.e., whether X is not greater than the current rustc version). -pub fn deprecation_in_effect(is_since_rustc_version: bool, since: Option<&str>) -> bool { +pub fn deprecation_in_effect(depr: &Deprecation) -> bool { + let is_since_rustc_version = depr.is_since_rustc_version; + let since = depr.since.map(Symbol::as_str); + let since = since.as_deref(); + fn parse_version(ver: &str) -> Vec { // We ignore non-integer components of the version (e.g., "nightly"). ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect() @@ -175,33 +178,50 @@ pub fn deprecation_suggestion( } } -pub fn deprecation_message(depr: &Deprecation, kind: &str, path: &str) -> (String, &'static Lint) { - let since = depr.since.map(Symbol::as_str); - let (message, lint) = if deprecation_in_effect(depr.is_since_rustc_version, since.as_deref()) { - (format!("use of deprecated {} `{}`", kind, path), DEPRECATED) +fn deprecation_lint(is_in_effect: bool) -> &'static Lint { + if is_in_effect { DEPRECATED } else { DEPRECATED_IN_FUTURE } +} + +fn deprecation_message( + is_in_effect: bool, + since: Option, + note: Option, + kind: &str, + path: &str, +) -> String { + let message = if is_in_effect { + format!("use of deprecated {} `{}`", kind, path) } else { - ( - if since.as_deref() == Some("TBD") { - format!( - "use of {} `{}` that will be deprecated in a future Rust version", - kind, path - ) - } else { - format!( - "use of {} `{}` that will be deprecated in future version {}", - kind, - path, - since.unwrap() - ) - }, - DEPRECATED_IN_FUTURE, - ) + let since = since.map(Symbol::as_str); + + if since.as_deref() == Some("TBD") { + format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path) + } else { + format!( + "use of {} `{}` that will be deprecated in future version {}", + kind, + path, + since.unwrap() + ) + } }; - let message = match depr.note { + + match note { Some(reason) => format!("{}: {}", message, reason), None => message, - }; - (message, lint) + } +} + +pub fn deprecation_message_and_lint( + depr: &Deprecation, + kind: &str, + path: &str, +) -> (String, &'static Lint) { + let is_in_effect = deprecation_in_effect(depr); + ( + deprecation_message(is_in_effect, depr.since, depr.note, kind, path), + deprecation_lint(is_in_effect), + ) } pub fn early_report_deprecation( @@ -303,20 +323,34 @@ pub fn eval_stability( // // #[rustc_deprecated] however wants to emit down the whole // hierarchy. - if !skip || depr_entry.attr.is_since_rustc_version { - let path = &with_no_trimmed_paths(|| self.def_path_str(def_id)); - let kind = self.def_kind(def_id).descr(def_id); - let (message, lint) = deprecation_message(&depr_entry.attr, kind, path); - late_report_deprecation( - self, - &message, - depr_entry.attr.suggestion, - lint, - span, - method_span, - id, - def_id, - ); + let depr_attr = &depr_entry.attr; + if !skip || depr_attr.is_since_rustc_version { + // Calculating message for lint involves calling `self.def_path_str`. + // Which by default to calculate visible path will invoke expensive `visible_parent_map` query. + // So we skip message calculation altogether, if lint is allowed. + let is_in_effect = deprecation_in_effect(depr_attr); + let lint = deprecation_lint(is_in_effect); + if self.lint_level_at_node(lint, id).0 != Level::Allow { + let def_path = &with_no_trimmed_paths(|| self.def_path_str(def_id)); + let def_kind = self.def_kind(def_id).descr(def_id); + + late_report_deprecation( + self, + &deprecation_message( + is_in_effect, + depr_attr.since, + depr_attr.note, + def_kind, + def_path, + ), + depr_attr.suggestion, + lint, + span, + method_span, + id, + def_id, + ); + } } }; } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index f15cf4bbc3a..4f6e23d8f84 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -1137,7 +1137,7 @@ fn check_stability_and_deprecation( } if let Some(depr) = &ext.deprecation { let path = pprust::path_to_string(&path); - let (message, lint) = stability::deprecation_message(depr, "macro", &path); + let (message, lint) = stability::deprecation_message_and_lint(depr, "macro", &path); stability::early_report_deprecation( &mut self.lint_buffer, &message, diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 1f27357f6c6..5045a99800a 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -599,14 +599,14 @@ fn short_item_info( let mut extra_info = vec![]; let error_codes = cx.shared.codes; - if let Some(Deprecation { note, since, is_since_rustc_version, suggestion: _ }) = + if let Some(depr @ Deprecation { note, since, is_since_rustc_version: _, suggestion: _ }) = item.deprecation(cx.tcx()) { // We display deprecation messages for #[deprecated] and #[rustc_deprecated] // but only display the future-deprecation messages for #[rustc_deprecated]. let mut message = if let Some(since) = since { let since = &since.as_str(); - if !stability::deprecation_in_effect(is_since_rustc_version, Some(since)) { + if !stability::deprecation_in_effect(&depr) { if *since == "TBD" { String::from("Deprecating in a future Rust version") } else { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 8888b42d948..fa0d211efe6 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -418,10 +418,7 @@ fn tag_html(class: &str, title: &str, contents: &str) -> String { // The trailing space after each tag is to space it properly against the rest of the docs. if let Some(depr) = &item.deprecation(tcx) { let mut message = "Deprecated"; - if !stability::deprecation_in_effect( - depr.is_since_rustc_version, - depr.since.map(|s| s.as_str()).as_deref(), - ) { + if !stability::deprecation_in_effect(depr) { message = "Deprecation planned"; } tags += &tag_html("deprecated", "", message);