Skip to content

Commit 318fb9a

Browse files
committed
fixup! Add negative tests where the diagnostic message would be wrong
1 parent dffdf37 commit 318fb9a

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
pub struct Foo;
1+
pub trait Bar {}
22

3-
pub trait Bar{}
3+
pub fn try_foo(x: impl Bar) {}
44

5-
pub fn try_foo(x: impl Bar){}
5+
pub struct ImplementsTraitForUsize<T> {
6+
_marker: std::marker::PhantomData<T>,
7+
}
8+
9+
impl Bar for ImplementsTraitForUsize<usize> {}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
pub struct Foo;
22

3-
pub trait Bar{}
3+
pub trait Bar {}
44

55
impl Bar for Foo {}
6+
7+
pub struct DoesNotImplementTrait;
8+
9+
pub struct ImplementsWrongTraitConditionally<T> {
10+
_marker: std::marker::PhantomData<T>,
11+
}
12+
13+
impl Bar for ImplementsWrongTraitConditionally<isize> {}

src/test/ui/traits/trait-bounds-same-crate-name.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,50 @@
66
// is not met but the struct implements a trait with the same path.
77

88
fn main() {
9-
let foo2 = {
9+
let foo = {
1010
extern crate crate_a2 as a;
1111
a::Foo
1212
};
1313

14+
let implements_no_traits = {
15+
extern crate crate_a2 as a;
16+
a::DoesNotImplementTrait
17+
};
18+
19+
let other_variant_implements_mismatched_trait = {
20+
extern crate crate_a2 as a;
21+
a::ImplementsWrongTraitConditionally { _marker: std::marker::PhantomData::<isize> }
22+
};
23+
24+
let other_variant_implements_correct_trait = {
25+
extern crate crate_a1 as a;
26+
a::ImplementsTraitForUsize { _marker: std::marker::PhantomData::<isize> }
27+
};
28+
1429
{
1530
extern crate crate_a1 as a;
16-
a::try_foo(foo2);
31+
a::try_foo(foo);
1732
//~^ ERROR E0277
18-
//~| Trait impl with same name found
33+
//~| trait impl with same name found
1934
//~| Perhaps two different versions of crate `crate_a2`
35+
36+
// We don't want to see the "version mismatch" help message here
37+
// because `implements_no_traits` has no impl for `Foo`
38+
a::try_foo(implements_no_traits);
39+
//~^ ERROR E0277
40+
41+
// We don't want to see the "version mismatch" help message here
42+
// because `other_variant_implements_mismatched_trait`
43+
// does not have an impl for its `<isize>` variant,
44+
// only for its `<usize>` variant.
45+
a::try_foo(other_variant_implements_mismatched_trait);
46+
//~^ ERROR E0277
47+
48+
// We don't want to see the "version mismatch" help message here
49+
// because `ImplementsTraitForUsize` only has
50+
// impls for the correct trait where the path is not misleading.
51+
a::try_foo(other_variant_implements_correct_trait);
52+
//~^ ERROR E0277
53+
//~| the following implementations were found:
2054
}
2155
}
Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,64 @@
11
error[E0277]: the trait bound `main::a::Foo: main::a::Bar` is not satisfied
2-
--> $DIR/trait-bounds-same-crate-name.rs:16:20
2+
--> $DIR/trait-bounds-same-crate-name.rs:31:20
33
|
4-
LL | a::try_foo(foo2);
5-
| ^^^^ the trait `main::a::Bar` is not implemented for `main::a::Foo`
4+
LL | a::try_foo(foo);
5+
| ^^^ the trait `main::a::Bar` is not implemented for `main::a::Foo`
66
|
7-
::: $DIR/auxiliary/crate_a1.rs:5:24
7+
::: $DIR/auxiliary/crate_a1.rs:3:24
88
|
9-
LL | pub fn try_foo(x: impl Bar){}
9+
LL | pub fn try_foo(x: impl Bar) {}
1010
| --- required by this bound in `main::a::try_foo`
1111
|
12-
help: Trait impl with same name found
12+
help: trait impl with same name found
1313
--> $DIR/auxiliary/crate_a2.rs:5:1
1414
|
1515
LL | impl Bar for Foo {}
1616
| ^^^^^^^^^^^^^^^^^^^
1717
= note: Perhaps two different versions of crate `crate_a2` are being used?
1818

19-
error: aborting due to previous error
19+
error[E0277]: the trait bound `main::a::DoesNotImplementTrait: main::a::Bar` is not satisfied
20+
--> $DIR/trait-bounds-same-crate-name.rs:38:20
21+
|
22+
LL | a::try_foo(implements_no_traits);
23+
| ^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `main::a::DoesNotImplementTrait`
24+
|
25+
::: $DIR/auxiliary/crate_a1.rs:3:24
26+
|
27+
LL | pub fn try_foo(x: impl Bar) {}
28+
| --- required by this bound in `main::a::try_foo`
29+
30+
error[E0277]: the trait bound `main::a::ImplementsWrongTraitConditionally<isize>: main::a::Bar` is not satisfied
31+
--> $DIR/trait-bounds-same-crate-name.rs:45:20
32+
|
33+
LL | a::try_foo(other_variant_implements_mismatched_trait);
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `main::a::ImplementsWrongTraitConditionally<isize>`
35+
|
36+
::: $DIR/auxiliary/crate_a1.rs:3:24
37+
|
38+
LL | pub fn try_foo(x: impl Bar) {}
39+
| --- required by this bound in `main::a::try_foo`
40+
|
41+
help: trait impl with same name found
42+
--> $DIR/auxiliary/crate_a2.rs:13:1
43+
|
44+
LL | impl Bar for ImplementsWrongTraitConditionally<isize> {}
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
= note: Perhaps two different versions of crate `crate_a2` are being used?
47+
48+
error[E0277]: the trait bound `main::a::ImplementsTraitForUsize<isize>: main::a::Bar` is not satisfied
49+
--> $DIR/trait-bounds-same-crate-name.rs:51:20
50+
|
51+
LL | a::try_foo(other_variant_implements_correct_trait);
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `main::a::ImplementsTraitForUsize<isize>`
53+
|
54+
::: $DIR/auxiliary/crate_a1.rs:3:24
55+
|
56+
LL | pub fn try_foo(x: impl Bar) {}
57+
| --- required by this bound in `main::a::try_foo`
58+
|
59+
= help: the following implementations were found:
60+
<main::a::ImplementsTraitForUsize<usize> as main::a::Bar>
61+
62+
error: aborting due to 4 previous errors
2063

2164
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)