Skip to content

Remove visible path calculation from allowed deprecation lint #89395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 74 additions & 40 deletions compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
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)]
Expand Down Expand Up @@ -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<u32> {
// We ignore non-integer components of the version (e.g., "nightly").
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
Expand Down Expand Up @@ -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<Symbol>,
note: Option<Symbol>,
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(
Expand Down Expand Up @@ -303,20 +323,34 @@ impl<'tcx> TyCtxt<'tcx> {
//
// #[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,
);
}
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ impl<'a> Resolver<'a> {
}
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,
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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);
Expand Down