Skip to content

Commit

Permalink
Represent absence of 'since' attribute as a variant of DeprecatedSince
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 30, 2023
1 parent c523672 commit e8868af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
32 changes: 17 additions & 15 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ pub fn eval_condition(

#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
pub struct Deprecation {
pub since: Option<DeprecatedSince>,
pub since: DeprecatedSince,
/// The note to issue a reason.
pub note: Option<Symbol>,
/// A text snippet used to completely replace any use of the deprecated item in an expression.
Expand All @@ -738,8 +738,10 @@ pub enum DeprecatedSince {
/// `feature(staged_api)` is off. Deprecation versions outside the standard
/// library are allowed to be arbitrary strings, for better or worse.
Symbol(Symbol),
/// Failed to parse a deprecation version. An error has already been
/// emitted.
/// Deprecation version is unspecified but optional.
Unspecified,
/// Failed to parse a deprecation version, or the deprecation version is
/// unspecified and required. An error has already been emitted.
Err,
}

Expand All @@ -749,12 +751,12 @@ impl Deprecation {
/// version).
pub fn is_in_effect(&self) -> bool {
match self.since {
Some(DeprecatedSince::RustcVersion(since)) => since <= RustcVersion::CURRENT,
Some(DeprecatedSince::Future) => false,
DeprecatedSince::RustcVersion(since) => since <= RustcVersion::CURRENT,
DeprecatedSince::Future => false,
// The `since` field doesn't have semantic purpose without `#![staged_api]`.
Some(DeprecatedSince::Symbol(_)) => true,
DeprecatedSince::Symbol(_) => true,
// Assume deprecation is in effect if "since" field is absent or invalid.
None | Some(DeprecatedSince::Err) => true,
DeprecatedSince::Unspecified | DeprecatedSince::Err => true,
}
}
}
Expand Down Expand Up @@ -867,20 +869,20 @@ pub fn find_deprecation(

let since = if let Some(since) = since {
if since.as_str() == "TBD" {
Some(DeprecatedSince::Future)
DeprecatedSince::Future
} else if !is_rustc {
Some(DeprecatedSince::Symbol(since))
DeprecatedSince::Symbol(since)
} else if let Some(version) = parse_version(since) {
Some(DeprecatedSince::RustcVersion(version))
DeprecatedSince::RustcVersion(version)
} else {
sess.emit_err(session_diagnostics::InvalidSince { span: attr.span });
Some(DeprecatedSince::Err)
DeprecatedSince::Err
}
} else if is_rustc {
sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
DeprecatedSince::Err
} else {
if is_rustc {
sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
}
None
DeprecatedSince::Unspecified
};

if is_rustc && note.is_none() {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn deprecation_lint(is_in_effect: bool) -> &'static Lint {

fn deprecation_message(
is_in_effect: bool,
since: Option<DeprecatedSince>,
since: DeprecatedSince,
note: Option<Symbol>,
kind: &str,
path: &str,
Expand All @@ -156,13 +156,13 @@ fn deprecation_message(
format!("use of deprecated {kind} `{path}`")
} else {
match since {
Some(DeprecatedSince::RustcVersion(version)) => format!(
DeprecatedSince::RustcVersion(version) => format!(
"use of {kind} `{path}` that will be deprecated in future version {version}"
),
Some(DeprecatedSince::Future) => {
DeprecatedSince::Future => {
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
}
Some(DeprecatedSince::Symbol(_)) | Some(DeprecatedSince::Err) | None => {
DeprecatedSince::Symbol(_) | DeprecatedSince::Unspecified | DeprecatedSince::Err => {
unreachable!("this deprecation is always in effect; {since:?}")
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<'tcx> TyCtxt<'tcx> {
// With #![staged_api], we want to emit down the whole
// hierarchy.
let depr_attr = &depr_entry.attr;
if !skip || matches!(depr_attr.since, Some(DeprecatedSince::RustcVersion(_))) {
if !skip || matches!(depr_attr.since, 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.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}

if let Some((
rustc_attr::Deprecation { since: Some(DeprecatedSince::RustcVersion(_)), .. },
rustc_attr::Deprecation { since: DeprecatedSince::RustcVersion(_), .. },
span,
)) = &depr
{
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
if let (
&Some(DeprecatedSince::RustcVersion(dep_since)),
&attr::Stable { since: stab_since, .. },
) = (&depr.as_ref().and_then(|(d, _)| d.since), &stab.level)
) = (&depr.as_ref().map(|(d, _)| d.since), &stab.level)
{
match stab_since {
StableSince::Current => {
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,18 +620,18 @@ fn short_item_info(
// We display deprecation messages for #[deprecated], but only display
// the future-deprecation messages for rustc versions.
let mut message = match since {
Some(DeprecatedSince::RustcVersion(version)) => {
DeprecatedSince::RustcVersion(version) => {
if depr.is_in_effect() {
format!("Deprecated since {version}")
} else {
format!("Deprecating in {version}")
}
}
Some(DeprecatedSince::Future) => String::from("Deprecating in a future Rust version"),
Some(DeprecatedSince::Symbol(since)) => {
DeprecatedSince::Future => String::from("Deprecating in a future Rust version"),
DeprecatedSince::Symbol(since) => {
format!("Deprecated since {}", Escape(since.as_str()))
}
Some(DeprecatedSince::Err) | None => String::from("Deprecated"),
DeprecatedSince::Unspecified | DeprecatedSince::Err => String::from("Deprecated"),
};

if let Some(note) = note {
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ where
pub(crate) fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
let rustc_attr::Deprecation { since, note, suggestion: _ } = deprecation;
let since = match since {
Some(DeprecatedSince::RustcVersion(version)) => Some(version.to_string()),
Some(DeprecatedSince::Future) => Some("TBD".to_owned()),
Some(DeprecatedSince::Symbol(since)) => Some(since.to_string()),
Some(DeprecatedSince::Err) | None => None,
DeprecatedSince::RustcVersion(version) => Some(version.to_string()),
DeprecatedSince::Future => Some("TBD".to_owned()),
DeprecatedSince::Symbol(since) => Some(since.to_string()),
DeprecatedSince::Unspecified | DeprecatedSince::Err => None,
};
Deprecation { since, note: note.map(|s| s.to_string()) }
}
Expand Down

0 comments on commit e8868af

Please sign in to comment.