Skip to content
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

Trait impls conflict even with different associated const bounds #103292

Open
joshlf opened this issue Oct 20, 2022 · 1 comment
Open

Trait impls conflict even with different associated const bounds #103292

joshlf opened this issue Oct 20, 2022 · 1 comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. F-associated_const_equality `#![feature(associated_const_equality)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@joshlf
Copy link
Contributor

joshlf commented Oct 20, 2022

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

@Rageking8
Copy link
Contributor

@rustbot label +C-bug +T-compiler +F-associated_const_equality

@rustbot rustbot added C-bug Category: This is a bug. F-associated_const_equality `#![feature(associated_const_equality)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2022
@fmease fmease added A-traits Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. F-associated_const_equality `#![feature(associated_const_equality)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants