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

Adding a specialized impl can break inference. #46363

Open
leoyvens opened this issue Nov 29, 2017 · 2 comments
Open

Adding a specialized impl can break inference. #46363

leoyvens opened this issue Nov 29, 2017 · 2 comments
Labels
A-inference Area: Type inference C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leoyvens
Copy link
Contributor

leoyvens commented Nov 29, 2017

Relevant to #31844. This is how it happens:

#![feature(specialization)]

trait B { fn b(&self) -> Self; }

// Impl 1
impl<T> B for Option<T> where T: Default
{
    default fn b(&self) -> Option<T> { Some(T::default()) }
}
// Removing one of the two concrete impls makes inference succeed.
// Impl 2
impl B for Option<String>
{
    fn b(&self) -> Self { Some("special".into()) }
}
// Impl 3
impl B for Option<i32>
{
    fn b(&self) -> Self { Some(0) }
}

fn main() { 
    // Cannot infer `T` in `Option<T>`.
   None.b();
}

This issue does not originate from specialization, since if we removed Impl 1 the same problem would occur. But with specialization if those impls were added in order, the story would be a bit confusing:

  1. With Impl 1, inference fails.
  2. Added Impl 2, yay inference succeeds.
  3. Added Impl 3, inference is back to failing.

The only fix would be to make inference fail in step 2. Even if it's not something we want fix it still seems worth noting somewhere that we are ok with specializaton breaking inference.

@pietroalbini pietroalbini added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference C-bug Category: This is a bug. labels Jan 23, 2018
@joshlf
Copy link
Contributor

joshlf commented Jun 10, 2018

Is this the same issue that's causing this error? #51464 (comment)

There used to be a bunch of specific impls for SliceIndex for various types. That PR adds a default impl (so the old specific impls are now specializations of the default impl). The error in question is a test case that used to pass that now fails inference.

@jhpratt
Copy link
Member

jhpratt commented Apr 1, 2021

I'm running into this when playing around with stdlib. For a type that accepts either i32 or any other integer type, inference fails because the compiler sees that i32 is a valid option and automatically chooses that without actually trying to solve the inference.

@matthewjasper matthewjasper added the F-specialization `#![feature(specialization)]` label Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants