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

Inference failure when a trait is changed to a marker trait #61651

Closed
scottmcm opened this issue Jun 8, 2019 · 1 comment · Fixed by #68057
Closed

Inference failure when a trait is changed to a marker trait #61651

scottmcm opened this issue Jun 8, 2019 · 1 comment · Fixed by #68057
Labels
A-inference Area: Type inference C-bug Category: This is a bug. F-marker_trait_attr `#![feature(marker_trait_attr)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Jun 8, 2019

#![feature(marker_trait_attr)]

#[marker] // Remove this line and it works?!?
trait Foo<T> {}
impl Foo<u16> for u8 {}
impl Foo<[u8; 1]> for u8 {}
fn foo<T: Foo<U>, U>(_: T) -> U { unimplemented!() }

fn main() {
    let _: u16 = foo(0_u8);
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=120bf7cdd44e52d7a6d2280cdcdc6de6

It works if I remove the #[marker], but when it's a #[marker] trait it fails with

error[E0308]: mismatched types
  --> src/lib.rs:10:18
   |
10 |     let _: u16 = foo(0_u8);
   |                  ^^^^^^^^^ expected u16, found array of 1 elements
   |
   = note: expected type `u16`
              found type `[u8; 1]`

(I didn't think this property of a trait affected inference at all, so I'm very confused by this...)

@scottmcm scottmcm added B-unstable Blocker: Implemented in the nightly compiler and unstable. A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 8, 2019
@jonas-schievink jonas-schievink removed the B-unstable Blocker: Implemented in the nightly compiler and unstable. label Jun 22, 2019
@Aaron1011
Copy link
Member

I ran into this with a slightly different example:

#![feature(marker_trait_attr)]

struct Wrapper<T>(T);

#[marker]
pub trait MyTrait {}

impl MyTrait for Wrapper<u32> {}
impl MyTrait for Wrapper<bool> {}

fn weird_call<T>(val: T) -> Wrapper<T> where Wrapper<T>: MyTrait {
    Wrapper(val)
}

fn main() {
    let a: Wrapper<u32> = weird_call(25);
}

with the following error:

error[E0308]: mismatched types
  --> src/main.rs:16:38
   |
16 |     let a: Wrapper<u32> = weird_call(25);
   |                                      ^^ expected `bool`, found integer

error[E0308]: mismatched types
  --> src/main.rs:16:27
   |
16 |     let a: Wrapper<u32> = weird_call(25);
   |            ------------   ^^^^^^^^^^^^^^ expected `u32`, found `bool`
   |            |
   |            expected due to this
   |
   = note: expected struct `Wrapper<u32>`
              found struct `Wrapper<bool>`

error: aborting due to 2 previous errors

Note that this example doesn't even rely on MyTrait being a marker trait - you can comment out #[marker], and it will compile.

@Centril Centril added the F-marker_trait_attr `#![feature(marker_trait_attr)]` label Jan 9, 2020
bors added a commit that referenced this issue Jan 20, 2020
…wjasper

Don't discard marker trait impls when inference variables are present

Fixes #61651

Previously, we would unconditionally discard impl candidates for marker
traits during trait selection. However, if the predicate had inference
variables, this could have the effect of constrainting inference
variables (due to a successful trait selection) when we would have
otherwise failed due to mutliple applicable impls,

This commit prevents marker trait impls from being discarded while the
obligation predicate has any inference variables, ensuring that
discarding impls will never cause us to incorrectly constraint inference
variables.
@bors bors closed this as completed in 4840cd8 Jan 20, 2020
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-marker_trait_attr `#![feature(marker_trait_attr)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants