diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index a9466d7df9d..1334868d05c 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -720,7 +720,7 @@ pub fn eval_condition( #[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)] pub struct Deprecation { - pub since: Option, + pub since: DeprecatedSince, /// The note to issue a reason. pub note: Option, /// A text snippet used to completely replace any use of the deprecated item in an expression. @@ -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, } @@ -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, } } } @@ -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() { diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index 4d27bcad911..c4439896faa 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -147,7 +147,7 @@ fn deprecation_lint(is_in_effect: bool) -> &'static Lint { fn deprecation_message( is_in_effect: bool, - since: Option, + since: DeprecatedSince, note: Option, kind: &str, path: &str, @@ -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:?}") } } @@ -347,7 +347,7 @@ pub fn eval_stability_allow_unstable( // 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. diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 675fe6c5d42..6ef160f3b39 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -197,7 +197,7 @@ fn annotate( } if let Some(( - rustc_attr::Deprecation { since: Some(DeprecatedSince::RustcVersion(_)), .. }, + rustc_attr::Deprecation { since: DeprecatedSince::RustcVersion(_), .. }, span, )) = &depr { @@ -228,7 +228,7 @@ fn annotate( 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 => { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index b64bfa2ae8a..ea697108ae2 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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 { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index acc928360d2..0b05e304dfc 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -141,10 +141,10 @@ fn from_tcx(f: I, tcx: TyCtxt<'_>) -> Vec { 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()) } }