Skip to content

Commit

Permalink
Gate stability attrs with other attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 3, 2022
1 parent 969f873 commit 3c7947e
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 163 deletions.
25 changes: 25 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Expand Up @@ -420,6 +420,31 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}
}

// Emit errors for non-staged-api crates.
if !self.features.staged_api {
if attr.has_name(sym::rustc_deprecated)
|| attr.has_name(sym::unstable)
|| attr.has_name(sym::stable)
|| attr.has_name(sym::rustc_const_unstable)
|| attr.has_name(sym::rustc_const_stable)
{
struct_span_err!(
self.sess,
attr.span,
E0734,
"stability attributes may not be used outside of the standard library",
)
.emit();
}
} else {
if attr.has_name(sym::deprecated) {
self.sess
.struct_span_err(attr.span, "`#[deprecated]` cannot be used in staged API")
.span_label(attr.span, "use `#[rustc_deprecated]` instead")
.emit();
}
}
}

fn visit_item(&mut self, i: &'a ast::Item) {
Expand Down
65 changes: 9 additions & 56 deletions compiler/rustc_passes/src/stability.rs
@@ -1,7 +1,6 @@
//! A pass that annotates every item and method with its stability level,
//! propagating default levels lexically from parent to children ast nodes.

use rustc_ast::Attribute;
use rustc_attr::{self as attr, ConstStability, Stability};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
Expand Down Expand Up @@ -113,12 +112,8 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
{
let attrs = self.tcx.get_attrs(def_id.to_def_id());
debug!("annotate(id = {:?}, attrs = {:?})", def_id, attrs);
let mut did_error = false;
if !self.tcx.features().staged_api {
did_error = self.forbid_staged_api_attrs(def_id, attrs, inherit_deprecation.clone());
}

let depr = if did_error { None } else { attr::find_deprecation(&self.tcx.sess, attrs) };
let depr = attr::find_deprecation(&self.tcx.sess, attrs);
let mut is_deprecated = false;
if let Some((depr, span)) = &depr {
is_deprecated = true;
Expand Down Expand Up @@ -148,16 +143,15 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}
}

if self.tcx.features().staged_api {
if let Some(a) = attrs.iter().find(|a| a.has_name(sym::deprecated)) {
self.tcx
.sess
.struct_span_err(a.span, "`#[deprecated]` cannot be used in staged API")
.span_label(a.span, "use `#[rustc_deprecated]` instead")
.span_label(item_sp, "")
.emit();
if !self.tcx.features().staged_api {
// Propagate unstability. This can happen even for non-staged-api crates in case
// -Zforce-unstable-if-unmarked is set.
if let Some(stab) = self.parent_stab {
if inherit_deprecation.yes() && stab.level.is_unstable() {
self.index.stab_map.insert(def_id, stab);
}
}
} else {

self.recurse_with_stability_attrs(
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
None,
Expand Down Expand Up @@ -329,47 +323,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
self.parent_const_stab = orig_parent_const_stab;
}
}

// returns true if an error occurred, used to suppress some spurious errors
fn forbid_staged_api_attrs(
&mut self,
def_id: LocalDefId,
attrs: &[Attribute],
inherit_deprecation: InheritDeprecation,
) -> bool {
// Emit errors for non-staged-api crates.
let unstable_attrs = [
sym::unstable,
sym::stable,
sym::rustc_deprecated,
sym::rustc_const_unstable,
sym::rustc_const_stable,
];
let mut has_error = false;
for attr in attrs {
let name = attr.name_or_empty();
if unstable_attrs.contains(&name) {
struct_span_err!(
self.tcx.sess,
attr.span,
E0734,
"stability attributes may not be used outside of the standard library",
)
.emit();
has_error = true;
}
}

// Propagate unstability. This can happen even for non-staged-api crates in case
// -Zforce-unstable-if-unmarked is set.
if let Some(stab) = self.parent_stab {
if inherit_deprecation.yes() && stab.level.is_unstable() {
self.index.stab_map.insert(def_id, stab);
}
}

has_error
}
}

impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/deprecation/deprecation-in-staged-api.stderr
Expand Up @@ -3,8 +3,6 @@ error: `#[deprecated]` cannot be used in staged API
|
LL | #[deprecated]
| ^^^^^^^^^^^^^ use `#[rustc_deprecated]` instead
LL | fn main() {}
| ------------

error: aborting due to previous error

12 changes: 6 additions & 6 deletions src/test/ui/feature-gates/feature-gate-staged_api.stderr
@@ -1,15 +1,15 @@
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/feature-gate-staged_api.rs:1:1
|
LL | #![stable(feature = "a", since = "b")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/feature-gate-staged_api.rs:8:1
|
LL | #[stable(feature = "a", since = "b")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/feature-gate-staged_api.rs:1:1
|
LL | #![stable(feature = "a", since = "b")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0734`.
Expand Up @@ -6,25 +6,38 @@

#![rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]

#[rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
mod rustc_deprecated {
mod inner { #![rustc_deprecated()] }
//~^ ERROR stability attributes may not be used outside of the standard library
mod inner {
#![rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
}

#[rustc_deprecated()] fn f() { }
#[rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
fn f() {}

#[rustc_deprecated()] struct S;
#[rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
//~| ERROR missing 'since' [E0542]
struct S;

#[rustc_deprecated()] type T = S;
#[rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
type T = S;

#[rustc_deprecated()] impl S { }
#[rustc_deprecated()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR missing 'since' [E0542]
impl S {}
}

fn main() {}
@@ -1,51 +1,94 @@
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:7:1
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:16:9
|
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:10:1
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:21:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:13:17
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:26:5
|
LL | mod inner { #![rustc_deprecated()] }
| ^^^^^^^^^^^^^^^^^^^^^^
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:16:5
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:32:5
|
LL | #[rustc_deprecated()] fn f() { }
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:19:5
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:37:5
|
LL | #[rustc_deprecated()] struct S;
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:19:5
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:11:1
|
LL | #[rustc_deprecated()] struct S;
| ^^^^^^^^^^^^^^^^^^^^^
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:23:5
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:7:1
|
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:7:1
|
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:11:1
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:16:9
|
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:21:5
|
LL | #[rustc_deprecated()] type T = S;
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0734]: stability attributes may not be used outside of the standard library
error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:26:5
|
LL | #[rustc_deprecated()] impl S { }
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:26:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:32:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:37:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 15 previous errors

For more information about this error, try `rustc --explain E0734`.
Some errors have detailed explanations: E0542, E0734.
For more information about an error, try `rustc --explain E0542`.
19 changes: 12 additions & 7 deletions src/test/ui/feature-gates/issue-43106-gating-of-stable.rs
Expand Up @@ -10,21 +10,26 @@
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
mod stable {
mod inner { #![stable()] }
//~^ ERROR stability attributes may not be used outside of the standard library
mod inner {
#![stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
}

#[stable()] fn f() { }
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
fn f() {}

#[stable()] struct S;
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
//~| ERROR stability attributes may not be used outside of the standard library
struct S;

#[stable()] type T = S;
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
type T = S;

#[stable()] impl S { }
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
impl S {}
}

fn main() {}

0 comments on commit 3c7947e

Please sign in to comment.