Skip to content

Better error message on duplicate trait implementation #3512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Blei opened this issue Sep 17, 2012 · 6 comments · Fixed by #12023
Closed

Better error message on duplicate trait implementation #3512

Blei opened this issue Sep 17, 2012 · 6 comments · Fixed by #12023
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system P-medium Medium priority
Milestone

Comments

@Blei
Copy link
Contributor

Blei commented Sep 17, 2012

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.

@catamorphism
Copy link
Contributor

Reproduced as of eed2ca6

@ghost ghost assigned catamorphism Feb 21, 2013
@catamorphism
Copy link
Contributor

Bumping to 0.7, though.

@catamorphism
Copy link
Contributor

Not critical for 0.7. Nominating for milestone 5, production-ready.

@graydon
Copy link
Contributor

graydon commented May 9, 2013

accepted for production-ready milestone

@emberian
Copy link
Member

emberian commented Jul 3, 2013

Still reproduces:

use std::hashmap::HashMap;
use std::to_bytes::{IterBytes, Cb};

impl IterBytes for char {
    pub fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
        (self as i32).iter_bytes(lsb0, f)
    }
}

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

It has a bunch of other non-bug errors too, but it still exhibits this bug.

@pnkfelix
Copy link
Member

Accepted for P-high.

@bors bors closed this as completed in 1bcc73f Feb 5, 2014
bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
exit integration test successfully if the crate build failed before applying rustfmt
RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 25, 2024
make miri-script a workspace root

This is needed to make miri-script build on stable (as is done by the `./miri` script) when the parent package uses unstable cargo features.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants