Skip to content

Commit 1bcc73f

Browse files
committed
auto merge of #12023 : nick29581/rust/err_res, r=alexcrichton
closes #3512
2 parents 4509b49 + 8d8c783 commit 1bcc73f

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

src/librustc/middle/typeck/coherence.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// each trait in the system to its implementations.
1616

1717

18-
use metadata::csearch::{each_impl, get_impl_trait};
18+
use metadata::csearch::{each_impl, get_impl_trait, each_implementation_for_trait};
1919
use metadata::csearch;
2020
use middle::ty::get;
2121
use middle::ty::{ImplContainer, lookup_item_type, subst};
@@ -434,7 +434,7 @@ impl CoherenceChecker {
434434

435435
pub fn check_implementation_coherence_of(&self, trait_def_id: DefId) {
436436
// Unify pairs of polytypes.
437-
self.iter_impls_of_trait(trait_def_id, |a| {
437+
self.iter_impls_of_trait_local(trait_def_id, |a| {
438438
let implementation_a = a;
439439
let polytype_a =
440440
self.get_self_type_for_implementation(implementation_a);
@@ -452,19 +452,41 @@ impl CoherenceChecker {
452452
if self.polytypes_unify(polytype_a.clone(), polytype_b) {
453453
let session = self.crate_context.tcx.sess;
454454
session.span_err(
455-
self.span_of_impl(implementation_b),
455+
self.span_of_impl(implementation_a),
456456
format!("conflicting implementations for trait `{}`",
457457
ty::item_path_str(self.crate_context.tcx,
458458
trait_def_id)));
459-
session.span_note(self.span_of_impl(implementation_a),
460-
"note conflicting implementation here");
459+
if implementation_b.did.crate == LOCAL_CRATE {
460+
session.span_note(self.span_of_impl(implementation_b),
461+
"note conflicting implementation here");
462+
} else {
463+
let crate_store = self.crate_context.tcx.sess.cstore;
464+
let cdata = crate_store.get_crate_data(implementation_b.did.crate);
465+
session.note(
466+
"conflicting implementation in crate `" + cdata.name + "`");
467+
}
461468
}
462469
}
463470
})
464471
})
465472
}
466473

467474
pub fn iter_impls_of_trait(&self, trait_def_id: DefId, f: |@Impl|) {
475+
self.iter_impls_of_trait_local(trait_def_id, |x| f(x));
476+
477+
if trait_def_id.crate == LOCAL_CRATE {
478+
return;
479+
}
480+
481+
let crate_store = self.crate_context.tcx.sess.cstore;
482+
csearch::each_implementation_for_trait(crate_store, trait_def_id, |impl_def_id| {
483+
let implementation = @csearch::get_impl(self.crate_context.tcx, impl_def_id);
484+
let _ = lookup_item_type(self.crate_context.tcx, implementation.did);
485+
f(implementation);
486+
});
487+
}
488+
489+
pub fn iter_impls_of_trait_local(&self, trait_def_id: DefId, f: |@Impl|) {
468490
let trait_impls = self.crate_context.tcx.trait_impls.borrow();
469491
match trait_impls.get().find(&trait_def_id) {
470492
Some(impls) => {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub trait Foo {
12+
}
13+
14+
impl Foo for int {
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #3512 - conflicting trait impls in different crates should give a
12+
// 'conflicting implementations' error message.
13+
14+
// aux-build:trait_impl_conflict.rs
15+
extern mod trait_impl_conflict;
16+
use trait_impl_conflict::Foo;
17+
18+
impl<A> Foo for A {
19+
//~^ ERROR conflicting implementations for trait `trait_impl_conflict::Foo`
20+
//~^^ ERROR cannot provide an extension implementation where both trait and type are not defined in this crate
21+
}
22+
23+
fn main() {
24+
}

0 commit comments

Comments
 (0)