1
Fork 0

Store version of deprecated attribute in structured form

This commit is contained in:
David Tolnay 2023-10-26 10:14:29 -07:00
parent 5c7cf83739
commit 2fe7d17bd9
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
7 changed files with 85 additions and 105 deletions

View file

@ -5,7 +5,9 @@ pub use self::StabilityLevel::*;
use crate::ty::{self, TyCtxt};
use rustc_ast::NodeId;
use rustc_attr::{self as attr, ConstStability, DefaultBodyStability, Deprecation, Stability};
use rustc_attr::{
self as attr, ConstStability, DefaultBodyStability, DeprecatedSince, Deprecation, Stability,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diagnostic};
use rustc_feature::GateIssue;
@ -126,36 +128,14 @@ 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(depr: &Deprecation) -> bool {
let is_since_rustc_version = depr.is_since_rustc_version;
let since = depr.since.as_ref().map(Symbol::as_str);
if !is_since_rustc_version {
match depr.since {
Some(DeprecatedSince::RustcVersion(since)) => since <= RustcVersion::CURRENT,
Some(DeprecatedSince::Future) => false,
// The `since` field doesn't have semantic purpose without `#![staged_api]`.
return true;
Some(DeprecatedSince::Symbol(_)) => true,
// Assume deprecation is in effect if "since" field is missing.
None => true,
}
if let Some(since) = since {
if since == "TBD" {
return false;
}
// We ignore non-integer components of the version (e.g., "nightly").
let since: Vec<u16> =
since.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect();
// We simply treat invalid `since` attributes as relating to a previous
// Rust version, thus always displaying the warning.
if since.len() != 3 {
return true;
}
let rustc = RustcVersion::CURRENT;
return since.as_slice() <= &[rustc.major, rustc.minor, rustc.patch];
};
// Assume deprecation is in effect if "since" field is missing
// or if we can't determine the current Rust version.
true
}
pub fn deprecation_suggestion(
@ -180,7 +160,7 @@ fn deprecation_lint(is_in_effect: bool) -> &'static Lint {
fn deprecation_message(
is_in_effect: bool,
since: Option<Symbol>,
since: Option<DeprecatedSince>,
note: Option<Symbol>,
kind: &str,
path: &str,
@ -188,9 +168,7 @@ fn deprecation_message(
let message = if is_in_effect {
format!("use of deprecated {kind} `{path}`")
} else {
let since = since.as_ref().map(Symbol::as_str);
if since == Some("TBD") {
if let Some(DeprecatedSince::Future) = since {
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
} else {
format!(
@ -381,7 +359,7 @@ impl<'tcx> TyCtxt<'tcx> {
// With #![staged_api], we want to emit down the whole
// hierarchy.
let depr_attr = &depr_entry.attr;
if !skip || depr_attr.is_since_rustc_version {
if !skip || matches!(depr_attr.since, Some(DeprecatedSince::RustcVersion(_))) {
// 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.