Skip to content

Commit 690a8a6

Browse files
committed
Tweak diagnostics for relaxed bounds in invalid positions
1 parent 7a52736 commit 690a8a6

File tree

7 files changed

+49
-35
lines changed

7 files changed

+49
-35
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,33 +2103,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21032103
}
21042104
}
21052105
RelaxedBoundPolicy::Forbidden(reason) => {
2106+
let gate = |context, subject| {
2107+
if self.tcx.features().more_maybe_bounds() {
2108+
return;
2109+
}
2110+
2111+
let mut diag = self.dcx().struct_span_err(
2112+
span,
2113+
format!("relaxed bounds are not permitted in {context}"),
2114+
);
2115+
if let Some(def_id) = trait_ref.trait_def_id()
2116+
&& self.tcx.is_lang_item(def_id, hir::LangItem::Sized)
2117+
{
2118+
diag.note(format!(
2119+
"{subject} are not implicitly bounded by `Sized`, \
2120+
so there is nothing to relax"
2121+
));
2122+
}
2123+
diag.emit();
2124+
};
2125+
21062126
match reason {
21072127
RelaxedBoundForbiddenReason::TraitObjectTy => {
2108-
if self.tcx.features().more_maybe_bounds() {
2109-
return;
2110-
}
2111-
2112-
self.dcx().span_err(
2113-
span,
2114-
"relaxed bounds are not permitted in trait object types",
2115-
);
2128+
gate("trait object types", "trait object types");
21162129
return;
21172130
}
21182131
RelaxedBoundForbiddenReason::SuperTrait => {
2119-
if self.tcx.features().more_maybe_bounds() {
2120-
return;
2121-
}
2122-
2123-
let mut diag = self.dcx().struct_span_err(
2124-
span,
2125-
"relaxed bounds are not permitted in supertrait bounds",
2126-
);
2127-
if let Some(def_id) = trait_ref.trait_def_id()
2128-
&& self.tcx.is_lang_item(def_id, hir::LangItem::Sized)
2129-
{
2130-
diag.note("traits are `?Sized` by default");
2131-
}
2132-
diag.emit();
2132+
gate("supertrait bounds", "traits");
21332133
return;
21342134
}
21352135
RelaxedBoundForbiddenReason::AssocTyBounds
@@ -2142,7 +2142,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21422142
.struct_span_err(span, "this relaxed bound is not permitted here")
21432143
.with_note(
21442144
"in this context, relaxed bounds are only allowed on \
2145-
type parameters defined by the closest item",
2145+
type parameters defined on the closest item",
21462146
)
21472147
.emit();
21482148
}

tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: this relaxed bound is not permitted here
1010
LL | trait Trait4 where Self: ?Trait1 {}
1111
| ^^^^^^^
1212
|
13-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
13+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
1414

1515
error: relaxed bounds are not permitted in trait object types
1616
--> $DIR/feature-gate-more-maybe-bounds.rs:8:28

tests/ui/parser/trait-object-trait-parens.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ error: relaxed bounds are not permitted in trait object types
33
|
44
LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
55
| ^^^^^^^^
6+
|
7+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
68

79
error: relaxed bounds are not permitted in trait object types
810
--> $DIR/trait-object-trait-parens.rs:13:16
911
|
1012
LL | let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
1113
| ^^^^^^
14+
|
15+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
1216

1317
error: relaxed bounds are not permitted in trait object types
1418
--> $DIR/trait-object-trait-parens.rs:18:44
1519
|
1620
LL | let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
1721
| ^^^^^^^^
22+
|
23+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
1824

1925
warning: trait objects without an explicit `dyn` are deprecated
2026
--> $DIR/trait-object-trait-parens.rs:8:16

tests/ui/sized-hierarchy/default-supertrait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: relaxed bounds are not permitted in supertrait bounds
44
LL | trait NegSized: ?Sized { }
55
| ^^^^^^
66
|
7-
= note: traits are `?Sized` by default
7+
= note: traits are not implicitly bounded by `Sized`, so there is nothing to relax
88

99
error: relaxed bounds are not permitted in supertrait bounds
1010
--> $DIR/default-supertrait.rs:13:21

tests/ui/trait-bounds/more_maybe_bounds.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ error: this relaxed bound is not permitted here
44
LL | fn baz<T>() where T: Iterator<Item: ?Trait1> {}
55
| ^^^^^^^
66
|
7-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
7+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
88

99
error: this relaxed bound is not permitted here
1010
--> $DIR/more_maybe_bounds.rs:29:21
1111
|
1212
LL | fn f() where T: ?Trait1 {}
1313
| ^^^^^^^
1414
|
15-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
15+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
1616

1717
error: this relaxed bound is not permitted here
1818
--> $DIR/more_maybe_bounds.rs:35:34
1919
|
2020
LL | struct S2<T>(T) where for<'a> T: ?Trait5<'a>;
2121
| ^^^^^^^^^^^
2222
|
23-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
23+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
2424

2525
error: bound modifier `?` can only be applied to default traits like `Sized`
2626
--> $DIR/more_maybe_bounds.rs:17:20

tests/ui/traits/wf-object/only-maybe-bound.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: relaxed bounds are not permitted in trait object types
33
|
44
LL | type _0 = dyn ?Sized;
55
| ^^^^^^
6+
|
7+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
68

79
error[E0224]: at least one trait is required for an object type
810
--> $DIR/only-maybe-bound.rs:3:11

tests/ui/unsized/relaxed-bounds-invalid-places.stderr

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,79 @@ error: this relaxed bound is not permitted here
44
LL | struct S1<T>(T) where (T): ?Sized;
55
| ^^^^^^
66
|
7-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
7+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
88

99
error: this relaxed bound is not permitted here
1010
--> $DIR/relaxed-bounds-invalid-places.rs:8:27
1111
|
1212
LL | struct S2<T>(T) where u8: ?Sized;
1313
| ^^^^^^
1414
|
15-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
15+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
1616

1717
error: this relaxed bound is not permitted here
1818
--> $DIR/relaxed-bounds-invalid-places.rs:10:35
1919
|
2020
LL | struct S3<T>(T) where &'static T: ?Sized;
2121
| ^^^^^^
2222
|
23-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
23+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
2424

2525
error: this relaxed bound is not permitted here
2626
--> $DIR/relaxed-bounds-invalid-places.rs:14:34
2727
|
2828
LL | struct S4<T>(T) where for<'a> T: ?Trait<'a>;
2929
| ^^^^^^^^^^
3030
|
31-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
31+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
3232

3333
error: this relaxed bound is not permitted here
3434
--> $DIR/relaxed-bounds-invalid-places.rs:22:21
3535
|
3636
LL | fn f() where T: ?Sized {}
3737
| ^^^^^^
3838
|
39-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
39+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
4040

4141
error: this relaxed bound is not permitted here
4242
--> $DIR/relaxed-bounds-invalid-places.rs:27:41
4343
|
4444
LL | struct S6<T>(T) where T: Iterator<Item: ?Sized>;
4545
| ^^^^^^
4646
|
47-
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
47+
= note: in this context, relaxed bounds are only allowed on type parameters defined on the closest item
4848

4949
error: relaxed bounds are not permitted in supertrait bounds
5050
--> $DIR/relaxed-bounds-invalid-places.rs:29:11
5151
|
5252
LL | trait Tr: ?Sized {}
5353
| ^^^^^^
5454
|
55-
= note: traits are `?Sized` by default
55+
= note: traits are not implicitly bounded by `Sized`, so there is nothing to relax
5656

5757
error: relaxed bounds are not permitted in trait object types
5858
--> $DIR/relaxed-bounds-invalid-places.rs:33:20
5959
|
6060
LL | type O1 = dyn Tr + ?Sized;
6161
| ^^^^^^
62+
|
63+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
6264

6365
error: relaxed bounds are not permitted in trait object types
6466
--> $DIR/relaxed-bounds-invalid-places.rs:34:15
6567
|
6668
LL | type O2 = dyn ?Sized + ?Sized + Tr;
6769
| ^^^^^^
70+
|
71+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
6872

6973
error: relaxed bounds are not permitted in trait object types
7074
--> $DIR/relaxed-bounds-invalid-places.rs:34:24
7175
|
7276
LL | type O2 = dyn ?Sized + ?Sized + Tr;
7377
| ^^^^^^
78+
|
79+
= note: trait object types are not implicitly bounded by `Sized`, so there is nothing to relax
7480

7581
error: bound modifier `?` can only be applied to `Sized`
7682
--> $DIR/relaxed-bounds-invalid-places.rs:14:34

0 commit comments

Comments
 (0)