Integrate stable feature checking into a query.

This commit is contained in:
Camille GILLOT 2022-01-30 15:41:35 +01:00
parent f7e0891423
commit 3301ac5f7b
12 changed files with 93 additions and 120 deletions

View file

@ -1054,11 +1054,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
tcx.ensure_ok().check_mod_unstable_api_usage(module);
});
},
{
sess.time("unused_lib_feature_checking", || {
rustc_passes::stability::check_unused_or_stable_features(tcx)
});
},
{
// We force these queries to run,
// since they might not otherwise get called.

View file

@ -13,7 +13,6 @@ use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
use rustc_feature::{EnabledLangFeature, EnabledLibFeature};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
use rustc_hir::hir_id::CRATE_HIR_ID;
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
use rustc_hir::{self as hir, AmbigArg, FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant};
use rustc_middle::hir::nested_filter;
@ -411,7 +410,7 @@ struct MissingStabilityAnnotations<'tcx> {
impl<'tcx> MissingStabilityAnnotations<'tcx> {
/// Verify that deprecation and stability attributes make sense with one another.
#[instrument(level = "trace", skip(self))]
fn check_compatible_stability(&self, def_id: LocalDefId, item_sp: Span) {
fn check_compatible_stability(&self, def_id: LocalDefId) {
if !self.tcx.features().staged_api() {
return;
}
@ -441,6 +440,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
|| (kind == AnnotationKind::Container && stab.level.is_stable() && depr.is_some())
{
if let Some(span) = find_attr_span!(Stability) {
let item_sp = self.tcx.def_span(def_id);
self.tcx.dcx().emit_err(errors::UselessStability { span, item_sp });
}
}
@ -452,6 +452,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
&& let attrs::StabilityLevel::Stable { since: stab_since, .. } = stab.level
&& let Some(span) = find_attr_span!(Stability)
{
let item_sp = self.tcx.def_span(def_id);
match stab_since {
StableSince::Current => {
self.tcx
@ -506,7 +507,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
}
#[instrument(level = "debug", skip(self))]
fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
fn check_missing_stability(&self, def_id: LocalDefId) {
let stab = self.tcx.lookup_stability(def_id);
self.tcx.ensure_ok().lookup_const_stability(def_id);
if !self.tcx.sess.is_test_crate()
@ -514,11 +515,12 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
&& self.effective_visibilities.is_reachable(def_id)
{
let descr = self.tcx.def_descr(def_id.to_def_id());
let span = self.tcx.def_span(def_id);
self.tcx.dcx().emit_err(errors::MissingStabilityAttr { span, descr });
}
}
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
fn check_missing_const_stability(&self, def_id: LocalDefId) {
let is_const = self.tcx.is_const_fn(def_id.to_def_id())
|| (self.tcx.def_kind(def_id.to_def_id()) == DefKind::Trait
&& self.tcx.is_const_trait(def_id.to_def_id()));
@ -528,6 +530,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
&& self.effective_visibilities.is_reachable(def_id)
&& self.tcx.lookup_const_stability(def_id).is_none()
{
let span = self.tcx.def_span(def_id);
let descr = self.tcx.def_descr(def_id.to_def_id());
self.tcx.dcx().emit_err(errors::MissingConstStabAttr { span, descr });
}
@ -542,7 +545,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
}
fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
self.check_compatible_stability(i.owner_id.def_id, i.span);
self.check_compatible_stability(i.owner_id.def_id);
// Inherent impls and foreign modules serve only as containers for other items,
// they don't have their own stability. They still can be annotated as unstable
@ -553,54 +556,54 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
| hir::ItemKind::ForeignMod { .. }
) {
self.check_missing_stability(i.owner_id.def_id, i.span);
self.check_missing_stability(i.owner_id.def_id);
}
// Ensure stable `const fn` have a const stability attribute.
self.check_missing_const_stability(i.owner_id.def_id, i.span);
self.check_missing_const_stability(i.owner_id.def_id);
intravisit::walk_item(self, i)
}
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
self.check_compatible_stability(ti.owner_id.def_id, ti.span);
self.check_missing_stability(ti.owner_id.def_id, ti.span);
self.check_compatible_stability(ti.owner_id.def_id);
self.check_missing_stability(ti.owner_id.def_id);
intravisit::walk_trait_item(self, ti);
}
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
self.check_compatible_stability(ii.owner_id.def_id, ii.span);
self.check_compatible_stability(ii.owner_id.def_id);
let impl_def_id = self.tcx.hir_get_parent_item(ii.hir_id());
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
self.check_missing_stability(ii.owner_id.def_id, ii.span);
self.check_missing_const_stability(ii.owner_id.def_id, ii.span);
self.check_missing_stability(ii.owner_id.def_id);
self.check_missing_const_stability(ii.owner_id.def_id);
}
intravisit::walk_impl_item(self, ii);
}
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) {
self.check_compatible_stability(var.def_id, var.span);
self.check_missing_stability(var.def_id, var.span);
self.check_compatible_stability(var.def_id);
self.check_missing_stability(var.def_id);
if let Some(ctor_def_id) = var.data.ctor_def_id() {
self.check_missing_stability(ctor_def_id, var.span);
self.check_missing_stability(ctor_def_id);
}
intravisit::walk_variant(self, var);
}
fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
self.check_compatible_stability(s.def_id, s.span);
self.check_missing_stability(s.def_id, s.span);
self.check_compatible_stability(s.def_id);
self.check_missing_stability(s.def_id);
intravisit::walk_field_def(self, s);
}
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
self.check_compatible_stability(i.owner_id.def_id, i.span);
self.check_missing_stability(i.owner_id.def_id, i.span);
self.check_compatible_stability(i.owner_id.def_id);
self.check_missing_stability(i.owner_id.def_id);
intravisit::walk_foreign_item(self, i);
}
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
self.check_compatible_stability(p.def_id, p.span);
self.check_compatible_stability(p.def_id);
// Note that we don't need to `check_missing_stability` for default generic parameters,
// as we assume that any default generic parameters without attributes are automatically
// stable (assuming they have not inherited instability from their parent).
@ -619,6 +622,21 @@ fn stability_implications(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> UnordMap<S
/// features and possibly prints errors.
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
tcx.hir_visit_item_likes_in_module(module_def_id, &mut Checker { tcx });
let is_staged_api =
tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api();
if is_staged_api {
let effective_visibilities = &tcx.effective_visibilities(());
let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities };
if module_def_id.is_top_level_module() {
missing.check_missing_stability(CRATE_DEF_ID);
}
tcx.hir_visit_item_likes_in_module(module_def_id, &mut missing);
}
if module_def_id.is_top_level_module() {
check_unused_or_stable_features(tcx)
}
}
pub(crate) fn provide(providers: &mut Providers) {
@ -1002,16 +1020,9 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
/// Given the list of enabled features that were not language features (i.e., that
/// were expected to be library features), and the list of features used from
/// libraries, identify activated features that don't exist and error about them.
// This is `pub` for rustdoc. rustc should call it through `check_mod_unstable_api_usage`.
pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
let is_staged_api =
tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api();
if is_staged_api {
let effective_visibilities = &tcx.effective_visibilities(());
let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities };
missing.check_missing_stability(CRATE_DEF_ID, tcx.hir_span(CRATE_HIR_ID));
tcx.hir_walk_toplevel_module(&mut missing);
tcx.hir_visit_all_item_likes_in_crate(&mut missing);
}
let _prof_timer = tcx.sess.timer("unused_lib_feature_checking");
let enabled_lang_features = tcx.features().enabled_lang_features();
let mut lang_features = UnordSet::default();

