Skip to content

Commit

Permalink
Add a DeprecatedSince::Err variant for versions that fail to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 30, 2023
1 parent 1e10fe9 commit b106167
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
27 changes: 8 additions & 19 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use rustc_session::parse::{feature_err, ParseSess};
use rustc_session::{RustcVersion, Session};
use rustc_span::hygiene::Transparency;
use rustc_span::{symbol::sym, symbol::Symbol, Span};
use std::fmt::{self, Display};
use std::num::NonZeroU32;

use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
Expand Down Expand Up @@ -736,12 +735,12 @@ pub enum DeprecatedSince {
RustcVersion(RustcVersion),
/// Deprecated in the future ("to be determined").
Future,
/// `feature(staged_api)` is off, or it's on but the deprecation version
/// cannot be parsed as a RustcVersion. In the latter case, an error has
/// already been emitted. In the former case, deprecation versions outside
/// the standard library are allowed to be arbitrary strings, for better or
/// worse.
/// `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.
Err,
}

impl Deprecation {
Expand All @@ -754,18 +753,8 @@ impl Deprecation {
Some(DeprecatedSince::Future) => false,
// The `since` field doesn't have semantic purpose without `#![staged_api]`.
Some(DeprecatedSince::Symbol(_)) => true,
// Assume deprecation is in effect if "since" field is missing.
None => true,
}
}
}

impl Display for DeprecatedSince {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeprecatedSince::RustcVersion(since) => Display::fmt(since, formatter),
DeprecatedSince::Future => formatter.write_str("TBD"),
DeprecatedSince::Symbol(since) => Display::fmt(since, formatter),
// Assume deprecation is in effect if "since" field is absent or invalid.
None | Some(DeprecatedSince::Err) => true,
}
}
}
Expand Down Expand Up @@ -885,7 +874,7 @@ pub fn find_deprecation(
Some(DeprecatedSince::RustcVersion(version))
} else {
sess.emit_err(session_diagnostics::InvalidSince { span: attr.span });
Some(DeprecatedSince::Symbol(since))
Some(DeprecatedSince::Err)
}
} else if is_rustc {
sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ fn deprecation_message(
let message = if is_in_effect {
format!("use of deprecated {kind} `{path}`")
} else {
if let Some(DeprecatedSince::Future) = since {
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
} else {
format!(
"use of {} `{}` that will be deprecated in future version {}",
kind,
path,
since.unwrap()
)
match since {
Some(DeprecatedSince::RustcVersion(version)) => format!(
"use of {kind} `{path}` that will be deprecated in future version {version}"
),
Some(DeprecatedSince::Future) => {
format!("use of {kind} `{path}` that will be deprecated in a future Rust version")
}
Some(DeprecatedSince::Symbol(_)) | Some(DeprecatedSince::Err) | None => {
unreachable!("this deprecation is always in effect; {since:?}")
}
}
};

Expand Down
19 changes: 10 additions & 9 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,19 @@ fn short_item_info(
if let Some(depr @ Deprecation { note, since, suggestion: _ }) = item.deprecation(cx.tcx()) {
// We display deprecation messages for #[deprecated], but only display
// the future-deprecation messages for rustc versions.
let mut message = if let Some(since) = since {
if !depr.is_in_effect() {
if let DeprecatedSince::Future = since {
String::from("Deprecating in a future Rust version")
let mut message = match since {
Some(DeprecatedSince::RustcVersion(version)) => {
if depr.is_in_effect() {
format!("Deprecated since {version}")
} else {
format!("Deprecating in {}", Escape(&since.to_string()))
format!("Deprecating in {version}")
}
} else {
format!("Deprecated since {}", Escape(&since.to_string()))
}
} else {
String::from("Deprecated")
Some(DeprecatedSince::Future) => String::from("Deprecating in a future Rust version"),
Some(DeprecatedSince::Symbol(since)) => {
format!("Deprecated since {}", Escape(since.as_str()))
}
Some(DeprecatedSince::Err) | None => String::from("Deprecated"),
};

if let Some(note) = note {
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::fmt;

use rustc_ast::ast;
use rustc_attr::DeprecatedSince;
use rustc_hir::{def::CtorKind, def::DefKind, def_id::DefId};
use rustc_metadata::rendered_const;
use rustc_middle::ty::{self, TyCtxt};
Expand Down Expand Up @@ -139,7 +140,13 @@ where

pub(crate) fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
let rustc_attr::Deprecation { since, note, suggestion: _ } = deprecation;
Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
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,
};
Deprecation { since, note: note.map(|s| s.to_string()) }
}

impl FromWithTcx<clean::GenericArgs> for GenericArgs {
Expand Down

0 comments on commit b106167

Please sign in to comment.