Skip to content

Ban empty features in stability annotations #74537

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum AttrError {
MultipleItem(String),
UnknownMetaItem(String, &'static [&'static str]),
MissingSince,
EmptyFeature,
MissingFeature,
MultipleStabilityLevels,
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
Expand All @@ -41,6 +42,9 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
AttrError::MissingSince => {
struct_span_err!(diag, span, E0542, "missing 'since'").emit();
}
AttrError::EmptyFeature => {
struct_span_err!(diag, span, E0546, "empty 'feature' attribute").emit();
}
AttrError::MissingFeature => {
struct_span_err!(diag, span, E0546, "missing 'feature'").emit();
}
Expand Down Expand Up @@ -427,6 +431,10 @@ where

match (feature, reason, issue) {
(Some(feature), reason, Some(_)) => {
if feature.is_empty() {
handle_errors(sess, attr.span, AttrError::EmptyFeature);
continue;
}
let level = Unstable { reason, issue: issue_num, is_soft };
if sym::unstable == meta_name {
stab = Some(Stability { level, feature, rustc_depr: None });
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,11 @@ impl Symbol {
pub fn to_ident_string(self) -> String {
Ident::with_dummy_span(self).to_string()
}

pub fn is_empty(self) -> bool {
// Invalid is "" (the empty string)
self == kw::Invalid
}
}

impl fmt::Debug for Symbol {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ impl Clean<Stability> for attr::Stability {
fn clean(&self, _: &DocContext<'_>) -> Stability {
Stability {
level: stability::StabilityLevel::from_attr_level(&self.level),
feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
feature: self.feature.to_string(),
since: match self.level {
attr::Stable { ref since } => since.to_string(),
_ => String::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ pub struct ProcMacro {
#[derive(Clone, Debug)]
pub struct Stability {
pub level: stability::StabilityLevel,
pub feature: Option<String>,
pub feature: String,
pub since: String,
pub deprecation: Option<Deprecation>,
pub unstable_reason: Option<String>,
Expand Down
30 changes: 15 additions & 15 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,7 @@ fn stability_tags(item: &clean::Item) -> String {
if item
.stability
.as_ref()
.map(|s| s.level == stability::Unstable && s.feature.as_deref() != Some("rustc_private"))
.map(|s| s.level == stability::Unstable && s.feature != "rustc_private")
== Some(true)
{
tags += &tag_html("unstable", "Experimental");
Expand Down Expand Up @@ -2291,25 +2291,25 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {

// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
// Those crates are permanently unstable so it makes no sense to render "unstable" everywhere.
if let Some(stab) = item.stability.as_ref().filter(|stab| {
stab.level == stability::Unstable && stab.feature.as_deref() != Some("rustc_private")
}) {
if let Some(stab) = item
.stability
.as_ref()
.filter(|stab| stab.level == stability::Unstable && stab.feature != "rustc_private")
{
let mut message =
"<span class='emoji'>🔬</span> This is a nightly-only experimental API.".to_owned();

if let Some(feature) = stab.feature.as_deref() {
let mut feature = format!("<code>{}</code>", Escape(&feature));
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, stab.issue) {
feature.push_str(&format!(
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
url = url,
issue = issue
));
}

message.push_str(&format!(" ({})", feature));
let mut feature = format!("<code>{}</code>", Escape(&stab.feature));
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, stab.issue) {
feature.push_str(&format!(
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
url = url,
issue = issue
));
}

message.push_str(&format!(" ({})", feature));

if let Some(unstable_reason) = &stab.unstable_reason {
let mut ids = cx.id_map.borrow_mut();
message = format!(
Expand Down