Skip to content

Commit d2b8e60

Browse files
committed
Account for derived obligations to suggest ?Sized bound
Fix #27964.
1 parent 95e5605 commit d2b8e60

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

src/librustc_trait_selection/traits/error_reporting/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1696,14 +1696,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
16961696
err: &mut DiagnosticBuilder<'tcx>,
16971697
obligation: &PredicateObligation<'tcx>,
16981698
) {
1699-
let (pred, item_def_id, span) = match (obligation.predicate.kind(), &obligation.cause.code)
1700-
{
1701-
(
1702-
ty::PredicateKind::Trait(pred, _),
1703-
ObligationCauseCode::BindingObligation(item_def_id, span),
1704-
) => (pred, item_def_id, span),
1705-
_ => return,
1706-
};
1699+
let (pred, item_def_id, span) =
1700+
match (obligation.predicate.kind(), &obligation.cause.code.peel_derives()) {
1701+
(
1702+
ty::PredicateKind::Trait(pred, _),
1703+
ObligationCauseCode::BindingObligation(item_def_id, span),
1704+
) => (pred, item_def_id, span),
1705+
_ => return,
1706+
};
17071707

17081708
let node = match (
17091709
self.tcx.hir().get_if_local(*item_def_id),

src/test/ui/dst/dst-sized-trait-param.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | impl Foo<[isize]> for usize { }
99
|
1010
= help: the trait `std::marker::Sized` is not implemented for `[isize]`
1111
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
12+
help: consider relaxing the implicit `Sized` restriction
13+
|
14+
LL | trait Foo<T: ?Sized> : Sized { fn take(self, x: &T) { } } // Note: T is sized
15+
| ^^^^^^^^
1216

1317
error[E0277]: the size for values of type `[usize]` cannot be known at compilation time
1418
--> $DIR/dst-sized-trait-param.rs:10:6

src/test/ui/extern/extern-types-unsized.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ LL | assert_sized::<Foo>();
2626
= help: within `Foo`, the trait `std::marker::Sized` is not implemented for `A`
2727
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
2828
= note: required because it appears within the type `Foo`
29+
help: consider relaxing the implicit `Sized` restriction
30+
|
31+
LL | fn assert_sized<T: ?Sized>() { }
32+
| ^^^^^^^^
2933

3034
error[E0277]: the size for values of type `A` cannot be known at compilation time
3135
--> $DIR/extern-types-unsized.rs:28:5
@@ -39,6 +43,10 @@ LL | assert_sized::<Bar<A>>();
3943
= help: within `Bar<A>`, the trait `std::marker::Sized` is not implemented for `A`
4044
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
4145
= note: required because it appears within the type `Bar<A>`
46+
help: consider relaxing the implicit `Sized` restriction
47+
|
48+
LL | fn assert_sized<T: ?Sized>() { }
49+
| ^^^^^^^^
4250

4351
error[E0277]: the size for values of type `A` cannot be known at compilation time
4452
--> $DIR/extern-types-unsized.rs:31:5
@@ -53,6 +61,10 @@ LL | assert_sized::<Bar<Bar<A>>>();
5361
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
5462
= note: required because it appears within the type `Bar<A>`
5563
= note: required because it appears within the type `Bar<Bar<A>>`
64+
help: consider relaxing the implicit `Sized` restriction
65+
|
66+
LL | fn assert_sized<T: ?Sized>() { }
67+
| ^^^^^^^^
5668

5769
error: aborting due to 4 previous errors
5870

src/test/ui/issues/issue-10412.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ LL | impl<'self> Serializable<str> for &'self str {
5757
|
5858
= help: the trait `std::marker::Sized` is not implemented for `str`
5959
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
60+
help: consider relaxing the implicit `Sized` restriction
61+
|
62+
LL | trait Serializable<'self, T: ?Sized> {
63+
| ^^^^^^^^
6064

6165
error: aborting due to 9 previous errors
6266

src/test/ui/unsized/unsized-trait-impl-trait-arg.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ LL | impl<X: ?Sized> T2<X> for S4<X> {
1111
|
1212
= help: the trait `std::marker::Sized` is not implemented for `X`
1313
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
14+
help: consider relaxing the implicit `Sized` restriction
15+
|
16+
LL | trait T2<Z: ?Sized> {
17+
| ^^^^^^^^
1418

1519
error: aborting due to previous error
1620

src/test/ui/unsized3.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ LL | f5(x1);
4848
= help: within `S<X>`, the trait `std::marker::Sized` is not implemented for `X`
4949
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
5050
= note: required because it appears within the type `S<X>`
51+
help: consider relaxing the implicit `Sized` restriction
52+
|
53+
LL | fn f5<Y: ?Sized>(x: &Y) {}
54+
| ^^^^^^^^
5155

5256
error[E0277]: the size for values of type `X` cannot be known at compilation time
5357
--> $DIR/unsized3.rs:40:8
@@ -91,6 +95,10 @@ LL | f5(&(32, *x1));
9195
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9296
= note: required because it appears within the type `S<X>`
9397
= note: required because it appears within the type `({integer}, S<X>)`
98+
help: consider relaxing the implicit `Sized` restriction
99+
|
100+
LL | fn f5<Y: ?Sized>(x: &Y) {}
101+
| ^^^^^^^^
94102

95103
error: aborting due to 6 previous errors
96104

src/test/ui/unsized7.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ LL | impl<X: ?Sized + T> T1<X> for S3<X> {
1111
|
1212
= help: the trait `std::marker::Sized` is not implemented for `X`
1313
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
14+
help: consider relaxing the implicit `Sized` restriction
15+
|
16+
LL | trait T1<Z: T + ?Sized> {
17+
| ^^^^^^^^
1418

1519
error: aborting due to previous error
1620

0 commit comments

Comments
 (0)