Skip to content

Commit 4e4942d

Browse files
committed
Auto merge of rust-lang#89395 - In-line:remove_visible_path_from_allowed_deprecated_lint, r=jyn514
Remove visible path calculation from allowed deprecation lint
2 parents 598d89b + d98ac57 commit 4e4942d

File tree

4 files changed

+78
-47
lines changed

4 files changed

+78
-47
lines changed

compiler/rustc_middle/src/middle/stability.rs

+74-40
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
1515
use rustc_hir::{self, HirId};
1616
use rustc_middle::ty::print::with_no_trimmed_paths;
1717
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
18-
use rustc_session::lint::{BuiltinLintDiagnostics, Lint, LintBuffer};
18+
use rustc_session::lint::{BuiltinLintDiagnostics, Level, Lint, LintBuffer};
1919
use rustc_session::parse::feature_err_issue;
2020
use rustc_session::{DiagnosticMessageId, Session};
2121
use rustc_span::symbol::{sym, Symbol};
2222
use rustc_span::{MultiSpan, Span};
23-
2423
use std::num::NonZeroU32;
2524

2625
#[derive(PartialEq, Clone, Copy, Debug)]
@@ -125,7 +124,11 @@ pub fn report_unstable(
125124

126125
/// Checks whether an item marked with `deprecated(since="X")` is currently
127126
/// deprecated (i.e., whether X is not greater than the current rustc version).
128-
pub fn deprecation_in_effect(is_since_rustc_version: bool, since: Option<&str>) -> bool {
127+
pub fn deprecation_in_effect(depr: &Deprecation) -> bool {
128+
let is_since_rustc_version = depr.is_since_rustc_version;
129+
let since = depr.since.map(Symbol::as_str);
130+
let since = since.as_deref();
131+
129132
fn parse_version(ver: &str) -> Vec<u32> {
130133
// We ignore non-integer components of the version (e.g., "nightly").
131134
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
@@ -175,33 +178,50 @@ pub fn deprecation_suggestion(
175178
}
176179
}
177180

178-
pub fn deprecation_message(depr: &Deprecation, kind: &str, path: &str) -> (String, &'static Lint) {
179-
let since = depr.since.map(Symbol::as_str);
180-
let (message, lint) = if deprecation_in_effect(depr.is_since_rustc_version, since.as_deref()) {
181-
(format!("use of deprecated {} `{}`", kind, path), DEPRECATED)
181+
fn deprecation_lint(is_in_effect: bool) -> &'static Lint {
182+
if is_in_effect { DEPRECATED } else { DEPRECATED_IN_FUTURE }
183+
}
184+
185+
fn deprecation_message(
186+
is_in_effect: bool,
187+
since: Option<Symbol>,
188+
note: Option<Symbol>,
189+
kind: &str,
190+
path: &str,
191+
) -> String {
192+
let message = if is_in_effect {
193+
format!("use of deprecated {} `{}`", kind, path)
182194
} else {
183-
(
184-
if since.as_deref() == Some("TBD") {
185-
format!(
186-
"use of {} `{}` that will be deprecated in a future Rust version",
187-
kind, path
188-
)
189-
} else {
190-
format!(
191-
"use of {} `{}` that will be deprecated in future version {}",
192-
kind,
193-
path,
194-
since.unwrap()
195-
)
196-
},
197-
DEPRECATED_IN_FUTURE,
198-
)
195+
let since = since.map(Symbol::as_str);
196+
197+
if since.as_deref() == Some("TBD") {
198+
format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path)
199+
} else {
200+
format!(
201+
"use of {} `{}` that will be deprecated in future version {}",
202+
kind,
203+
path,
204+
since.unwrap()
205+
)
206+
}
199207
};
200-
let message = match depr.note {
208+
209+
match note {
201210
Some(reason) => format!("{}: {}", message, reason),
202211
None => message,
203-
};
204-
(message, lint)
212+
}
213+
}
214+
215+
pub fn deprecation_message_and_lint(
216+
depr: &Deprecation,
217+
kind: &str,
218+
path: &str,
219+
) -> (String, &'static Lint) {
220+
let is_in_effect = deprecation_in_effect(depr);
221+
(
222+
deprecation_message(is_in_effect, depr.since, depr.note, kind, path),
223+
deprecation_lint(is_in_effect),
224+
)
205225
}
206226

207227
pub fn early_report_deprecation(
@@ -303,20 +323,34 @@ impl<'tcx> TyCtxt<'tcx> {
303323
//
304324
// #[rustc_deprecated] however wants to emit down the whole
305325
// hierarchy.
306-
if !skip || depr_entry.attr.is_since_rustc_version {
307-
let path = &with_no_trimmed_paths(|| self.def_path_str(def_id));
308-
let kind = self.def_kind(def_id).descr(def_id);
309-
let (message, lint) = deprecation_message(&depr_entry.attr, kind, path);
310-
late_report_deprecation(
311-
self,
312-
&message,
313-
depr_entry.attr.suggestion,
314-
lint,
315-
span,
316-
method_span,
317-
id,
318-
def_id,
319-
);
326+
let depr_attr = &depr_entry.attr;
327+
if !skip || depr_attr.is_since_rustc_version {
328+
// Calculating message for lint involves calling `self.def_path_str`.
329+
// Which by default to calculate visible path will invoke expensive `visible_parent_map` query.
330+
// So we skip message calculation altogether, if lint is allowed.
331+
let is_in_effect = deprecation_in_effect(depr_attr);
332+
let lint = deprecation_lint(is_in_effect);
333+
if self.lint_level_at_node(lint, id).0 != Level::Allow {
334+
let def_path = &with_no_trimmed_paths(|| self.def_path_str(def_id));
335+
let def_kind = self.def_kind(def_id).descr(def_id);
336+
337+
late_report_deprecation(
338+
self,
339+
&deprecation_message(
340+
is_in_effect,
341+
depr_attr.since,
342+
depr_attr.note,
343+
def_kind,
344+
def_path,
345+
),
346+
depr_attr.suggestion,
347+
lint,
348+
span,
349+
method_span,
350+
id,
351+
def_id,
352+
);
353+
}
320354
}
321355
};
322356
}

compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ impl<'a> Resolver<'a> {
11371137
}
11381138
if let Some(depr) = &ext.deprecation {
11391139
let path = pprust::path_to_string(&path);
1140-
let (message, lint) = stability::deprecation_message(depr, "macro", &path);
1140+
let (message, lint) = stability::deprecation_message_and_lint(depr, "macro", &path);
11411141
stability::early_report_deprecation(
11421142
&mut self.lint_buffer,
11431143
&message,

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,14 @@ fn short_item_info(
599599
let mut extra_info = vec![];
600600
let error_codes = cx.shared.codes;
601601

602-
if let Some(Deprecation { note, since, is_since_rustc_version, suggestion: _ }) =
602+
if let Some(depr @ Deprecation { note, since, is_since_rustc_version: _, suggestion: _ }) =
603603
item.deprecation(cx.tcx())
604604
{
605605
// We display deprecation messages for #[deprecated] and #[rustc_deprecated]
606606
// but only display the future-deprecation messages for #[rustc_deprecated].
607607
let mut message = if let Some(since) = since {
608608
let since = &since.as_str();
609-
if !stability::deprecation_in_effect(is_since_rustc_version, Some(since)) {
609+
if !stability::deprecation_in_effect(&depr) {
610610
if *since == "TBD" {
611611
String::from("Deprecating in a future Rust version")
612612
} else {

src/librustdoc/html/render/print_item.rs

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

0 commit comments

Comments
 (0)