Skip to content

Spurious 'conflicting implementations' error when specializing a type with a compilation error #68830

Open
@Aaron1011

Description

@Aaron1011

The following code:

#![feature(specialization)]

struct BadStruct {
    err: MissingType
}

trait MyTrait<T> {
    fn foo();
}

impl<T, D> MyTrait<T> for D {
    default fn foo() {}
}

impl<T> MyTrait<T> for BadStruct {
    fn foo() {}
}

gives the following errors:

error[E0412]: cannot find type `MissingType` in this scope
 --> src/lib.rs:4:10
  |
4 |     err: MissingType
  |          ^^^^^^^^^^^ not found in this scope

error[E0119]: conflicting implementations of trait `MyTrait<_>` for type `BadStruct`:
  --> src/lib.rs:15:1
   |
11 | impl<T, D> MyTrait<T> for D {
   | --------------------------- first implementation here
...
15 | impl<T> MyTrait<T> for BadStruct {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `BadStruct`

error: aborting due to 2 previous errors

For some reason, the fact that the definition of BadStruct has an error (MissingType is not defining) causes a 'conflicting implementations' error to be emitted when BadStruct has a specialized impl. If MissingType is changed to a type which actually exists (e.g. ()), the 'conflicting implementations' error disappears.

I found this when working on rustc - a missing use statement caused 30 spurious specialization-related errors to be emiited, which all disappeared when I added the missing import.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-specializationArea: Trait impl specializationC-bugCategory: This is a bug.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-specialization`#![feature(specialization)]`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