Skip to content

Specialization influnces inference #36262

Open
@arielb1

Description

@arielb1

STR

#![feature(specialization)]

struct My<T>(T);

trait Conv<T> {
    fn conv(self) -> T;
}

impl<T> Conv<T> for My<T> {
    default fn conv(self) -> T { self.0 }
}

#[cfg(broken)]
impl Conv<u32> for My<u32> {
    default fn conv(self) -> u32 { self.0 }
}

fn main() {
    let x = My(0);
    x.conv() + 0i32;
}

Expected Result

Adding a specialized impl will not affect type inference.

Actual Result

After adding the specialized impl, inference is guided to take it:

error[E0308]: mismatched types
  --> <anon>:20:16
   |
20 |     x.conv() + 0i32;
   |                ^^^^ expected u32, found i32

error[E0277]: the trait bound `u32: std::ops::Add<i32>` is not satisfied
  --> <anon>:20:5
   |
20 |     x.conv() + 0i32;
   |     ^^^^^^^^^^^^^^^ trait `u32: std::ops::Add<i32>` not satisfied
   |
   = help: the following implementations were found:
   = help:   <u32 as std::ops::Add>
   = help:   <&'a u32 as std::ops::Add<u32>>
   = help:   <u32 as std::ops::Add<&'a u32>>
   = help:   <&'b u32 as std::ops::Add<&'a u32>>

error: aborting due to 2 previous errors

cc @aturon @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-specializationArea: Trait impl specializationA-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-specialization`#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions