Skip to content

Associated types bounds seem to be ignored in generic impls #50237

Open
@glandium

Description

@glandium

Consider the following code:

#![crate_type="lib"]

use std::marker::PhantomData;

trait Foo {
    type Type: Qux;
}

struct Bar<T>(PhantomData<T>);

trait Qux {}

impl<T: Foo> From<T::Type> for Bar<T> {
    fn from(t: T::Type) -> Self {
        Bar(PhantomData)
    }
}

This fails to build with:

error[E0119]: conflicting implementations of trait `std::convert::From<Bar<_>>` for type `Bar<_>`:
  --> src/lib.rs:13:1
   |
13 | impl<T: Foo> From<T::Type> for Bar<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;

This implies the compiler thinks T::Type might be Bar<T>, but because of the Qux bound, and the fact that there is no impl<T> Qux for Bar<T> {}, it's not possible.

In fact, doing some handholding works: impl<T: Foo<Type=U>, U: Qux> From<U> for Bar<T> yields no error and is equivalent, albeit unnecessarily verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions