about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYorwba <yorwb4@gmail.com>2017-06-22 17:07:38 +0800
committerYorwba <yorwb4@gmail.com>2017-06-22 22:01:22 +0800
commit8205c348b4a793878a1cda06ebbc93860e9fea0c (patch)
treeee9b9806942cefba048df876795dbb3ebd3af399
parent6f01c84fc8ae1b07d8165ceccb2e432f45a2ff1a (diff)
downloadrust-8205c348b4a793878a1cda06ebbc93860e9fea0c.tar.gz
rust-8205c348b4a793878a1cda06ebbc93860e9fea0c.zip
Note different versions of same crate when absolute paths of different types match.
-rw-r--r--src/librustc/infer/error_reporting/mod.rs5
-rw-r--r--src/test/run-make/type-mismatch-same-crate-name/Makefile19
-rw-r--r--src/test/run-make/type-mismatch-same-crate-name/crateA.rs26
-rw-r--r--src/test/run-make/type-mismatch-same-crate-name/crateB.rs14
-rw-r--r--src/test/run-make/type-mismatch-same-crate-name/crateC.rs35
5 files changed, 98 insertions, 1 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs
index 11bac21bc42..2265c0c0a8c 100644
--- a/src/librustc/infer/error_reporting/mod.rs
+++ b/src/librustc/infer/error_reporting/mod.rs
@@ -337,9 +337,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
             if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
                 let exp_path = self.tcx.item_path_str(did1);
                 let found_path = self.tcx.item_path_str(did2);
+                let exp_abs_path = self.tcx.absolute_item_path_str(did1);
+                let found_abs_path = self.tcx.absolute_item_path_str(did2);
                 // We compare strings because DefPath can be different
                 // for imported and non-imported crates
-                if exp_path == found_path {
+                if exp_path == found_path
+                || exp_abs_path == found_abs_path {
                     let crate_name = self.tcx.sess.cstore.crate_name(did1.krate);
                     err.span_note(sp, &format!("Perhaps two different versions \
                                                 of crate `{}` are being used?",
diff --git a/src/test/run-make/type-mismatch-same-crate-name/Makefile b/src/test/run-make/type-mismatch-same-crate-name/Makefile
new file mode 100644
index 00000000000..24ef203278a
--- /dev/null
+++ b/src/test/run-make/type-mismatch-same-crate-name/Makefile
@@ -0,0 +1,19 @@
+-include ../tools.mk
+
+all:
+	# compile two different versions of crateA
+	$(RUSTC) --crate-type=rlib crateA.rs -C metadata=-1 -C extra-filename=-1
+	$(RUSTC) --crate-type=rlib crateA.rs -C metadata=-2 -C extra-filename=-2
+	# make crateB depend on version 1 of crateA
+	$(RUSTC) --crate-type=rlib crateB.rs --extern crateA=$(TMPDIR)/libcrateA-1.rlib
+	# make crateC depend on version 2 of crateA
+	$(RUSTC) crateC.rs --extern crateA=$(TMPDIR)/libcrateA-2.rlib 2>&1 | \
+		grep -z \
+	"mismatched types.*\
+	crateB::try_foo(foo2);.*\
+	expected struct \`crateA::foo::Foo\`, found struct \`crateA::Foo\`.*\
+	different versions of crate \`crateA\`.*\
+	mismatched types.*\
+	crateB::try_bar(bar2);.*\
+	expected trait \`crateA::bar::Bar\`, found trait \`crateA::Bar\`.*\
+	different versions of crate \`crateA\`"
diff --git a/src/test/run-make/type-mismatch-same-crate-name/crateA.rs b/src/test/run-make/type-mismatch-same-crate-name/crateA.rs
new file mode 100644
index 00000000000..e40266bb4cd
--- /dev/null
+++ b/src/test/run-make/type-mismatch-same-crate-name/crateA.rs
@@ -0,0 +1,26 @@
+// Copyright 2017 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.
+
+mod foo {
+    pub struct Foo;
+}
+
+mod bar {
+    pub trait Bar{}
+
+    pub fn bar() -> Box<Bar> {
+        unimplemented!()
+    }
+}
+
+// This makes the publicly accessible path
+// differ from the internal one.
+pub use foo::Foo;
+pub use bar::{Bar, bar};
diff --git a/src/test/run-make/type-mismatch-same-crate-name/crateB.rs b/src/test/run-make/type-mismatch-same-crate-name/crateB.rs
new file mode 100644
index 00000000000..da4ea1c9387
--- /dev/null
+++ b/src/test/run-make/type-mismatch-same-crate-name/crateB.rs
@@ -0,0 +1,14 @@
+// Copyright 2017 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.
+
+extern crate crateA;
+
+pub fn try_foo(x: crateA::Foo){}
+pub fn try_bar(x: Box<crateA::Bar>){}
diff --git a/src/test/run-make/type-mismatch-same-crate-name/crateC.rs b/src/test/run-make/type-mismatch-same-crate-name/crateC.rs
new file mode 100644
index 00000000000..da869d2145f
--- /dev/null
+++ b/src/test/run-make/type-mismatch-same-crate-name/crateC.rs
@@ -0,0 +1,35 @@
+// Copyright 2017 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.
+
+// This tests the extra note reported when a type error deals with
+// seemingly identical types.
+// The main use case of this error is when there are two crates
+// (generally different versions of the same crate) with the same name
+// causing a type mismatch.
+
+// The test is nearly the same as the one in
+// compile-fail/type-mismatch-same-crate-name.rs
+// but deals with the case where one of the crates
+// is only introduced as an indirect dependency.
+// and the type is accessed via a reexport.
+// This is similar to how the error can be introduced
+// when using cargo's automatic dependency resolution.
+
+extern crate crateA;
+
+fn main() {
+    let foo2 = crateA::Foo;
+    let bar2 = crateA::bar();
+    {
+        extern crate crateB;
+        crateB::try_foo(foo2);
+        crateB::try_bar(bar2);
+    }
+}