-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
lint ineffective #[unstable] annotations on re-exports #161178
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
base: main
Are you sure you want to change the base?
Changes from all commits
79d5626
7b7695d
7249fbc
e22c3c2
fbec8a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,10 @@ use rustc_hir::{ | |
| use rustc_lint_defs as lint; | ||
| use rustc_lint_defs::builtin::{ | ||
| DEPRECATED, DUPLICATE_FEATURES, INEFFECTIVE_UNSTABLE_TRAIT_IMPL, STABLE_FEATURES, | ||
| UNUSED_UNSTABLE_REEXPORT_ATTRIBUTES, | ||
| }; | ||
| use rustc_middle::hir::nested_filter; | ||
| use rustc_middle::metadata::Reexport; | ||
| use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures}; | ||
| use rustc_middle::middle::privacy::EffectiveVisibilities; | ||
| use rustc_middle::middle::stability::{AllowUnstable, Deprecated, DeprecationEntry, EvalResult}; | ||
|
|
@@ -523,7 +525,9 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { | |
| /// Cross-references the feature names of unstable APIs with enabled | ||
| /// features and possibly prints errors. | ||
| fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, mod_id: LocalModId) { | ||
| tcx.hir_visit_item_likes_in_module(mod_id, &mut Checker { tcx }); | ||
| let mut checker = Checker { tcx, mod_id, unstable_reexports: FxIndexMap::default() }; | ||
| tcx.hir_visit_item_likes_in_module(mod_id, &mut checker); | ||
| checker.emit_unused_unstable_reexport_attributes(); | ||
|
|
||
| let is_staged_api = | ||
| tcx.sess.opts.unstable_opts.force_unstable_if_unmarked || tcx.features().staged_api(); | ||
|
|
@@ -553,8 +557,135 @@ pub(crate) fn provide(providers: &mut Providers) { | |
| }; | ||
| } | ||
|
|
||
| struct UnstableReexport { | ||
| hir_id: HirId, | ||
| span: Span, | ||
| has_target: bool, | ||
| all_targets_stable: bool, | ||
| } | ||
|
|
||
| struct Checker<'tcx> { | ||
| tcx: TyCtxt<'tcx>, | ||
| mod_id: LocalModId, | ||
| unstable_reexports: FxIndexMap<Span, UnstableReexport>, | ||
| } | ||
|
|
||
| impl<'tcx> Checker<'tcx> { | ||
| fn unstable_reexport_span(&self, item: &'tcx hir::Item<'tcx>) -> Option<Span> { | ||
| let attrs = self.tcx.hir_attrs(item.hir_id()); | ||
| let (stability, span) = | ||
| find_attr!(attrs, Stability { stability, span } => (*stability, *span))?; | ||
|
|
||
| matches!(stability.level, StabilityLevel::Unstable { .. }).then_some(span) | ||
| } | ||
|
Comment on lines
+574
to
+580
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this span would be more helpful if it pointed to the item being re-exported
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the attribute span is only kept as the grouping key for grouped reexport for now |
||
|
|
||
| fn classify_reexport_targets<Id>( | ||
| &self, | ||
| targets: impl IntoIterator<Item = Res<Id>>, | ||
| ) -> (bool, bool) { | ||
| let mut has_target = false; | ||
| let mut all_targets_stable = true; | ||
|
|
||
| for res in targets { | ||
| match res { | ||
| Res::Def(_, def_id) => { | ||
| has_target = true; | ||
|
|
||
| if self.tcx.lookup_stability(def_id).is_some_and(|stab| !stab.level.is_stable()) | ||
| { | ||
| all_targets_stable = false; | ||
| } | ||
| } | ||
|
|
||
| Res::PrimTy(_) => { | ||
| has_target = true; | ||
| } | ||
|
|
||
| _ => { | ||
| all_targets_stable = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| (has_target, all_targets_stable) | ||
| } | ||
|
|
||
| fn record_unstable_reexport( | ||
| &mut self, | ||
| item: &'tcx hir::Item<'tcx>, | ||
| attr_span: Span, | ||
| span: Span, | ||
| has_target: bool, | ||
| all_targets_stable: bool, | ||
| ) { | ||
| let entry = self.unstable_reexports.entry(attr_span).or_insert(UnstableReexport { | ||
| hir_id: item.hir_id(), | ||
| span, | ||
| has_target: false, | ||
| all_targets_stable: true, | ||
| }); | ||
|
|
||
| entry.has_target |= has_target; | ||
| entry.all_targets_stable &= all_targets_stable; | ||
| } | ||
|
|
||
| fn check_single_unstable_reexport( | ||
| &mut self, | ||
| item: &'tcx hir::Item<'tcx>, | ||
| path: &'tcx UsePath<'tcx>, | ||
| ) { | ||
| let Some(attr_span) = self.unstable_reexport_span(item) else { | ||
| return; | ||
| }; | ||
|
|
||
| let (has_target, all_targets_stable) = | ||
| self.classify_reexport_targets(path.res.present_items()); | ||
|
|
||
| self.record_unstable_reexport(item, attr_span, path.span, has_target, all_targets_stable); | ||
| } | ||
|
|
||
| fn check_glob_unstable_reexport( | ||
| &mut self, | ||
| item: &'tcx hir::Item<'tcx>, | ||
| path: &'tcx UsePath<'tcx>, | ||
| ) { | ||
| let Some(attr_span) = self.unstable_reexport_span(item) else { | ||
| return; | ||
| }; | ||
|
|
||
| let glob_def_id = item.owner_id.def_id.to_def_id(); | ||
|
|
||
| let targets = self | ||
| .tcx | ||
| .module_children_local(self.mod_id.to_local_def_id()) | ||
| .iter() | ||
| .filter(|child| { | ||
| child.reexport_chain.iter().any(|reexport| { | ||
| matches!( | ||
| *reexport, | ||
| Reexport::Glob(def_id) if def_id == glob_def_id | ||
| ) | ||
| }) | ||
| }) | ||
| .map(|child| child.res); | ||
|
|
||
| let (has_target, all_targets_stable) = self.classify_reexport_targets(targets); | ||
|
|
||
| self.record_unstable_reexport(item, attr_span, path.span, has_target, all_targets_stable); | ||
| } | ||
|
|
||
| fn emit_unused_unstable_reexport_attributes(&self) { | ||
| for reexport in self.unstable_reexports.values() { | ||
| if reexport.has_target && reexport.all_targets_stable { | ||
| self.tcx.emit_node_span_lint( | ||
| UNUSED_UNSTABLE_REEXPORT_ATTRIBUTES, | ||
| reexport.hir_id, | ||
| reexport.span, | ||
| diagnostics::UnusedUnstableReexportAttributes, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> Visitor<'tcx> for Checker<'tcx> { | ||
|
|
@@ -583,6 +714,20 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { | |
| self.tcx.check_stability(def_id, Some(item.hir_id()), item.span, None); | ||
| } | ||
|
|
||
| hir::ItemKind::Use(path, hir::UseKind::Single(_)) | ||
| if self.tcx.features().staged_api() | ||
| && self.tcx.local_visibility(item.owner_id.def_id).is_public() => | ||
| { | ||
| self.check_single_unstable_reexport(item, path); | ||
| } | ||
|
|
||
| hir::ItemKind::Use(path, hir::UseKind::Glob) | ||
| if self.tcx.features().staged_api() | ||
| && self.tcx.local_visibility(item.owner_id.def_id).is_public() => | ||
| { | ||
| self.check_glob_unstable_reexport(item, path); | ||
| } | ||
|
|
||
| // For implementations of traits, check the stability of each item | ||
| // individually as it's possible to have a stable trait with unstable | ||
| // items. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //@ check-pass | ||
| //@ normalize-stderr: "(\n)\n$" -> "$1" | ||
| // This lint is only available with `staged_api`. | ||
| #![allow(unused_unstable_reexport_attributes)] | ||
| //~^ WARNING unknown lint: `unused_unstable_reexport_attributes` | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| warning: unknown lint: `unused_unstable_reexport_attributes` | ||
| --> $DIR/feature-gate-unused_unstable_reexport_attributes.rs:4:10 | ||
| | | ||
| LL | #![allow(unused_unstable_reexport_attributes)] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: the `unused_unstable_reexport_attributes` lint is unstable | ||
| = note: `#[warn(unknown_lints)]` on by default | ||
|
|
||
| warning: 1 warning emitted |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #![crate_type = "lib"] | ||
| #![crate_name = "stable_glob_source"] | ||
| #![feature(staged_api)] | ||
| #![stable(feature = "stable_glob_source", since = "1.0.0")] | ||
|
|
||
| #[stable(feature = "stable_glob_source", since = "1.0.0")] | ||
| pub fn stable_a() {} | ||
|
|
||
| #[stable(feature = "stable_glob_source", since = "1.0.0")] | ||
| pub fn stable_b() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //@ aux-build:lint-stability.rs | ||
| //@ aux-build:stable-glob-source.rs | ||
| //@ normalize-stderr: "(\n)\n$" -> "$1" | ||
|
|
||
| #![crate_type = "lib"] | ||
| #![feature(staged_api)] | ||
| #![stable(feature = "reexport_test", since = "1.0.0")] | ||
|
|
||
| extern crate lint_stability; | ||
| extern crate stable_glob_source; | ||
|
|
||
| // Every item introduced by this glob is stable, so the unstable annotation | ||
| // cannot make the exported paths unstable. | ||
| #[unstable(feature = "stable_glob_reexport", issue = "none")] | ||
| pub use stable_glob_source::*; //~ ERROR `#[unstable]` does not make this re-exported path unstable | ||
|
|
||
| // This glob contains unstable items, so #94972 makes the annotation | ||
| // relevant when checking the import itself. | ||
| #[unstable(feature = "unstable_test_feature", issue = "none")] | ||
| pub use lint_stability::*; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also feature gate this lint behind the
staged_apifeature? and add a feature gate test for it?(If you can suppress the "use
#![feature(staged_api)].." suggestion that'd be nice - we suppress that suggestion elsewhere as well.)