View file

@ -417,6 +417,14 @@ LL | #![cold]
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:12
|
LL | #![feature(rust1)]
| ^^^^^
|
= note: `#[warn(stable_features)]` on by default
warning: `#[macro_use]` only has an effect on `extern crate` and modules
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:176:5
|
@ -1161,13 +1169,5 @@ warning: crate-level attribute should be an inner attribute: add an exclamation
LL | #[type_length_limit="0100"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:12
|
LL | #![feature(rust1)]
| ^^^^^
|
= note: `#[warn(stable_features)]` on by default
warning: 173 warnings emitted

View file

@ -1,8 +1,8 @@
warning: use of deprecated function `lint_stability::deprecated`: text
--> $DIR/lint-stability-deprecated.rs:24:9
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
--> $DIR/lint-stability-deprecated.rs:97:48
|
LL | deprecated();
| ^^^^^^^^^^
LL | struct S2<T: TraitWithAssociatedTypes>(T::TypeDeprecated);
| ^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-stability-deprecated.rs:6:9
@ -10,6 +10,12 @@ note: the lint level is defined here
LL | #![warn(deprecated)]
| ^^^^^^^^^^
warning: use of deprecated function `lint_stability::deprecated`: text
--> $DIR/lint-stability-deprecated.rs:24:9
|
LL | deprecated();
| ^^^^^^^^^^
warning: use of deprecated method `lint_stability::Trait::trait_deprecated`: text
--> $DIR/lint-stability-deprecated.rs:29:16
|
@ -316,12 +322,6 @@ warning: use of deprecated function `this_crate::MethodTester::test_method_body:
LL | fn_in_body();
| ^^^^^^^^^^
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
--> $DIR/lint-stability-deprecated.rs:97:48
|
LL | struct S2<T: TraitWithAssociatedTypes>(T::TypeDeprecated);
| ^^^^^^^^^^^^^^^^^
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
--> $DIR/lint-stability-deprecated.rs:101:13
|

View file

@ -1,3 +1,12 @@
error[E0658]: use of unstable library feature `unstable_test_feature`
--> $DIR/lint-stability.rs:88:48
|
LL | struct S1<T: TraitWithAssociatedTypes>(T::TypeUnstable);
| ^^^^^^^^^^^^^^^
|
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature `unstable_test_feature`
--> $DIR/lint-stability.rs:17:5
|
@ -367,15 +376,6 @@ LL | let _ = Unstable::StableVariant;
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature `unstable_test_feature`
--> $DIR/lint-stability.rs:88:48
|
LL | struct S1<T: TraitWithAssociatedTypes>(T::TypeUnstable);
| ^^^^^^^^^^^^^^^
|
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: use of unstable library feature `unstable_test_feature`
--> $DIR/lint-stability.rs:92:13
|

View file

@ -1,17 +1,14 @@
error: function has missing stability attribute
--> $DIR/missing-stability.rs:8:1
|
LL | / pub fn unmarked() {
LL | |
LL | | ()
LL | | }
| |_^
LL | pub fn unmarked() {
| ^^^^^^^^^^^^^^^^^
error: function has missing stability attribute
--> $DIR/missing-stability.rs:22:5
|
LL | pub fn unmarked() {}
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -23,7 +23,7 @@ error: module has missing stability attribute
--> $DIR/effective_visibilities_invariants.rs:5:1
|
LL | pub mod m {}
| ^^^^^^^^^^^^
| ^^^^^^^^^
error: aborting due to 3 previous errors

View file

@ -20,28 +20,20 @@ LL | | fn main() {}
error: trait has missing stability attribute
--> $DIR/issue-113860-1.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | fn fun() {}
LL | |
LL | | }
| |_^
LL | pub trait Trait {
| ^^^^^^^^^^^^^^^
error: implementation has missing stability attribute
--> $DIR/issue-113860-1.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) fn fun() {}
LL | |
LL | | }
| |_^
LL | impl Trait for u8 {
| ^^^^^^^^^^^^^^^^^
error: associated function has missing stability attribute
--> $DIR/issue-113860-1.rs:6:5
|
LL | fn fun() {}
| ^^^^^^^^^^^
| ^^^^^^^^
error: aborting due to 5 previous errors

View file

@ -20,28 +20,20 @@ LL | | fn main() {}
error: trait has missing stability attribute
--> $DIR/issue-113860-2.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | type X;
LL | |
LL | | }
| |_^
LL | pub trait Trait {
| ^^^^^^^^^^^^^^^
error: implementation has missing stability attribute
--> $DIR/issue-113860-2.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) type X = Self;
LL | |
LL | | }
| |_^
LL | impl Trait for u8 {
| ^^^^^^^^^^^^^^^^^
error: associated type has missing stability attribute
--> $DIR/issue-113860-2.rs:6:5
|
LL | type X;
| ^^^^^^^
| ^^^^^^
error: aborting due to 5 previous errors

View file

@ -20,28 +20,20 @@ LL | | fn main() {}
error: trait has missing stability attribute
--> $DIR/issue-113860.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | const X: u32;
LL | |
LL | | }
| |_^
LL | pub trait Trait {
| ^^^^^^^^^^^^^^^
error: implementation has missing stability attribute
--> $DIR/issue-113860.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) const X: u32 = 3;
LL | |
LL | | }
| |_^
LL | impl Trait for u8 {
| ^^^^^^^^^^^^^^^^^
error: associated constant has missing stability attribute
--> $DIR/issue-113860.rs:6:5
|
LL | const X: u32;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^
error: aborting due to 5 previous errors

View file

@ -2,29 +2,25 @@ error: function has missing const stability attribute
--> $DIR/missing-const-stability.rs:7:1
|
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^
error: trait has missing const stability attribute
--> $DIR/missing-const-stability.rs:23:1
|
LL | / pub const trait Bar {
LL | |
LL | | #[stable(feature = "stable", since = "1.0.0")]
LL | | fn fun();
LL | | }
| |_^
LL | pub const trait Bar {
| ^^^^^^^^^^^^^^^^^^^
error: function has missing const stability attribute
--> $DIR/missing-const-stability.rs:36:1
|
LL | pub const unsafe fn size_of_val<T>(x: *const T) -> usize { 42 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: associated function has missing const stability attribute
--> $DIR/missing-const-stability.rs:16:5
|
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^
error: aborting due to 4 previous errors

View file

@ -1,10 +1,8 @@
error: macro has missing stability attribute
--> $DIR/stability-attribute-sanity-3.rs:8:1
|
LL | / macro_rules! mac {
LL | | () => ()
LL | | }
| |_^
LL | macro_rules! mac {
| ^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error