Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store #[deprecated] attribute's since value in parsed form #117377

Merged
merged 10 commits into from
Oct 31, 2023
Prev Previous commit
Next Next commit
Add a DeprecatedSince::Err variant for versions that fail to parse
  • Loading branch information
dtolnay committed Oct 30, 2023
commit b106167673c271940e1277cb27f6b75a58f66473
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variant name is not very descriptive? Custom ? UserDefined ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you like NonStandard? It indicates both that this is used outside the standard library and also that it can contain an arbitrary string.

/// 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