1
Fork 0

Auto merge of #89395 - In-line:remove_visible_path_from_allowed_deprecated_lint, r=jyn514

Remove visible path calculation from allowed deprecation lint
This commit is contained in:
bors 2021-10-01 06:19:52 +00:00
commit 4e4942dfa6
4 changed files with 78 additions and 47 deletions

View file

@ -15,12 +15,11 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::{self, HirId}; use rustc_hir::{self, HirId};
use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE}; 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::parse::feature_err_issue;
use rustc_session::{DiagnosticMessageId, Session}; use rustc_session::{DiagnosticMessageId, Session};
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
use rustc_span::{MultiSpan, Span}; use rustc_span::{MultiSpan, Span};
use std::num::NonZeroU32; use std::num::NonZeroU32;
#[derive(PartialEq, Clone, Copy, Debug)] #[derive(PartialEq, Clone, Copy, Debug)]
@ -125,7 +124,11 @@ pub fn report_unstable(
/// Checks whether an item marked with `deprecated(since="X")` is currently /// Checks whether an item marked with `deprecated(since="X")` is currently
/// deprecated (i.e., whether X is not greater than the current rustc version). /// 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<u32> { fn parse_version(ver: &str) -> Vec<u32> {
// We ignore non-integer components of the version (e.g., "nightly"). // We ignore non-integer components of the version (e.g., "nightly").
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect() 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) { fn deprecation_lint(is_in_effect: bool) -> &'static Lint {
let since = depr.since.map(Symbol::as_str); if is_in_effect { DEPRECATED } else { DEPRECATED_IN_FUTURE }
let (message, lint) = if deprecation_in_effect(depr.is_since_rustc_version, since.as_deref()) { }
(format!("use of deprecated {} `{}`", kind, path), DEPRECATED)
fn deprecation_message(
is_in_effect: bool,
since: Option<Symbol>,
note: Option<Symbol>,
kind: &str,
path: &str,
) -> String {
let message = if is_in_effect {
format!("use of deprecated {} `{}`", kind, path)
} else { } else {
( let since = since.map(Symbol::as_str);
if since.as_deref() == Some("TBD") {
format!( if since.as_deref() == Some("TBD") {
"use of {} `{}` that will be deprecated in a future Rust version", format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path)
kind, path } else {
) format!(
} else { "use of {} `{}` that will be deprecated in future version {}",
format!( kind,
"use of {} `{}` that will be deprecated in future version {}", path,
kind, since.unwrap()
path, )
since.unwrap() }
)
},
DEPRECATED_IN_FUTURE,
)
}; };
let message = match depr.note {
match note {
Some(reason) => format!("{}: {}", message, reason), Some(reason) => format!("{}: {}", message, reason),
None => message, 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( pub fn early_report_deprecation(
@ -303,20 +323,34 @@ impl<'tcx> TyCtxt<'tcx> {
// //
// #[rustc_deprecated] however wants to emit down the whole // #[rustc_deprecated] however wants to emit down the whole
// hierarchy. // hierarchy.
if !skip || depr_entry.attr.is_since_rustc_version { let depr_attr = &depr_entry.attr;
let path = &with_no_trimmed_paths(|| self.def_path_str(def_id)); if !skip || depr_attr.is_since_rustc_version {
let kind = self.def_kind(def_id).descr(def_id); // Calculating message for lint involves calling `self.def_path_str`.
let (message, lint) = deprecation_message(&depr_entry.attr, kind, path); // Which by default to calculate visible path will invoke expensive `visible_parent_map` query.
late_report_deprecation( // So we skip message calculation altogether, if lint is allowed.
self, let is_in_effect = deprecation_in_effect(depr_attr);
&message, let lint = deprecation_lint(is_in_effect);
depr_entry.attr.suggestion, if self.lint_level_at_node(lint, id).0 != Level::Allow {
lint, let def_path = &with_no_trimmed_paths(|| self.def_path_str(def_id));
span, let def_kind = self.def_kind(def_id).descr(def_id);
method_span,
id, late_report_deprecation(
def_id, 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,
);
}
} }
}; };
} }

View file

@ -1137,7 +1137,7 @@ impl<'a> Resolver<'a> {
} }
if let Some(depr) = &ext.deprecation { if let Some(depr) = &ext.deprecation {
let path = pprust::path_to_string(&path); 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( stability::early_report_deprecation(
&mut self.lint_buffer, &mut self.lint_buffer,
&message, &message,

View file

@ -599,14 +599,14 @@ fn short_item_info(
let mut extra_info = vec![]; let mut extra_info = vec![];
let error_codes = cx.shared.codes; 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()) item.deprecation(cx.tcx())
{ {
// We display deprecation messages for #[deprecated] and #[rustc_deprecated] // We display deprecation messages for #[deprecated] and #[rustc_deprecated]
// but only display the future-deprecation messages for #[rustc_deprecated]. // but only display the future-deprecation messages for #[rustc_deprecated].
let mut message = if let Some(since) = since { let mut message = if let Some(since) = since {
let since = &since.as_str(); 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" { if *since == "TBD" {
String::from("Deprecating in a future Rust version") String::from("Deprecating in a future Rust version")
} else { } else {

View file

@ -418,10 +418,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
// The trailing space after each tag is to space it properly against the rest of the docs. // The trailing space after each tag is to space it properly against the rest of the docs.
if let Some(depr) = &item.deprecation(tcx) { if let Some(depr) = &item.deprecation(tcx) {
let mut message = "Deprecated"; let mut message = "Deprecated";
if !stability::deprecation_in_effect( if !stability::deprecation_in_effect(depr) {
depr.is_since_rustc_version,
depr.since.map(|s| s.as_str()).as_deref(),
) {
message = "Deprecation planned"; message = "Deprecation planned";
} }
tags += &tag_html("deprecated", "", message); tags += &tag_html("deprecated", "", message);