Navigation Menu

Skip to content

Commit

Permalink
Account for derived obligations to suggest ?Sized bound
Browse files Browse the repository at this point in the history
Fix #27964.
  • Loading branch information
estebank committed Jun 16, 2020
1 parent 95e5605 commit d2b8e60
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/librustc_trait_selection/traits/error_reporting/mod.rs
Expand Up @@ -1696,14 +1696,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'tcx>,
obligation: &PredicateObligation<'tcx>,
) {
let (pred, item_def_id, span) = match (obligation.predicate.kind(), &obligation.cause.code)
{
(
ty::PredicateKind::Trait(pred, _),
ObligationCauseCode::BindingObligation(item_def_id, span),
) => (pred, item_def_id, span),
_ => return,
};
let (pred, item_def_id, span) =
match (obligation.predicate.kind(), &obligation.cause.code.peel_derives()) {
(
ty::PredicateKind::Trait(pred, _),
ObligationCauseCode::BindingObligation(item_def_id, span),
) => (pred, item_def_id, span),
_ => return,
};

let node = match (
self.tcx.hir().get_if_local(*item_def_id),
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/dst/dst-sized-trait-param.stderr
Expand Up @@ -9,6 +9,10 @@ LL | impl Foo<[isize]> for usize { }
|
= help: the trait `std::marker::Sized` is not implemented for `[isize]`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
help: consider relaxing the implicit `Sized` restriction
|
LL | trait Foo<T: ?Sized> : Sized { fn take(self, x: &T) { } } // Note: T is sized
| ^^^^^^^^

error[E0277]: the size for values of type `[usize]` cannot be known at compilation time
--> $DIR/dst-sized-trait-param.rs:10:6
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/extern/extern-types-unsized.stderr
Expand Up @@ -26,6 +26,10 @@ LL | assert_sized::<Foo>();
= help: within `Foo`, the trait `std::marker::Sized` is not implemented for `A`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because it appears within the type `Foo`
help: consider relaxing the implicit `Sized` restriction
|
LL | fn assert_sized<T: ?Sized>() { }
| ^^^^^^^^

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

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

error: aborting due to 4 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issues/issue-10412.stderr
Expand Up @@ -57,6 +57,10 @@ LL | impl<'self> Serializable<str> for &'self str {
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
help: consider relaxing the implicit `Sized` restriction
|
LL | trait Serializable<'self, T: ?Sized> {
| ^^^^^^^^

error: aborting due to 9 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/unsized/unsized-trait-impl-trait-arg.stderr
Expand Up @@ -11,6 +11,10 @@ LL | impl<X: ?Sized> T2<X> for S4<X> {
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
help: consider relaxing the implicit `Sized` restriction
|
LL | trait T2<Z: ?Sized> {
| ^^^^^^^^

error: aborting due to previous error

Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/unsized3.stderr
Expand Up @@ -48,6 +48,10 @@ LL | f5(x1);
= help: within `S<X>`, the trait `std::marker::Sized` is not implemented for `X`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because it appears within the type `S<X>`
help: consider relaxing the implicit `Sized` restriction
|
LL | fn f5<Y: ?Sized>(x: &Y) {}
| ^^^^^^^^

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

error: aborting due to 6 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/unsized7.stderr
Expand Up @@ -11,6 +11,10 @@ LL | impl<X: ?Sized + T> T1<X> for S3<X> {
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
help: consider relaxing the implicit `Sized` restriction
|
LL | trait T1<Z: T + ?Sized> {
| ^^^^^^^^

error: aborting due to previous error

Expand Down

0 comments on commit d2b8e60

Please sign in to comment.