Inherent impl blocks on trait objects don't check for coherence or overlapping impls until called #38967

Closed
QuietMisdreavus opened this Issue Jan 10, 2017 · 6 comments

Projects

None yet

6 participants

@QuietMisdreavus
Contributor

Consider the following snippet:

trait A {}

trait B {}

trait C<T> {}

impl <T: A> C<T> { fn asdf(&self) { println!("asdf"); } }

impl <T: B> C<T> { fn asdf(&self) { println!("asdf!!"); } }

struct Z {}
impl A for Z {}
impl B for Z {}

struct W {}
impl C<Z> for W {}

fn main() {
    let asdf = Box::new(W {}) as Box<C<Z>>;
    
    asdf.asdf();
}

When asdf.asdf() is called, which impl should be used? Is it possible to select one impl or the other?

When discussing this in #rust, @mbrubeck also found that impls on trait objects don't check for overlap at all:

trait C<T> {}

impl<T> C<T> { fn asdf(&self) {} }
impl<T> C<T> { fn asdf(&self) {} }

struct W {}
impl<T> C<T> for W {}

fn main() {
    let b = Box::new(W {}) as Box<C<()>>;
    b.asdf();
}

In both of these examples, taking out the call to asdf() makes the sample compile, meaning that it's only upon method resolution that this conflict is found. The former can be ruled out as a coherence issue, and the latter is directly an overlapping impl, that wouldn't be allowed on a regular struct/enum.

cc #36889, per talchas in #rust

@mbrubeck
Contributor

Here's a minimal test case. The following should trigger the overlapping_inherent_impls lint but does not:

trait C {}
impl C { fn f() {} }
impl C { fn f() {} }
@apasel422
Member

CC #22889

@nikomatsakis
Contributor

Hmm, seems like a bug, for sure. For example, this code https://is.gd/YSKjuW using structs does error.

cc @aturon, who wrote the lint I think.

@nikomatsakis
Contributor

triage: P-medium

@rust-highfive rust-highfive added P-medium and removed I-nominated labels Jan 12, 2017
@nikomatsakis
Contributor

The source of the bug looks pretty simple. I'll open a PR.

@nikomatsakis
Contributor

Pending fix in #39143

@alexcrichton alexcrichton added a commit to alexcrichton/rust that referenced this issue Jan 19, 2017
@alexcrichton alexcrichton Rollup merge of #39143 - nikomatsakis:issue-38967, r=eddyb
check inherent impls of traits for overlap as well

Simple oversight. Fixes #38967.

r? @eddyb
7ac00bd
@alexcrichton alexcrichton added a commit to alexcrichton/rust that referenced this issue Jan 20, 2017
@alexcrichton alexcrichton Rollup merge of #39143 - nikomatsakis:issue-38967, r=eddyb
check inherent impls of traits for overlap as well

Simple oversight. Fixes #38967.

r? @eddyb
c6e4faa
@bors bors closed this in #39143 Jan 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment