Skip to content

Error choosing among several implementations of a trait's static method #3903

Closed
@burg

Description

@burg

It seems that rustc isn't able to choose among multiple impls, and insists that some arbitrary one is the "right" one. (Note that type parameters are explicit to work around #3902)

Testcase

mod base {
    trait HasNew<T> {
        static pure fn new() -> T;
    }

    pub struct Foo {
        dummy: (),
    }

    pub impl Foo : HasNew<Foo> {
        static pure fn new() -> Foo {
            Foo { dummy: () }
        }
    }

    pub struct Bar {
        dummy: (),
    }

    pub impl Bar : HasNew<Bar> {
        static pure fn new() -> Bar {
            Bar { dummy: () }
        }
    }
}

fn main() {
    let f: base::Foo = base::new::<base::Foo, base::Foo>();
    debug!("%?", f);
}

Error

[burg@host-5-178 Desktop]# rustc test.rs
test.rs:28:23: 28:56 error: mismatched types: expected `@base::HasNew<base::Bar>` but found `@base::HasNew<base::Foo>` (expected class base::Bar but found class base::Foo)
test.rs:28     let f: base::Foo = base::new::<base::Foo, base::Foo>();
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.rs:28:23: 28:56 error: mismatched types: expected `base::Foo` but found `base::Bar` (expected class base::Foo but found class base::Bar)
test.rs:28     let f: base::Foo = base::new::<base::Foo, base::Foo>();
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.rs:28:23: 28:56 error: mismatched types: expected `@base::HasNew<base::Bar>` but found `@base::HasNew<base::Foo>` (expected class base::Bar but found class base::Foo)
test.rs:28     let f: base::Foo = base::new::<base::Foo, base::Foo>();
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.rs:28:23: 28:56 error: mismatched types: expected `base::Foo` but found `base::Bar` (expected class base::Foo but found class base::Bar)
test.rs:28     let f: base::Foo = base::new::<base::Foo, base::Foo>();
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions