Skip to content

Commit 609a38d

Browse files
committed
Integrate stable feature checking into a query.
1 parent c603311 commit 609a38d

13 files changed

+97
-122
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10541054
tcx.ensure_ok().check_mod_unstable_api_usage(module);
10551055
});
10561056
},
1057-
{
1058-
sess.time("unused_lib_feature_checking", || {
1059-
rustc_passes::stability::check_unused_or_stable_features(tcx)
1060-
});
1061-
},
10621057
{
10631058
// We force these queries to run,
10641059
// since they might not otherwise get called.

compiler/rustc_passes/src/stability.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
1313
use rustc_feature::{ACCEPTED_LANG_FEATURES, EnabledLangFeature, EnabledLibFeature};
1414
use rustc_hir::def::{DefKind, Res};
1515
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
16-
use rustc_hir::hir_id::CRATE_HIR_ID;
1716
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
1817
use rustc_hir::{self as hir, AmbigArg, FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant};
1918
use rustc_middle::hir::nested_filter;
@@ -410,7 +409,7 @@ struct MissingStabilityAnnotations<'tcx> {
410409

411410
impl<'tcx> MissingStabilityAnnotations<'tcx> {
412411
#[instrument(level = "trace", skip(self))]
413-
fn check_compatible_stability(&self, def_id: LocalDefId, item_sp: Span) {
412+
fn check_compatible_stability(&self, def_id: LocalDefId) {
414413
if !self.tcx.features().staged_api() {
415414
return;
416415
}
@@ -440,6 +439,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
440439
|| (kind == AnnotationKind::Container && stab.level.is_stable() && depr.is_some())
441440
{
442441
if let Some(span) = find_attr_span!(Stability) {
442+
let item_sp = self.tcx.def_span(def_id);
443443
self.tcx.dcx().emit_err(errors::UselessStability { span, item_sp });
444444
}
445445
}
@@ -451,6 +451,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
451451
&& let attrs::StabilityLevel::Stable { since: stab_since, .. } = stab.level
452452
&& let Some(span) = find_attr_span!(Stability)
453453
{
454+
let item_sp = self.tcx.def_span(def_id);
454455
match stab_since {
455456
StableSince::Current => {
456457
self.tcx
@@ -477,6 +478,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
477478
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
478479
&& let Some(span) = find_attr_span!(Stability)
479480
{
481+
let item_sp = self.tcx.def_span(def_id);
480482
self.tcx
481483
.dcx()
482484
.emit_err(errors::UnstableAttrForAlreadyStableFeature { span, item_sp });
@@ -513,6 +515,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
513515
&& let Some(const_span) = find_attr_span!(ConstStability)
514516
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
515517
{
518+
let item_sp = self.tcx.def_span(def_id);
516519
self.tcx.dcx().emit_err(errors::UnstableAttrForAlreadyStableFeature {
517520
span: const_span,
518521
item_sp,
@@ -529,19 +532,20 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
529532
}
530533

531534
#[instrument(level = "debug", skip(self))]
532-
fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
535+
fn check_missing_stability(&self, def_id: LocalDefId) {
533536
let stab = self.tcx.lookup_stability(def_id);
534537
self.tcx.ensure_ok().lookup_const_stability(def_id);
535538
if !self.tcx.sess.is_test_crate()
536539
&& stab.is_none()
537540
&& self.effective_visibilities.is_reachable(def_id)
538541
{
539542
let descr = self.tcx.def_descr(def_id.to_def_id());
543+
let span = self.tcx.def_span(def_id);
540544
self.tcx.dcx().emit_err(errors::MissingStabilityAttr { span, descr });
541545
}
542546
}
543547

544-
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
548+
fn check_missing_const_stability(&self, def_id: LocalDefId) {
545549
let is_const = self.tcx.is_const_fn(def_id.to_def_id())
546550
|| (self.tcx.def_kind(def_id.to_def_id()) == DefKind::Trait
547551
&& self.tcx.is_const_trait(def_id.to_def_id()));
@@ -551,6 +555,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
551555
&& self.effective_visibilities.is_reachable(def_id)
552556
&& self.tcx.lookup_const_stability(def_id).is_none()
553557
{
558+
let span = self.tcx.def_span(def_id);
554559
let descr = self.tcx.def_descr(def_id.to_def_id());
555560
self.tcx.dcx().emit_err(errors::MissingConstStabAttr { span, descr });
556561
}
@@ -565,7 +570,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
565570
}
566571

567572
fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
568-
self.check_compatible_stability(i.owner_id.def_id, i.span);
573+
self.check_compatible_stability(i.owner_id.def_id);
569574

570575
// Inherent impls and foreign modules serve only as containers for other items,
571576
// they don't have their own stability. They still can be annotated as unstable
@@ -576,54 +581,54 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
576581
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
577582
| hir::ItemKind::ForeignMod { .. }
578583
) {
579-
self.check_missing_stability(i.owner_id.def_id, i.span);
584+
self.check_missing_stability(i.owner_id.def_id);
580585
}
581586

582587
// Ensure stable `const fn` have a const stability attribute.
583-
self.check_missing_const_stability(i.owner_id.def_id, i.span);
588+
self.check_missing_const_stability(i.owner_id.def_id);
584589

585590
intravisit::walk_item(self, i)
586591
}
587592

588593
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
589-
self.check_compatible_stability(ti.owner_id.def_id, ti.span);
590-
self.check_missing_stability(ti.owner_id.def_id, ti.span);
594+
self.check_compatible_stability(ti.owner_id.def_id);
595+
self.check_missing_stability(ti.owner_id.def_id);
591596
intravisit::walk_trait_item(self, ti);
592597
}
593598

594599
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
595-
self.check_compatible_stability(ii.owner_id.def_id, ii.span);
600+
self.check_compatible_stability(ii.owner_id.def_id);
596601
let impl_def_id = self.tcx.hir_get_parent_item(ii.hir_id());
597602
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
598-
self.check_missing_stability(ii.owner_id.def_id, ii.span);
599-
self.check_missing_const_stability(ii.owner_id.def_id, ii.span);
603+
self.check_missing_stability(ii.owner_id.def_id);
604+
self.check_missing_const_stability(ii.owner_id.def_id);
600605
}
601606
intravisit::walk_impl_item(self, ii);
602607
}
603608

604609
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) {
605-
self.check_compatible_stability(var.def_id, var.span);
606-
self.check_missing_stability(var.def_id, var.span);
610+
self.check_compatible_stability(var.def_id);
611+
self.check_missing_stability(var.def_id);
607612
if let Some(ctor_def_id) = var.data.ctor_def_id() {
608-
self.check_missing_stability(ctor_def_id, var.span);
613+
self.check_missing_stability(ctor_def_id);
609614
}
610615
intravisit::walk_variant(self, var);
611616
}
612617

613618
fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
614-
self.check_compatible_stability(s.def_id, s.span);
615-
self.check_missing_stability(s.def_id, s.span);
619+
self.check_compatible_stability(s.def_id);
620+
self.check_missing_stability(s.def_id);
616621
intravisit::walk_field_def(self, s);
617622
}
618623

619624
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
620-
self.check_compatible_stability(i.owner_id.def_id, i.span);
621-
self.check_missing_stability(i.owner_id.def_id, i.span);
625+
self.check_compatible_stability(i.owner_id.def_id);
626+
self.check_missing_stability(i.owner_id.def_id);
622627
intravisit::walk_foreign_item(self, i);
623628
}
624629

625630
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
626-
self.check_compatible_stability(p.def_id, p.span);
631+
self.check_compatible_stability(p.def_id);
627632
// Note that we don't need to `check_missing_stability` for default generic parameters,
628633
// as we assume that any default generic parameters without attributes are automatically
629634
// stable (assuming they have not inherited instability from their parent).
@@ -642,6 +647,21 @@ fn stability_implications(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> UnordMap<S
642647
/// features and possibly prints errors.
643648
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
644649
tcx.hir_visit_item_likes_in_module(module_def_id, &mut Checker { tcx });
650+
651+
let is_staged_api =
652+
tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api();
653+
if is_staged_api {
654+
let effective_visibilities = &tcx.effective_visibilities(());
655+
let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities };
656+
if module_def_id.is_top_level_module() {
657+
missing.check_missing_stability(CRATE_DEF_ID);
658+
}
659+
tcx.hir_visit_item_likes_in_module(module_def_id, &mut missing);
660+
}
661+
662+
if module_def_id.is_top_level_module() {
663+
check_unused_or_stable_features(tcx)
664+
}
645665
}
646666

647667
pub(crate) fn provide(providers: &mut Providers) {
@@ -992,16 +1012,9 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
9921012
/// Given the list of enabled features that were not language features (i.e., that
9931013
/// were expected to be library features), and the list of features used from
9941014
/// libraries, identify activated features that don't exist and error about them.
1015+
// This is `pub` for rustdoc. rustc should call it through `check_mod_unstable_api_usage`.
9951016
pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
996-
let is_staged_api =
997-
tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api();
998-
if is_staged_api {
999-
let effective_visibilities = &tcx.effective_visibilities(());
1000-
let mut missing = MissingStabilityAnnotations { tcx, effective_visibilities };
1001-
missing.check_missing_stability(CRATE_DEF_ID, tcx.hir_span(CRATE_HIR_ID));
1002-
tcx.hir_walk_toplevel_module(&mut missing);
1003-
tcx.hir_visit_all_item_likes_in_crate(&mut missing);
1004-
}
1017+
let _prof_timer = tcx.sess.timer("unused_lib_feature_checking");
10051018

10061019
let enabled_lang_features = tcx.features().enabled_lang_features();
10071020
let mut lang_features = UnordSet::default();

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ LL | #![cold]
417417
|
418418
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
419419

420+
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
421+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:12
422+
|
423+
LL | #![feature(rust1)]
424+
| ^^^^^
425+
|
426+
= note: `#[warn(stable_features)]` on by default
427+
420428
warning: `#[macro_use]` only has an effect on `extern crate` and modules
421429
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:176:5
422430
|
@@ -1161,13 +1169,5 @@ warning: crate-level attribute should be an inner attribute: add an exclamation
11611169
LL | #[type_length_limit="0100"] impl S { }
11621170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
11631171

1164-
warning: the feature `rust1` has been stable since 1.0.0 and no longer requires an attribute to enable
1165-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:12
1166-
|
1167-
LL | #![feature(rust1)]
1168-
| ^^^^^
1169-
|
1170-
= note: `#[warn(stable_features)]` on by default
1171-
11721172
warning: 173 warnings emitted
11731173

tests/ui/feature-gates/unstable-attribute-rejects-already-stable-features.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
66
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
77
LL | const fn my_fun() {}
8-
| -------------------- the stability attribute annotates this item
8+
| ----------------- the stability attribute annotates this item
99
|
1010
help: consider removing the attribute
1111
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
@@ -19,7 +19,7 @@ error: can't mark as unstable using an already stable feature
1919
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
2121
LL | const fn my_fun() {}
22-
| -------------------- the stability attribute annotates this item
22+
| ----------------- the stability attribute annotates this item
2323
|
2424
help: consider removing the attribute
2525
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1

tests/ui/lint/lint-stability-deprecated.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
warning: use of deprecated function `lint_stability::deprecated`: text
2-
--> $DIR/lint-stability-deprecated.rs:24:9
1+
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
2+
--> $DIR/lint-stability-deprecated.rs:97:48
33
|
4-
LL | deprecated();
5-
| ^^^^^^^^^^
4+
LL | struct S2<T: TraitWithAssociatedTypes>(T::TypeDeprecated);
5+
| ^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/lint-stability-deprecated.rs:6:9
99
|
1010
LL | #![warn(deprecated)]
1111
| ^^^^^^^^^^
1212

13+
warning: use of deprecated function `lint_stability::deprecated`: text
14+
--> $DIR/lint-stability-deprecated.rs:24:9
15+
|
16+
LL | deprecated();
17+
| ^^^^^^^^^^
18+
1319
warning: use of deprecated method `lint_stability::Trait::trait_deprecated`: text
1420
--> $DIR/lint-stability-deprecated.rs:29:16
1521
|
@@ -316,12 +322,6 @@ warning: use of deprecated function `this_crate::MethodTester::test_method_body:
316322
LL | fn_in_body();
317323
| ^^^^^^^^^^
318324

319-
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
320-
--> $DIR/lint-stability-deprecated.rs:97:48
321-
|
322-
LL | struct S2<T: TraitWithAssociatedTypes>(T::TypeDeprecated);
323-
| ^^^^^^^^^^^^^^^^^
324-
325325
warning: use of deprecated associated type `lint_stability::TraitWithAssociatedTypes::TypeDeprecated`: text
326326
--> $DIR/lint-stability-deprecated.rs:101:13
327327
|

tests/ui/lint/lint-stability.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error[E0658]: use of unstable library feature `unstable_test_feature`
2+
--> $DIR/lint-stability.rs:88:48
3+
|
4+
LL | struct S1<T: TraitWithAssociatedTypes>(T::TypeUnstable);
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
110
error[E0658]: use of unstable library feature `unstable_test_feature`
211
--> $DIR/lint-stability.rs:17:5
312
|
@@ -367,15 +376,6 @@ LL | let _ = Unstable::StableVariant;
367376
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
368377
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
369378

370-
error[E0658]: use of unstable library feature `unstable_test_feature`
371-
--> $DIR/lint-stability.rs:88:48
372-
|
373-
LL | struct S1<T: TraitWithAssociatedTypes>(T::TypeUnstable);
374-
| ^^^^^^^^^^^^^^^
375-
|
376-
= help: add `#![feature(unstable_test_feature)]` to the crate attributes to enable
377-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
378-
379379
error[E0658]: use of unstable library feature `unstable_test_feature`
380380
--> $DIR/lint-stability.rs:92:13
381381
|
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
error: function has missing stability attribute
22
--> $DIR/missing-stability.rs:8:1
33
|
4-
LL | / pub fn unmarked() {
5-
LL | |
6-
LL | | ()
7-
LL | | }
8-
| |_^
4+
LL | pub fn unmarked() {
5+
| ^^^^^^^^^^^^^^^^^
96

107
error: function has missing stability attribute
118
--> $DIR/missing-stability.rs:22:5
129
|
1310
LL | pub fn unmarked() {}
14-
| ^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^
1512

1613
error: aborting due to 2 previous errors
1714

tests/ui/privacy/effective_visibilities_invariants.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error: module has missing stability attribute
2323
--> $DIR/effective_visibilities_invariants.rs:5:1
2424
|
2525
LL | pub mod m {}
26-
| ^^^^^^^^^^^^
26+
| ^^^^^^^^^
2727

2828
error: aborting due to 3 previous errors
2929

tests/ui/privacy/issue-113860-1.stderr

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,20 @@ LL | | fn main() {}
2020
error: trait has missing stability attribute
2121
--> $DIR/issue-113860-1.rs:4:1
2222
|
23-
LL | / pub trait Trait {
24-
LL | |
25-
LL | | fn fun() {}
26-
LL | |
27-
LL | | }
28-
| |_^
23+
LL | pub trait Trait {
24+
| ^^^^^^^^^^^^^^^
2925

3026
error: implementation has missing stability attribute
3127
--> $DIR/issue-113860-1.rs:10:1
3228
|
33-
LL | / impl Trait for u8 {
34-
LL | |
35-
LL | | pub(self) fn fun() {}
36-
LL | |
37-
LL | | }
38-
| |_^
29+
LL | impl Trait for u8 {
30+
| ^^^^^^^^^^^^^^^^^
3931

4032
error: associated function has missing stability attribute
4133
--> $DIR/issue-113860-1.rs:6:5
4234
|
4335
LL | fn fun() {}
44-
| ^^^^^^^^^^^
36+
| ^^^^^^^^
4537

4638
error: aborting due to 5 previous errors
4739

0 commit comments

Comments
 (0)