about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBryanskiy <ivakin.kir@gmail.com>2025-08-24 21:29:26 +0300
committerBryanskiy <ivakin.kir@gmail.com>2025-09-10 15:08:08 +0300
commit3ab7b397bbd27d220d1eebc1b21963235dd26711 (patch)
treec1fb5eea0054a90677bb1c5596ffd36363aef965
parentbd089e1e6e52256c0535f19f58b4b6fe9609b70c (diff)
downloadrust-3ab7b397bbd27d220d1eebc1b21963235dd26711.tar.gz
rust-3ab7b397bbd27d220d1eebc1b21963235dd26711.zip
Permit `more_maybe_bounds` in supertraits and trait objects only
-rw-r--r--compiler/rustc_ast_lowering/src/lib.rs15
-rw-r--r--tests/ui/trait-bounds/more_maybe_bounds.rs14
-rw-r--r--tests/ui/trait-bounds/more_maybe_bounds.stderr32
-rw-r--r--tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs7
-rw-r--r--tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr10
5 files changed, 60 insertions, 18 deletions
diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs
index 72f20a95ff0..4e2243e8787 100644
--- a/compiler/rustc_ast_lowering/src/lib.rs
+++ b/compiler/rustc_ast_lowering/src/lib.rs
@@ -2101,17 +2101,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
                 {
                     return;
                 }
