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

-Ztrait-solver=next-coherence allows overlapping impls #118173

Closed
cynecx opened this issue Nov 22, 2023 · 2 comments
Closed

-Ztrait-solver=next-coherence allows overlapping impls #118173

cynecx opened this issue Nov 22, 2023 · 2 comments
Labels
C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@cynecx
Copy link
Contributor

cynecx commented Nov 22, 2023

trait Foo<'a, K: ?Sized> {
    fn foo(self);
}

// K=str => &'a [&'a str]: Foo<'a, str>
impl<'a, K: ?Sized> Foo<'a, K> for &'a [&'a K] {
    fn foo(self) {
        println!("a");
    }
}

// K=&'a str => &'a [&'a str]: Foo<'a, str>
impl<'a, K: Owned<'a>> Foo<'a, K::Ref> for &'a [K] {
    fn foo(self) {
        println!("b");
    }
}

trait Owned<'a> {
    type Ref: ?Sized;
}

impl<'a> Owned<'a> for &'a str {
    type Ref = str;
}

fn main() {
    let a = &[""] as &[_];
    Foo::<'static, str>::foo(a);
}

https://godbolt.org/z/9Khvh38oK

With -Ztrait-solver=next-coherence:

error[E0283]: type annotations needed: cannot satisfy `&[&str]: Foo<'static, str>`
  --> <source>:29:5
   |
29 |     Foo::<'static, str>::foo(a);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: multiple `impl`s satisfying `&[&str]: Foo<'static, str>` found
  --> <source>:6:1
   |
6  | impl<'a, K: ?Sized> Foo<'a, K> for &'a [&'a K] {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
13 | impl<'a, K: Owned<'a>> Foo<'a, K::Ref> for &'a [K] {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.
Compiler returned: 1

With -Ztrait-solver=next: Compiles.

With classic solver (As expected):

error[E0119]: conflicting implementations of trait `Foo<'_, _>` for type `&[&_]`
  --> <source>:13:1
   |
6  | impl<'a, K: ?Sized> Foo<'a, K> for &'a [&'a K] {
   | ---------------------------------------------- first implementation here
...
13 | impl<'a, K: Owned<'a>> Foo<'a, K::Ref> for &'a [K] {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&[&_]`

error: aborting due to previous error
rustc 1.76.0-nightly (2f8d81f9d 2023-11-21)
binary: rustc
commit-hash: 2f8d81f9dbac6b8df982199f69da04a4c8357227
commit-date: 2023-11-21
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 22, 2023
@compiler-errors
Copy link
Member

compiler-errors commented Nov 22, 2023

I'm almost certain this will be fixed by #117088, cc @lcnr

edit: Yes, I've confirmed this manually.

@saethlin saethlin added C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 23, 2023
@cynecx
Copy link
Contributor Author

cynecx commented Dec 12, 2023

This has been fixed since #117088 has been merged.

@cynecx cynecx closed this as completed Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. 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