diff options
| author | Fabian Wolff <fabi.wolff@arcor.de> | 2021-05-16 16:20:35 +0200 |
|---|---|---|
| committer | Fabian Wolff <fabi.wolff@arcor.de> | 2021-05-16 16:20:35 +0200 |
| commit | 48d07d13266d321a2be15a18d335cf642dbd685c (patch) | |
| tree | 634cb5d0f1f157fba56b46e1da55b15e819d1205 /src/test/ui | |
| parent | 94ecdfd115a353f65d4b21056b23c7074339be6e (diff) | |
| download | rust-48d07d13266d321a2be15a18d335cf642dbd685c.tar.gz rust-48d07d13266d321a2be15a18d335cf642dbd685c.zip | |
Suggest borrowing if a trait implementation is found for &/&mut <type>
Diffstat (limited to 'src/test/ui')
| -rw-r--r-- | src/test/ui/suggestions/imm-ref-trait-object-literal.stderr | 8 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-84973-2.rs | 13 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-84973-2.stderr | 15 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-84973.rs | 33 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-84973.stderr | 15 |
5 files changed, 80 insertions, 4 deletions
diff --git a/src/test/ui/suggestions/imm-ref-trait-object-literal.stderr b/src/test/ui/suggestions/imm-ref-trait-object-literal.stderr index df483b3912d..3cf74410569 100644 --- a/src/test/ui/suggestions/imm-ref-trait-object-literal.stderr +++ b/src/test/ui/suggestions/imm-ref-trait-object-literal.stderr @@ -21,10 +21,10 @@ LL | fn foo<X: Trait>(_: X) {} | ----- required by this bound in `foo` ... LL | foo(s); - | ^ the trait `Trait` is not implemented for `S` - | - = help: the following implementations were found: - <&'a mut S as Trait> + | ^ + | | + | expected an implementor of trait `Trait` + | help: consider borrowing mutably here: `&mut s` error: aborting due to 2 previous errors diff --git a/src/test/ui/suggestions/issue-84973-2.rs b/src/test/ui/suggestions/issue-84973-2.rs new file mode 100644 index 00000000000..050cf8c64b3 --- /dev/null +++ b/src/test/ui/suggestions/issue-84973-2.rs @@ -0,0 +1,13 @@ +// A slight variation of issue-84973.rs. Here, a mutable borrow is +// required (and the obligation kind is different). + +trait Tr {} +impl Tr for &mut i32 {} + +fn foo<T: Tr>(i: T) {} + +fn main() { + let a: i32 = 32; + foo(a); + //~^ ERROR: the trait bound `i32: Tr` is not satisfied [E0277] +} diff --git a/src/test/ui/suggestions/issue-84973-2.stderr b/src/test/ui/suggestions/issue-84973-2.stderr new file mode 100644 index 00000000000..6394e58a2a9 --- /dev/null +++ b/src/test/ui/suggestions/issue-84973-2.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `i32: Tr` is not satisfied + --> $DIR/issue-84973-2.rs:11:9 + | +LL | fn foo<T: Tr>(i: T) {} + | -- required by this bound in `foo` +... +LL | foo(a); + | ^ + | | + | expected an implementor of trait `Tr` + | help: consider borrowing mutably here: `&mut a` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/suggestions/issue-84973.rs b/src/test/ui/suggestions/issue-84973.rs new file mode 100644 index 00000000000..42468478ed9 --- /dev/null +++ b/src/test/ui/suggestions/issue-84973.rs @@ -0,0 +1,33 @@ +// Checks whether borrowing is suggested when a trait bound is not satisfied +// for found type `T`, but is for `&/&mut T`. + +fn main() { + let f = Fancy{}; + let o = Other::new(f); + //~^ ERROR: the trait bound `Fancy: SomeTrait` is not satisfied [E0277] +} + +struct Fancy {} + +impl <'a> SomeTrait for &'a Fancy { +} + +trait SomeTrait {} + +struct Other<'a, G> { + a: &'a str, + g: G, +} + +// Broadly copied from https://docs.rs/petgraph/0.5.1/src/petgraph/dot.rs.html#70 +impl<'a, G> Other<'a, G> +where + G: SomeTrait, +{ + pub fn new(g: G) -> Self { + Other { + a: "hi", + g: g, + } + } +} diff --git a/src/test/ui/suggestions/issue-84973.stderr b/src/test/ui/suggestions/issue-84973.stderr new file mode 100644 index 00000000000..49fa94da859 --- /dev/null +++ b/src/test/ui/suggestions/issue-84973.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `Fancy: SomeTrait` is not satisfied + --> $DIR/issue-84973.rs:6:24 + | +LL | let o = Other::new(f); + | ^ + | | + | expected an implementor of trait `SomeTrait` + | help: consider borrowing here: `&f` +... +LL | pub fn new(g: G) -> Self { + | ------------------------ required by `Other::<'a, G>::new` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. |
