Skip to content

Commit b106167

Browse files
committed
Add a DeprecatedSince::Err variant for versions that fail to parse
1 parent 1e10fe9 commit b106167

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_session::parse::{feature_err, ParseSess};
1313
use rustc_session::{RustcVersion, Session};
1414
use rustc_span::hygiene::Transparency;
1515
use rustc_span::{symbol::sym, symbol::Symbol, Span};
16-
use std::fmt::{self, Display};
1716
use std::num::NonZeroU32;
1817

1918
use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
@@ -736,12 +735,12 @@ pub enum DeprecatedSince {
736735
RustcVersion(RustcVersion),
737736
/// Deprecated in the future ("to be determined").
738737
Future,
739-
/// `feature(staged_api)` is off, or it's on but the deprecation version
740-
/// cannot be parsed as a RustcVersion. In the latter case, an error has
741-
/// already been emitted. In the former case, deprecation versions outside
742-
/// the standard library are allowed to be arbitrary strings, for better or
743-
/// worse.
738+
/// `feature(staged_api)` is off. Deprecation versions outside the standard
739+
/// library are allowed to be arbitrary strings, for better or worse.
744740
Symbol(Symbol),
741+
/// Failed to parse a deprecation version. An error has already been
742+
/// emitted.
743+
Err,
745744
}
746745

747746
impl Deprecation {
@@ -754,18 +753,8 @@ impl Deprecation {
754753
Some(DeprecatedSince::Future) => false,
755754
// The `since` field doesn't have semantic purpose without `#![staged_api]`.
756755
Some(DeprecatedSince::Symbol(_)) => true,
757-
// Assume deprecation is in effect if "since" field is missing.
758-
None => true,
759-
}
760-
}
761-
}
762-
763-
impl Display for DeprecatedSince {
764-
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
765-
match self {
766-
DeprecatedSince::RustcVersion(since) => Display::fmt(since, formatter),
767-
DeprecatedSince::Future => formatter.write_str("TBD"),
768-
DeprecatedSince::Symbol(since) => Display::fmt(since, formatter),
756+
// Assume deprecation is in effect if "since" field is absent or invalid.
757+
None | Some(DeprecatedSince::Err) => true,
769758
}
770759
}
771760
}
@@ -885,7 +874,7 @@ pub fn find_deprecation(
885874
Some(DeprecatedSince::RustcVersion(version))
886875
} else {
887876
sess.emit_err(session_diagnostics::InvalidSince { span: attr.span });
888-
Some(DeprecatedSince::Symbol(since))
877+
Some(DeprecatedSince::Err)
889878
}
890879
} else if is_rustc {
891880
sess.emit_err(session_diagnostics::MissingSince { span: attr.span });

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ fn deprecation_message(
155155
let message = if is_in_effect {
156156
format!("use of deprecated {kind} `{path}`")
157157
} else {
158-
if let Some(DeprecatedSince::Future) = since {
159-
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
160-
} else {
161-
format!(
162-
"use of {} `{}` that will be deprecated in future version {}",
163-
kind,
164-
path,
165-
since.unwrap()
166-
)
158+
match since {
159+
Some(DeprecatedSince::RustcVersion(version)) => format!(
160+
"use of {kind} `{path}` that will be deprecated in future version {version}"
161+
),
162+
Some(DeprecatedSince::Future) => {
163+
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
164+
}
165+
Some(DeprecatedSince::Symbol(_)) | Some(DeprecatedSince::Err) | None => {
166+
unreachable!("this deprecation is always in effect; {since:?}")
167+
}
167168
}
168169
};
169170

src/librustdoc/html/render/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,18 +619,19 @@ fn short_item_info(
619619
if let Some(depr @ Deprecation { note, since, suggestion: _ }) = item.deprecation(cx.tcx()) {
620620
// We display deprecation messages for #[deprecated], but only display
621621
// the future-deprecation messages for rustc versions.
622-
let mut message = if let Some(since) = since {
623-
if !depr.is_in_effect() {
624-
if let DeprecatedSince::Future = since {
625-
String::from("Deprecating in a future Rust version")
622+
let mut message = match since {
623+
Some(DeprecatedSince::RustcVersion(version)) => {
624+
if depr.is_in_effect() {
625+
format!("Deprecated since {version}")
626626
} else {
627-
format!("Deprecating in {}", Escape(&since.to_string()))
627+
format!("Deprecating in {version}")
628628
}
629-
} else {
630-
format!("Deprecated since {}", Escape(&since.to_string()))
631629
}
632-
} else {
633-
String::from("Deprecated")
630+
Some(DeprecatedSince::Future) => String::from("Deprecating in a future Rust version"),
631+
Some(DeprecatedSince::Symbol(since)) => {
632+
format!("Deprecated since {}", Escape(since.as_str()))
633+
}
634+
Some(DeprecatedSince::Err) | None => String::from("Deprecated"),
634635
};
635636

636637
if let Some(note) = note {

src/librustdoc/json/conversions.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use std::fmt;
88

99
use rustc_ast::ast;
10+
use rustc_attr::DeprecatedSince;
1011
use rustc_hir::{def::CtorKind, def::DefKind, def_id::DefId};
1112
use rustc_metadata::rendered_const;
1213
use rustc_middle::ty::{self, TyCtxt};
@@ -139,7 +140,13 @@ where
139140

140141
pub(crate) fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
141142
let rustc_attr::Deprecation { since, note, suggestion: _ } = deprecation;
142-
Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
143+
let since = match since {
144+
Some(DeprecatedSince::RustcVersion(version)) => Some(version.to_string()),
145+
Some(DeprecatedSince::Future) => Some("TBD".to_owned()),
146+
Some(DeprecatedSince::Symbol(since)) => Some(since.to_string()),
147+
Some(DeprecatedSince::Err) | None => None,
148+
};
149+
Deprecation { since, note: note.map(|s| s.to_string()) }
143150
}
144151

145152
impl FromWithTcx<clean::GenericArgs> for GenericArgs {

0 commit comments

Comments
 (0)