about summary refs log tree commit diff
diff options
context:
space:
mode:
authortiif <pekyuan@gmail.com>2025-07-14 13:36:19 +0000
committertiif <pekyuan@gmail.com>2025-07-15 13:48:30 +0000
commitab29256b96660ffd01ab0257588cb6cbd08355a8 (patch)
treea1456295dfdd9de49838bc6c254b8e4ab06e3ef0
parent1e5c7b28772bb99b8af4ac350aaabbb28168dc99 (diff)
downloadrust-ab29256b96660ffd01ab0257588cb6cbd08355a8.tar.gz
rust-ab29256b96660ffd01ab0257588cb6cbd08355a8.zip
Make stability attribute not to error when unstable feature bound is in effect
-rw-r--r--compiler/rustc_passes/src/stability.rs32
-rw-r--r--tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.rs35
-rw-r--r--tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.stderr11
3 files changed, 76 insertions, 2 deletions
diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs
index adda94fda8f..e5530d52686 100644
--- a/compiler/rustc_passes/src/stability.rs
+++ b/compiler/rustc_passes/src/stability.rs
@@ -802,12 +802,28 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
                     // FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
                     let const_stab = attrs::find_attr!(attrs, AttributeKind::ConstStability{stability, ..} => *stability);
 
+                    let unstable_feature_stab =
+                        find_attr!(attrs, AttributeKind::UnstableFeatureBound(i) => i)
+                            .map(|i| i.as_slice())
+                            .unwrap_or_default();
+
                     // If this impl block has an #[unstable] attribute, give an
                     // error if all involved types and traits are stable, because
                     // it will have no effect.
                     // See: https://github.com/rust-lang/rust/issues/55436
+                    //
+                    // The exception is when there are both  #[unstable_feature_bound(..)] and
+                    //  #![unstable(feature = "..", issue = "..")] that have the same symbol because
+                    // that can effectively mark an impl as unstable.
+                    //
+                    // For example:
+                    // ```
+                    // #[unstable_feature_bound(feat_foo)]
+                    // #[unstable(feature = "feat_foo", issue = "none")]
+                    // impl Foo for Bar {}
+                    // ```
                     if let Some((
-                        Stability { level: attrs::StabilityLevel::Unstable { .. }, .. },
+                        Stability { level: attrs::StabilityLevel::Unstable { .. }, feature },
                         span,
                     )) = stab
                     {
@@ -815,9 +831,21 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
                         c.visit_ty_unambig(self_ty);
                         c.visit_trait_ref(t);
 
+                        // Skip the lint if the impl is marked as unstable using
+                        // #[unstable_feature_bound(..)]
+                        let mut unstable_feature_bound_in_effect = false;
+                        for (unstable_bound_feat_name, _) in unstable_feature_stab {
+                            if *unstable_bound_feat_name == feature {
+                                unstable_feature_bound_in_effect = true;
+                            }
+                        }
+
                         // do not lint when the trait isn't resolved, since resolution error should
                         // be fixed first
-                        if t.path.res != Res::Err && c.fully_stable {
+                        if t.path.res != Res::Err
+                            && c.fully_stable
+                            && !unstable_feature_bound_in_effect
+                        {
                             self.tcx.emit_node_span_lint(
                                 INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
                                 item.hir_id(),
diff --git a/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.rs b/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.rs
new file mode 100644
index 00000000000..99501893ae0
--- /dev/null
+++ b/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.rs
@@ -0,0 +1,35 @@
+#![allow(internal_features)]
+#![feature(staged_api)]
+#![allow(dead_code)]
+#![stable(feature = "a", since = "1.1.1" )]
+
+/// If #[unstable(..)] and #[unstable_feature_name(..)] have the same feature name,
+/// the error should not be thrown as it can effectively mark an impl as unstable.
+///
+/// If the feature name in #[feature] does not exist in #[unstable_feature_bound(..)]
+/// an error should still be thrown because that feature will not be unstable.
+
+#[stable(feature = "a", since = "1.1.1")]
+trait Moo {}
+#[stable(feature = "a", since = "1.1.1")]
+trait Foo {}
+#[stable(feature = "a", since = "1.1.1")]
+trait Boo {}
+#[stable(feature = "a", since = "1.1.1")]
+pub struct Bar;
+
+
+#[unstable(feature = "feat_moo", issue = "none")]
+#[unstable_feature_bound(feat_foo)] //~^ ERROR: an `#[unstable]` annotation here has no effect
+impl Moo for Bar {}
+
+#[unstable(feature = "feat_foo", issue = "none")]
+#[unstable_feature_bound(feat_foo)]
+impl Foo for Bar {}
+
+
+#[unstable(feature = "feat_foo", issue = "none")]
+#[unstable_feature_bound(feat_foo, feat_bar)]
+impl Boo for Bar {}
+
+fn main() {}
diff --git a/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.stderr b/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.stderr
new file mode 100644
index 00000000000..4c8af2bbe56
--- /dev/null
+++ b/tests/ui/unstable-feature-bound/unstable-feature-bound-no-effect.stderr
@@ -0,0 +1,11 @@
+error: an `#[unstable]` annotation here has no effect
+  --> $DIR/unstable-feature-bound-no-effect.rs:22:1
+   |
+LL | #[unstable(feature = "feat_moo", issue = "none")]
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #55436 <https://github.com/rust-lang/rust/issues/55436> for more information
+   = note: `#[deny(ineffective_unstable_trait_impl)]` on by default
+
+error: aborting due to 1 previous error
+