Skip to content

Confusing error reported for trait impls on DST with the last field being Sized  #83097

Description

@BoxyUwU

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d9a00dc0e329f0ea73b037f3af9f26db

use std::marker::PhantomData;

trait Trait {}

struct Unsized([u8], ());
struct Foo<T: ?Sized>(PhantomData<T>);

impl<T> Trait for Foo<T> {}
impl Trait for Foo<Unsized> {}

The current output is:

error[E0119]: conflicting implementations of trait `Trait` for type `Foo<Unsized>`:
 --> src/main.rs:9:1
  |
8 | impl<T> Trait for Foo<T> {}
  | ------------------------ first implementation here
9 | impl Trait for Foo<Unsized> {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Foo<Unsized>`

Ideally the output should look like:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:1:16
  |
5 | struct Unsized([u8], ());
  |                ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: only the last field of a struct may have a dynamically sized type
  = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
  |
5 | struct Unsized(&[u8], ());
  |                ^
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
5 | struct Unsized(Box<[u8]>, ());
  |                ^^^^    ^

error: aborting due to previous error

Note: this error is particularly confusing because the trait impls *dont* overlap after fixing the field ordering error

cc @estebank

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions