Skip to content

Mention implicit T: Sized bound in E0277 errors #69791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/librustc_infer/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,17 +584,29 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
))
);

let explanation =
if obligation.cause.code == ObligationCauseCode::MainFunctionType {
"consider using `()`, or a `Result`".to_owned()
} else {
format!(
"{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref.print_only_trait_path(),
trait_ref.self_ty(),
)
};
let explanation = if obligation.cause.code
== ObligationCauseCode::MainFunctionType
{
"consider using `()`, or a `Result`".to_owned()
} else if self.tcx.lang_items().sized_trait().map_or(false, |sized_id| {
let self_ty = trait_ref.self_ty();
sized_id == trait_ref.def_id()
&& matches!(self_ty.kind, ty::Param(_))
&& self_ty != self.tcx.types.self_param
}) {
// Detect type parameters with an implied `Sized` bound and explain
// it instead of giving a generic message. This will be displayed
// as a `help`.
"type parameters have an implicit `Sized` requirement by default"
.to_string()
} else {
format!(
"{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref.print_only_trait_path(),
trait_ref.self_ty(),
)
};

if self.suggest_add_reference_to_arg(
&obligation,
Expand Down
12 changes: 7 additions & 5 deletions src/librustc_infer/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
body_id: hir::HirId,
) {
let self_ty = trait_ref.self_ty();
let (param_ty, projection) = match &self_ty.kind {
ty::Param(_) => (true, None),
ty::Projection(projection) => (false, Some(projection)),
let projection = match &self_ty.kind {
ty::Param(_) => None,
ty::Projection(projection) => Some(projection),
_ => return,
};

Expand Down Expand Up @@ -64,7 +64,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
generics,
kind: hir::TraitItemKind::Method(..),
..
}) if param_ty && self_ty == self.tcx.types.self_param => {
}) if matches!(self_ty.kind, ty::Param(_))
&& self_ty == self.tcx.types.self_param =>
{
// Restricting `Self` for a single method.
suggest_restriction(&generics, "`Self`", err);
return;
Expand Down Expand Up @@ -138,7 +140,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
})
| hir::Node::TraitItem(hir::TraitItem { generics, span, .. })
| hir::Node::ImplItem(hir::ImplItem { generics, span, .. })
if param_ty =>
if matches!(self_ty.kind, ty::Param(_)) =>
{
// Missing generic type parameter bound.
let param_name = self_ty.to_string();
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_typeck/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,13 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
_ => None,
})
.collect();
let is_param = |ty: Ty<'_>| match ty.kind {
ty::Param(_) => true,
_ => false,
};
let bad_substs: Vec<_> = substs
.iter()
.enumerate()
.filter_map(|(i, k)| {
if let GenericArgKind::Type(ty) = k.unpack() { Some((i, ty)) } else { None }
})
.filter(|(_, ty)| !is_param(ty))
.filter(|(_, ty)| !matches!(ty.kind, ty::Param(_)))
.collect();
if !bad_substs.is_empty() {
let identity_substs = InternalSubsts::identity_for_item(self.tcx, self.def_id);
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/too_generic_eval_ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | impl<A, B> Foo<A, B> {
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `A`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `B` cannot be known at compilation time
Expand All @@ -38,7 +38,7 @@ LL | impl<A, B> Foo<A, B> {
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `B`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to 3 previous errors
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/dst/dst-object-from-unsized-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn test1<T: ?Sized + Foo>(t: &T) {
LL | let u: &dyn Foo = t;
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, these help messages seem to be kind of confusing to me. That is, just looking at the error message, I can't really tell what it's talking about here.

= 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 for the cast to the object type `dyn Foo`

Expand All @@ -18,7 +18,7 @@ LL | fn test2<T: ?Sized + Foo>(t: &T) {
LL | let v: &dyn Foo = t as &dyn Foo;
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= 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 for the cast to the object type `dyn Foo`

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-27060-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | pub struct Bad<T: ?Sized> {
LL | data: T,
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: the last field of a packed struct may only have a dynamically sized type if it does not need drop to be run

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/traits/trait-suggest-where-clause.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | mem::size_of::<U>();
LL | pub const fn size_of<T>() -> usize {
| - required by this bound in `std::mem::size_of`
|
= help: the trait `std::marker::Sized` is not implemented for `U`
= help: type parameters have an implicit `Sized` requirement by default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a case where it kind of makes sense, but I feel like we would ideally want it to be in place of the "required by this bound" message. e.g., something like

pub const fn size_of<T>
                     - required because of the default `Sized` requirement on this type parameter

= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `U` cannot be known at compilation time
Expand All @@ -29,7 +29,7 @@ LL | mem::size_of::<Misc<U>>();
LL | pub const fn size_of<T>() -> usize {
| - required by this bound in `std::mem::size_of`
|
= help: within `Misc<U>`, the trait `std::marker::Sized` is not implemented for `U`
= help: type parameters have an implicit `Sized` requirement by default
= 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 `Misc<U>`

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/union/union-sized-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | union Foo<T: ?Sized> {
LL | value: T,
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of a union may have a dynamically sized type

Expand All @@ -18,7 +18,7 @@ LL | struct Foo2<T: ?Sized> {
LL | value: T,
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: only the last field of a struct may have a dynamically sized type

Expand All @@ -30,7 +30,7 @@ LL | enum Foo3<T: ?Sized> {
LL | Value(T),
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of an enum variant may have a dynamically sized type

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unsized/unsized-bare-typaram.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | fn foo<T: ?Sized>() { bar::<T>() }
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unsized/unsized-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | fn foo2<T: ?Sized>() { not_sized::<Foo<T>>() }
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to previous error
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/unsized/unsized-enum2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | // parameter
LL | VA(W),
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `W`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of an enum variant may have a dynamically sized type

Expand All @@ -20,7 +20,7 @@ LL | enum E<W: ?Sized, X: ?Sized, Y: ?Sized, Z: ?Sized> {
LL | VB{x: X},
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of an enum variant may have a dynamically sized type

Expand All @@ -33,7 +33,7 @@ LL | enum E<W: ?Sized, X: ?Sized, Y: ?Sized, Z: ?Sized> {
LL | VC(isize, Y),
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `Y`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of an enum variant may have a dynamically sized type

Expand All @@ -46,7 +46,7 @@ LL | enum E<W: ?Sized, X: ?Sized, Y: ?Sized, Z: ?Sized> {
LL | VD{u: isize, x: Z},
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `Z`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: no field of an enum variant may have a dynamically sized type

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unsized/unsized-inherent-impl-self-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | impl<X: ?Sized> S5<X> {
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to previous error
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/unsized/unsized-struct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | fn foo2<T: ?Sized>() { not_sized::<Foo<T>>() }
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `T` cannot be known at compilation time
Expand All @@ -23,7 +23,7 @@ LL | fn bar2<T: ?Sized>() { is_sized::<Bar<T>>() }
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: within `Bar<T>`, the trait `std::marker::Sized` is not implemented for `T`
= help: type parameters have an implicit `Sized` requirement by default
= 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<T>`

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unsized/unsized-trait-impl-self-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | impl<X: ?Sized> T3<X> for S5<X> {
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unsized/unsized-trait-impl-trait-arg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | impl<X: ?Sized> T2<X> for S4<X> {
| |
| this type parameter needs to be `std::marker::Sized`
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error: aborting due to previous error
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/unsized3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | fn f2<X>(x: &X) {
| |
| required by this bound in `f2`
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `X` cannot be known at compilation time
Expand All @@ -27,7 +27,7 @@ LL | fn f4<X: T>(x: &X) {
| |
| required by this bound in `f4`
|
= help: the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `X` cannot be known at compilation time
Expand All @@ -41,7 +41,7 @@ LL | fn f8<X: ?Sized>(x1: &S<X>, x2: &S<X>) {
LL | f5(x1);
| ^^ doesn't have a size known at compile-time
|
= help: within `S<X>`, the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= 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>`

Expand All @@ -53,7 +53,7 @@ LL | fn f9<X: ?Sized>(x1: Box<S<X>>) {
LL | f5(&(*x1, 34));
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `S<X>`, the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= 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: only the last element of a tuple may have a dynamically sized type
Expand All @@ -66,7 +66,7 @@ LL | fn f10<X: ?Sized>(x1: Box<S<X>>) {
LL | f5(&(32, *x1));
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `({integer}, S<X>)`, the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= 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>)`
Expand All @@ -83,7 +83,7 @@ LL | fn f10<X: ?Sized>(x1: Box<S<X>>) {
LL | f5(&(32, *x1));
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `({integer}, S<X>)`, the trait `std::marker::Sized` is not implemented for `X`
= help: type parameters have an implicit `Sized` requirement by default
= 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>)`
Expand Down
Loading