Gate stability attrs with other attributes.

This commit is contained in:
Camille GILLOT 2022-01-18 20:37:14 +01:00
parent 969f8735dc
commit 3c7947ee43
14 changed files with 242 additions and 196 deletions

View File

@ -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) {

View File

@ -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;
@ -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;
@ -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,
@ -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> {

View File

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

View File

@ -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`.

View File

@ -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()] }
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() {}

View File

@ -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:16:9
|
LL | #![rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:21:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:26:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:32:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:37:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:11:1
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:7:1
|
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
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[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:13:17
error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:16:9
|
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
error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:21: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
|
LL | #[rustc_deprecated()] struct S;
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:19:5
|
LL | #[rustc_deprecated()] struct S;
| ^^^^^^^^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:23:5
|
LL | #[rustc_deprecated()] type T = S;
| ^^^^^^^^^^^^^^^^^^^^^
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: aborting due to 8 previous errors
error[E0542]: missing 'since'
--> $DIR/issue-43106-gating-of-rustc_deprecated.rs:26:5
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0734`.
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 15 previous errors
Some errors have detailed explanations: E0542, E0734.
For more information about an error, try `rustc --explain E0542`.

View File

@ -10,21 +10,26 @@
#[stable()]
//~^ ERROR stability attributes may not be used outside of the standard library
mod stable {
mod inner { #![stable()] }
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() {}

View File

@ -1,9 +1,33 @@
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:7:1
--> $DIR/issue-43106-gating-of-stable.rs:14:9
|
LL | #![stable()]
| ^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:18:5
|
LL | #[stable()]
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:22:5
|
LL | #[stable()]
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:26:5
|
LL | #[stable()]
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:30:5
|
LL | #[stable()]
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:10:1
|
@ -11,41 +35,11 @@ LL | #[stable()]
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:13:17
--> $DIR/issue-43106-gating-of-stable.rs:7:1
|
LL | mod inner { #![stable()] }
LL | #![stable()]
| ^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:16:5
|
LL | #[stable()] fn f() { }
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:19:5
|
LL | #[stable()] struct S;
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:19:5
|
LL | #[stable()] struct S;
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:23:5
|
LL | #[stable()] type T = S;
| ^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-stable.rs:26:5
|
LL | #[stable()] impl S { }
| ^^^^^^^^^^^
error: aborting due to 8 previous errors
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0734`.

View File

@ -10,21 +10,26 @@
#[unstable()]
//~^ ERROR stability attributes may not be used outside of the standard library
mod unstable {
mod inner { #![unstable()] }
mod inner {
#![unstable()]
//~^ ERROR stability attributes may not be used outside of the standard library
}
#[unstable()] fn f() { }
#[unstable()]
//~^ ERROR stability attributes may not be used outside of the standard library
fn f() {}
#[unstable()] struct S;
#[unstable()]
//~^ 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;
#[unstable()] type T = S;
#[unstable()]
//~^ ERROR stability attributes may not be used outside of the standard library
type T = S;
#[unstable()] impl S { }
#[unstable()]
//~^ ERROR stability attributes may not be used outside of the standard library
impl S {}
}
fn main() {}

View File

@ -1,9 +1,33 @@
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:7:1
--> $DIR/issue-43106-gating-of-unstable.rs:14:9
|
LL | #![unstable()]
| ^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:18:5
|
LL | #[unstable()]
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:22:5
|
LL | #[unstable()]
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:26:5
|
LL | #[unstable()]
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:30:5
|
LL | #[unstable()]
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:10:1
|
@ -11,41 +35,11 @@ LL | #[unstable()]
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:13:17
--> $DIR/issue-43106-gating-of-unstable.rs:7:1
|
LL | mod inner { #![unstable()] }
LL | #![unstable()]
| ^^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:16:5
|
LL | #[unstable()] fn f() { }
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:19:5
|
LL | #[unstable()] struct S;
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:19:5
|
LL | #[unstable()] struct S;
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:23:5
|
LL | #[unstable()] type T = S;
| ^^^^^^^^^^^^^
error[E0734]: stability attributes may not be used outside of the standard library
--> $DIR/issue-43106-gating-of-unstable.rs:26:5
|
LL | #[unstable()] impl S { }
| ^^^^^^^^^^^^^
error: aborting due to 8 previous errors
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0734`.

View File

@ -3,4 +3,5 @@
#[unstable()] //~ ERROR: stability attributes may not be used
#[stable()] //~ ERROR: stability attributes may not be used
#[rustc_deprecated()] //~ ERROR: stability attributes may not be used
fn main() { }
//~^ ERROR missing 'since'
fn main() {}

View File

@ -16,6 +16,13 @@ error[E0734]: stability attributes may not be used outside of the standard libra
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
error[E0542]: missing 'since'
--> $DIR/stability-attribute-non-staged-force-unstable.rs:5:1
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0734`.
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0542, E0734.
For more information about an error, try `rustc --explain E0542`.

View File

@ -1,4 +1,5 @@
#[unstable()] //~ ERROR: stability attributes may not be used
#[stable()] //~ ERROR: stability attributes may not be used
#[rustc_deprecated()] //~ ERROR: stability attributes may not be used
fn main() { }
//~^ ERROR missing 'since'
fn main() {}

View File

@ -16,6 +16,13 @@ error[E0734]: stability attributes may not be used outside of the standard libra
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
error[E0542]: missing 'since'
--> $DIR/stability-attribute-non-staged.rs:3:1
|
LL | #[rustc_deprecated()]
| ^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0734`.
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0542, E0734.
For more information about an error, try `rustc --explain E0542`.