Skip to content

Trait impls conflict even with different associated const bounds #103292

Open
@joshlf

Description

@joshlf

I have the following code:

#![feature(associated_const_equality)]

pub trait Align {
    const ALIGN: usize;
}

impl<T> Align for T {
    const ALIGN: usize = std::mem::size_of::<T>();
}

pub trait AlignedTo<const N: usize> {}

impl<T: Align<ALIGN=1>> AlignedTo<1> for T {}
impl<T: Align<ALIGN=2>> AlignedTo<2> for T {}
impl<T: Align<ALIGN=2>> AlignedTo<1> for T {}
impl<T: Align<ALIGN=4>> AlignedTo<4> for T {}
impl<T: Align<ALIGN=4>> AlignedTo<2> for T {}
impl<T: Align<ALIGN=4>> AlignedTo<1> for T {}

I would expect that this code would be accepted. While there are multiple blanket impls of AlignedTo<1> and AlignedTo<2>, they're bounded by mutually-exclusive bounds - e.g., a type cannot be both Align<ALIGN=1> and Align<ALIGN=2> at the same time. However, I get this error:

error[[E0119]](https://doc.rust-lang.org/nightly/error-index.html#E0119): conflicting implementations of trait `AlignedTo<1>`
  --> src/lib.rs:15:1
   |
13 | impl<T: Align<ALIGN=1>> AlignedTo<1> for T {}
   | ------------------------------------------ first implementation here
14 | impl<T: Align<ALIGN=2>> AlignedTo<2> for T {}
15 | impl<T: Align<ALIGN=2>> AlignedTo<1> for T {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

error[[E0119]](https://doc.rust-lang.org/nightly/error-index.html#E0119): conflicting implementations of trait `AlignedTo<2>`
  --> src/lib.rs:17:1
   |
14 | impl<T: Align<ALIGN=2>> AlignedTo<2> for T {}
   | ------------------------------------------ first implementation here
...
17 | impl<T: Align<ALIGN=4>> AlignedTo<2> for T {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

error[[E0119]](https://doc.rust-lang.org/nightly/error-index.html#E0119): conflicting implementations of trait `AlignedTo<1>`
  --> src/lib.rs:18:1
   |
13 | impl<T: Align<ALIGN=1>> AlignedTo<1> for T {}
   | ------------------------------------------ first implementation here
...
18 | impl<T: Align<ALIGN=4>> AlignedTo<1> for T {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

Possibly related to #20400.

Version

Nightly 2022-10-19 4b8f431

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-associated_const_equality`#![feature(associated_const_equality)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Can Do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions