Skip to content

Commit 55e0947

Browse files
committed
Permit more_maybe_bounds in supertraits and trait objects only
1 parent 2bb97c0 commit 55e0947

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,24 +2101,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21012101
{
21022102
return;
21032103
}
2104-
if self.tcx.features().more_maybe_bounds() {
2105-
return;
2106-
}
21072104
}
21082105
RelaxedBoundPolicy::Forbidden(reason) => {
2109-
if self.tcx.features().more_maybe_bounds() {
2110-
return;
2111-
}
2112-
21132106
match reason {
21142107
RelaxedBoundForbiddenReason::TraitObjectTy => {
2108+
if self.tcx.features().more_maybe_bounds() {
2109+
return;
2110+
}
2111+
21152112
self.dcx().span_err(
21162113
span,
21172114
"relaxed bounds are not permitted in trait object types",
21182115
);
21192116
return;
21202117
}
21212118
RelaxedBoundForbiddenReason::SuperTrait => {
2119+
if self.tcx.features().more_maybe_bounds() {
2120+
return;
2121+
}
2122+
21222123
let mut diag = self.dcx().struct_span_err(
21232124
span,
21242125
"relaxed bounds are not permitted in supertrait bounds",

tests/ui/trait-bounds/more_maybe_bounds.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
2121

2222
// FIXME: `?Trait1` should be rejected, `Trait1` isn't marked `#[lang = "default_traitN"]`.
2323
fn baz<T>() where T: Iterator<Item: ?Trait1> {}
24+
//~^ ERROR this relaxed bound is not permitted here
25+
26+
struct S1<T>(T);
27+
28+
impl<T> S1<T> {
29+
fn f() where T: ?Trait1 {}
30+
//~^ ERROR this relaxed bound is not permitted here
31+
}
32+
33+
trait Trait5<'a> {}
34+
35+
struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
36+
//~^ ERROR this relaxed bound is not permitted here
37+
//~| ERROR bound modifier `?` can only be applied to default traits like `Sized`
2438

2539
struct S;
2640
impl !Trait2 for S {}

tests/ui/trait-bounds/more_maybe_bounds.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
error: this relaxed bound is not permitted here
2+
--> $DIR/more_maybe_bounds.rs:23:37
3+
|
4+
LL | fn baz<T>() where T: Iterator<Item: ?Trait1> {}
5+
| ^^^^^^^
6+
|
7+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
8+
9+
error: this relaxed bound is not permitted here
10+
--> $DIR/more_maybe_bounds.rs:29:21
11+
|
12+
LL | fn f() where T: ?Trait1 {}
13+
| ^^^^^^^
14+
|
15+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
16+
17+
error: this relaxed bound is not permitted here
18+
--> $DIR/more_maybe_bounds.rs:35:34
19+
|
20+
LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
21+
| ^^^^^^^^^^^
22+
|
23+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
24+
125
error: bound modifier `?` can only be applied to default traits like `Sized`
226
--> $DIR/more_maybe_bounds.rs:17:20
327
|
@@ -16,5 +40,11 @@ error: bound modifier `?` can only be applied to default traits like `Sized`
1640
LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
1741
| ^^^^^^^
1842

19-
error: aborting due to 3 previous errors
43+
error: bound modifier `?` can only be applied to default traits like `Sized`
44+
--> $DIR/more_maybe_bounds.rs:35:34
45+
|
46+
LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
47+
| ^^^^^^^^^^^
48+
49+
error: aborting due to 7 previous errors
2050

tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ struct LeakS;
4242
mod supertraits {
4343
use crate::*;
4444

45-
trait MaybeLeakT1: ?Leak {}
46-
trait MaybeLeakT2 where Self: ?Leak {}
47-
48-
impl MaybeLeakT1 for NonLeakS {}
49-
impl MaybeLeakT2 for NonLeakS {}
45+
trait MaybeLeak: ?Leak {}
46+
impl MaybeLeak for NonLeakS {}
5047

5148
trait LeakT {}
5249
impl LeakT for NonLeakS {}

tests/ui/traits/default_auto_traits/maybe-bounds-in-traits.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error[E0277]: the trait bound `NonLeakS: Leak` is not satisfied
2-
--> $DIR/maybe-bounds-in-traits.rs:52:20
2+
--> $DIR/maybe-bounds-in-traits.rs:49:20
33
|
44
LL | impl LeakT for NonLeakS {}
55
| ^^^^^^^^ the trait `Leak` is not implemented for `NonLeakS`
66
|
77
note: required by a bound in `LeakT`
8-
--> $DIR/maybe-bounds-in-traits.rs:51:5
8+
--> $DIR/maybe-bounds-in-traits.rs:48:5
99
|
1010
LL | trait LeakT {}
1111
| ^^^^^^^^^^^^^^ required by this bound in `LeakT`
1212

1313
error[E0277]: the trait bound `NonLeakS: Leak` is not satisfied
14-
--> $DIR/maybe-bounds-in-traits.rs:61:22
14+
--> $DIR/maybe-bounds-in-traits.rs:58:22
1515
|
1616
LL | type Leak2 = NonLeakS;
1717
| ^^^^^^^^ the trait `Leak` is not implemented for `NonLeakS`
1818
|
1919
note: required by a bound in `Test1::Leak2`
20-
--> $DIR/maybe-bounds-in-traits.rs:61:9
20+
--> $DIR/maybe-bounds-in-traits.rs:58:9
2121
|
2222
LL | type Leak2 = NonLeakS;
2323
| ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Test1::Leak2`
2424

2525
error[E0658]: `&mut Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature
26-
--> $DIR/maybe-bounds-in-traits.rs:80:20
26+
--> $DIR/maybe-bounds-in-traits.rs:77:20
2727
|
2828
LL | fn mut_foo(&mut self) {}
2929
| ^^^^^^^^^

0 commit comments

Comments
 (0)