Skip to content

rustc: where clauses are sometimes silently ignored #18906

Closed
@aturon

Description

@aturon

I'm reasonably sure the problem below is related to multidispatch and its interaction with type inference. It works fine for a standalone function, but not in an impl context:

pub trait Borrow<Sized? Borrowed> {
    fn borrow(&self) -> &Borrowed;
}

impl<T: Sized> Borrow<T> for T {
    fn borrow(&self) -> &T { self }
}

trait Foo {
    fn foo(&self, other: &Self);
}

// This works fine
fn bar<K, Q>(k: &K, q: &Q) where K: Borrow<Q>, Q: Foo {
    q.foo(k.borrow())
}

struct MyTree<K>;
impl<K> MyTree<K> {
    // This fails
    fn bar<Q>(k: &K, q: &Q) where K: Borrow<Q>, Q: Foo {
        q.foo(k.borrow())
    }
}

The error is:

borrow-test.rs:22:15: 22:25 error: mismatched types: expected `&Q`, found `&K` (expected type parameter, found a different type parameter)
borrow-test.rs:22         q.foo(k.borrow())
                                ^~~~~~~~~~

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions