Skip to content

Better error message on duplicate trait implementation #3512

Closed
@Blei

Description

@Blei

Some days ago, I wrote some code involving char as HashMap key. At that time, char didn't implement the IterBytes trait, so I implemented it locally myself. After updating rust, my code failed to compile with a somewhat less than obvious error.

Example:

extern mod std;
use std::map::HashMap;
use to_bytes::{IterBytes, Cb};

impl char: IterBytes {
    pure fn iter_bytes(lsb0: bool, f: Cb) {
        (self as i32).iter_bytes(lsb0, f);
    }
}

fn main() {
    let h = HashMap();
    h.insert('a', ());
    io::println(h.contains_key('a').to_str());
}

Produces the following errors:

minimal.rs:12:12: 12:19 error: multiple applicable methods in scope
minimal.rs:12     let h = HashMap();
                          ^~~~~~~
minimal.rs:12:12: 12:19 error: multiple applicable methods in scope
minimal.rs:12     let h = HashMap();
                          ^~~~~~~
minimal.rs:13:4: 13:12 error: multiple applicable methods in scope
minimal.rs:13     h.insert('a', ());
                  ^~~~~~~~
minimal.rs:13:4: 13:12 error: multiple applicable methods in scope
minimal.rs:13     h.insert('a', ());
                  ^~~~~~~~
minimal.rs:14:16: 14:30 error: multiple applicable methods in scope
minimal.rs:14     io::println(h.contains_key('a').to_str());
                              ^~~~~~~~~~~~~~
minimal.rs:14:16: 14:30 error: multiple applicable methods in scope
minimal.rs:14     io::println(h.contains_key('a').to_str());
                              ^~~~~~~~~~~~~~
error: aborting due to 6 previous errors

Note that there is no way to know that the problem is the duplicate implementation of IterBytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions