Skip to content

Check for trait impl conflicts across crates #12023

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

Merged
merged 1 commit into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/librustc/middle/typeck/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// each trait in the system to its implementations.


use metadata::csearch::{each_impl, get_impl_trait};
use metadata::csearch::{each_impl, get_impl_trait, each_implementation_for_trait};
use metadata::csearch;
use middle::ty::get;
use middle::ty::{ImplContainer, lookup_item_type, subst};
Expand Down Expand Up @@ -434,7 +434,7 @@ impl CoherenceChecker {

pub fn check_implementation_coherence_of(&self, trait_def_id: DefId) {
// Unify pairs of polytypes.
self.iter_impls_of_trait(trait_def_id, |a| {
self.iter_impls_of_trait_local(trait_def_id, |a| {
let implementation_a = a;
let polytype_a =
self.get_self_type_for_implementation(implementation_a);
Expand All @@ -452,19 +452,41 @@ impl CoherenceChecker {
if self.polytypes_unify(polytype_a.clone(), polytype_b) {
let session = self.crate_context.tcx.sess;
session.span_err(
self.span_of_impl(implementation_b),
self.span_of_impl(implementation_a),
format!("conflicting implementations for trait `{}`",
ty::item_path_str(self.crate_context.tcx,
trait_def_id)));
session.span_note(self.span_of_impl(implementation_a),
"note conflicting implementation here");
if implementation_b.did.crate == LOCAL_CRATE {
session.span_note(self.span_of_impl(implementation_b),
"note conflicting implementation here");
} else {
let crate_store = self.crate_context.tcx.sess.cstore;
let cdata = crate_store.get_crate_data(implementation_b.did.crate);
session.note(
"conflicting implementation in crate `" + cdata.name + "`");
}
}
}
})
})
}

pub fn iter_impls_of_trait(&self, trait_def_id: DefId, f: |@Impl|) {
self.iter_impls_of_trait_local(trait_def_id, |x| f(x));

if trait_def_id.crate == LOCAL_CRATE {
return;
}

let crate_store = self.crate_context.tcx.sess.cstore;
csearch::each_implementation_for_trait(crate_store, trait_def_id, |impl_def_id| {
let implementation = @csearch::get_impl(self.crate_context.tcx, impl_def_id);
let _ = lookup_item_type(self.crate_context.tcx, implementation.did);
f(implementation);
});
}

pub fn iter_impls_of_trait_local(&self, trait_def_id: DefId, f: |@Impl|) {
let trait_impls = self.crate_context.tcx.trait_impls.borrow();
match trait_impls.get().find(&trait_def_id) {
Some(impls) => {
Expand Down
15 changes: 15 additions & 0 deletions src/test/auxiliary/trait_impl_conflict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub trait Foo {
}

impl Foo for int {
}
24 changes: 24 additions & 0 deletions src/test/compile-fail/conflicting-implementations-aux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Regression test for #3512 - conflicting trait impls in different crates should give a
// 'conflicting implementations' error message.

// aux-build:trait_impl_conflict.rs
extern mod trait_impl_conflict;
use trait_impl_conflict::Foo;

impl<A> Foo for A {
//~^ ERROR conflicting implementations for trait `trait_impl_conflict::Foo`
//~^^ ERROR cannot provide an extension implementation where both trait and type are not defined in this crate
}

fn main() {
}