Skip to content

E0119 can overwrite E0277 leading to confusing error message #66712

Open
@CAD97

Description

@CAD97

Code:

trait Trait {}

struct Unsized {
    tail: [()],
    marker: (),
}

impl<T: Sized> Trait for T {}
impl Trait for Unsized {}

error:

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

Without impl Trait for Unsized:

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
 --> src/lib.rs:4:5
  |
4 |     tail: [()],
  |     ^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[()]`
  = 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

The better behavior would be to

  • Make sure that the E0277 is emitted even in the case of other errors.
  • When finding an E0277 struct definition, treat it as if it were ?Sized through the rest of the pipeline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-DSTsArea: Dynamically-sized types (DSTs)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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