-                if self.tcx.features().more_maybe_bounds() {
-                    return;
-                }
             }
             RelaxedBoundPolicy::Forbidden(reason) => {
-                if self.tcx.features().more_maybe_bounds() {
-                    return;
-                }
-
                 match reason {
                     RelaxedBoundForbiddenReason::TraitObjectTy => {
+                        if self.tcx.features().more_maybe_bounds() {
+                            return;
+                        }
+
                         self.dcx().span_err(
                             span,
                             "relaxed bounds are not permitted in trait object types",
@@ -2119,6 +2116,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
                         return;
                     }
                     RelaxedBoundForbiddenReason::SuperTrait => {
+                        if self.tcx.features().more_maybe_bounds() {
+                            return;
+                        }
+
                         let mut diag = self.dcx().struct_span_err(
                             span,
                             "relaxed bounds are not permitted in supertrait bounds",
diff --git a/tests/ui/trait-bounds/more_maybe_bounds.rs b/tests/ui/trait-bounds/more_maybe_bounds.rs
index d367dd5b299..ddd4313bd5e 100644
--- a/tests/ui/trait-bounds/more_maybe_bounds.rs
+++ b/tests/ui/trait-bounds/more_maybe_bounds.rs
@@ -21,6 +21,20 @@ fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
 
 // FIXME: `?Trait1` should be rejected, `Trait1` isn't marked `#[lang = "default_traitN"]`.
 fn baz<T>() where T: Iterator<Item: ?Trait1> {}
+//~^ ERROR this relaxed bound is not permitted here
+
+struct S1<T>(T);
+
+impl<T> S1<T> {
+    fn f() where T: ?Trait1 {}
+    //~^ ERROR this relaxed bound is not permitted here
+}
+
+trait Trait5<'a> {}
+
+struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
+//~^ ERROR this relaxed bound is not permitted here
+//~| ERROR bound modifier `?` can only be applied to default traits like `Sized`
 
 struct S;
 impl !Trait2 for S {}
diff --git a/tests/ui/trait-bounds/more_maybe_bounds.stderr b/tests/ui/trait-bounds/more_maybe_bounds.stderr
index 8dd83fc7728..0d78cfd5820 100644
--- a/tests/ui/trait-bounds/more_maybe_bounds.stderr
+++ b/tests/ui/trait-bounds/more_maybe_bounds.stderr
@@ -1,3 +1,27 @@
+error: this relaxed bound is not permitted here
+  --> $DIR/more_maybe_bounds.rs:23:37
+   |
+LL | fn baz<T>() where T: Iterator<Item: ?Trait1> {}
+   |                                     ^^^^^^^
+   |
+   = note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
+
+error: this relaxed bound is not permitted here
+  --> $DIR/more_maybe_bounds.rs:29:21
+   |
+LL |     fn f() where T: ?Trait1 {}
+   |                     ^^^^^^^
+   |
+   = note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
+
+error: this relaxed bound is not permitted here
+  --> $DIR/more_maybe_bounds.rs:35:34
+   |
+LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
+   |                                  ^^^^^^^^^^^
+   |
+   = note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
+
 error: bound modifier `?` can only be applied to default traits like `Sized`
   --> $DIR/more_maybe_bounds.rs:17:20
    |
@@ -16,5 +40,11 @@ error: bound modifier `?` can only be applied to default traits like `Sized`
 LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
    |                                        ^^^^^^^
 
-error: aborting due to 3 previous errors
+error: bound modifier `?` can only be applied to default traits like `Sized`
+  --> $DIR/more_maybe_bounds.rs:35:34
+   |
+LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
+   |                                  ^^^^^^^^^^^
+
+error: aborting due to 7 previous errors
 
diff --git a/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs b/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs
index 820132b4e54..ac4c4aca2ef 100644
--- a/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs
+++ b/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs
@@ -42,11 +42,8 @@ struct LeakS;
 mod supertraits {
     use crate::*;
 
-    trait MaybeLeakT1: ?Leak {}
-    trait MaybeLeakT2 where Self: ?Leak {}
-
-    impl MaybeLeakT1 for NonLeakS {}
-    impl MaybeLeakT2 for NonLeakS {}
+    trait MaybeLeak: ?Leak {}
+    impl MaybeLeak for NonLeakS {}
 
     trait LeakT {}
     impl LeakT for NonLeakS {}
diff --git a/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr b/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr
index 95c77b1756c..ab62ab81b21 100644
--- a/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr
+++ b/tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr
@@ -1,5 +1,5 @@
 error[E0277]: the trait bound `NonLeakS: Leak` is not satisfied
-  --> $DIR/maybe-bounds-in-traits.rs:52:20
+  --> $DIR/maybe-bounds-in-traits.rs:49:20
    |
 LL |     impl LeakT for NonLeakS {}
    |                    ^^^^^^^^ unsatisfied trait bound
@@ -10,13 +10,13 @@ help: the trait `Leak` is not implemented for `NonLeakS`
 LL | struct NonLeakS;
    | ^^^^^^^^^^^^^^^
 note: required by a bound in `LeakT`
-  --> $DIR/maybe-bounds-in-traits.rs:51:5
+  --> $DIR/maybe-bounds-in-traits.rs:48:5
    |
 LL |     trait LeakT {}
    |     ^^^^^^^^^^^^^^ required by this bound in `LeakT`
 
 error[E0277]: the trait bound `NonLeakS: Leak` is not satisfied
-  --> $DIR/maybe-bounds-in-traits.rs:61:22
+  --> $DIR/maybe-bounds-in-traits.rs:58:22
    |
 LL |         type Leak2 = NonLeakS;
    |                      ^^^^^^^^ unsatisfied trait bound
@@ -27,13 +27,13 @@ help: the trait `Leak` is not implemented for `NonLeakS`
 LL | struct NonLeakS;
    | ^^^^^^^^^^^^^^^
 note: required by a bound in `Test1::Leak2`
-  --> $DIR/maybe-bounds-in-traits.rs:61:9
+  --> $DIR/maybe-bounds-in-traits.rs:58:9
    |
 LL |         type Leak2 = NonLeakS;
    |         ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Test1::Leak2`
 
 error[E0658]: `&mut Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature
-  --> $DIR/maybe-bounds-in-traits.rs:80:20
+  --> $DIR/maybe-bounds-in-traits.rs:77:20
    |
 LL |         fn mut_foo(&mut self) {}
    |                    ^^^^^^^^^