Skip to content

Commit e1ae7b7

Browse files
committed
tests: Check error when struct from wrong crate version is used for impl
1 parent 9f2ef0f commit e1ae7b7

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Bar;
2+
3+
impl From<Bar> for foo::Foo {
4+
fn from(_: Bar) -> Self {
5+
foo::Foo
6+
}
7+
}
8+
9+
fn main() {
10+
// The user might wrongly expect this to work since From<Bar> for Foo
11+
// implies Into<Foo> for Bar. What the user missed is that different
12+
// versions of Foo exist in the dependency graph, and the impl is for the
13+
// wrong version.
14+
re_export_foo::into_foo(Bar);
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: the trait bound `re_export_foo::foo::Foo: From<Bar>` is not satisfied
2+
--> main.rs:14:29
3+
|
4+
LL | re_export_foo::into_foo(Bar);
5+
| ----------------------- ^^^ the trait `From<Bar>` is not implemented for `re_export_foo::foo::Foo`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= note: required for `Bar` to implement `Into<re_export_foo::foo::Foo>`
10+
note: required by a bound in `into_foo`
11+
--> $DIR/re-export-foo.rs:3:25
12+
|
13+
LL | pub fn into_foo(_: impl Into<foo::Foo>) {}
14+
| ^^^^^^^^^^^^^^ required by this bound in `into_foo`
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use foo;
2+
3+
pub fn into_foo(_: impl Into<foo::Foo>) {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use run_make_support::{Rustc, cwd, diff, rust_lib_name, rustc};
2+
3+
fn rustc_with_common_args() -> Rustc {
4+
let mut rustc = rustc();
5+
rustc.remap_path_prefix(cwd(), "$DIR");
6+
rustc.edition("2018"); // Don't require `extern crate`
7+
rustc
8+
}
9+
10+
fn main() {
11+
rustc_with_common_args()
12+
.input("foo-v1.rs")
13+
.crate_type("rlib")
14+
.crate_name("foo")
15+
.extra_filename("-v1")
16+
.metadata("-v1")
17+
.run();
18+
19+
rustc_with_common_args()
20+
.input("foo-v2.rs")
21+
.crate_type("rlib")
22+
.crate_name("foo")
23+
.extra_filename("-v2")
24+
.metadata("-v2")
25+
.run();
26+
27+
rustc_with_common_args()
28+
.input("re-export-foo.rs")
29+
.crate_type("rlib")
30+
.extern_("foo", rust_lib_name("foo-v2"))
31+
.run();
32+
33+
let stderr = rustc_with_common_args()
34+
.input("main.rs")
35+
.extern_("foo", rust_lib_name("foo-v1"))
36+
.extern_("re_export_foo", rust_lib_name("re_export_foo"))
37+
.library_search_path(cwd())
38+
.ui_testing()
39+
.run_fail()
40+
.stderr_utf8();
41+
42+
diff().expected_file("main.stderr").actual_text("(rustc)", &stderr).run();
43+
}

0 commit comments

Comments
 (0)