Skip to content

Commit 620f480

Browse files
committed
better suggestions
1 parent e2aad3f commit 620f480

10 files changed

+70
-53
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -781,19 +781,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
781781

782782
if imm_result && mut_result {
783783
err.span_suggestions(
784-
span,
784+
span.shrink_to_lo(),
785785
"consider borrowing here",
786-
[format!("&{}", snippet), format!("&mut {}", snippet)].into_iter(),
786+
["&".to_string(), "&mut ".to_string()].into_iter(),
787787
Applicability::MaybeIncorrect,
788788
);
789789
} else {
790-
err.span_suggestion(
791-
span,
790+
err.span_suggestion_verbose(
791+
span.shrink_to_lo(),
792792
&format!(
793793
"consider{} borrowing here",
794794
if mut_result { " mutably" } else { "" }
795795
),
796-
format!("&{}{}", if mut_result { "mut " } else { "" }, snippet),
796+
format!("&{}", if mut_result { "mut " } else { "" }),
797797
Applicability::MaybeIncorrect,
798798
);
799799
}

src/test/ui/derives/deriving-copyclone.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0277]: the trait bound `C: Copy` is not satisfied
22
--> $DIR/deriving-copyclone.rs:31:13
33
|
44
LL | is_copy(B { a: 1, b: C });
5-
| ------- ^^^^^^^^^^^^^^^^
6-
| | |
7-
| | expected an implementor of trait `Copy`
8-
| | help: consider borrowing here: `&B { a: 1, b: C }`
5+
| ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
6+
| |
97
| required by a bound introduced by this call
108
|
119
note: required because of the requirements on the impl of `Copy` for `B<C>`
@@ -19,15 +17,17 @@ note: required by a bound in `is_copy`
1917
LL | fn is_copy<T: Copy>(_: T) {}
2018
| ^^^^ required by this bound in `is_copy`
2119
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
20+
help: consider borrowing here
21+
|
22+
LL | is_copy(&B { a: 1, b: C });
23+
| +
2224

2325
error[E0277]: the trait bound `C: Clone` is not satisfied
2426
--> $DIR/deriving-copyclone.rs:32:14
2527
|
2628
LL | is_clone(B { a: 1, b: C });
27-
| -------- ^^^^^^^^^^^^^^^^
28-
| | |
29-
| | expected an implementor of trait `Clone`
30-
| | help: consider borrowing here: `&B { a: 1, b: C }`
29+
| -------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Clone`
30+
| |
3131
| required by a bound introduced by this call
3232
|
3333
note: required because of the requirements on the impl of `Clone` for `B<C>`
@@ -41,15 +41,17 @@ note: required by a bound in `is_clone`
4141
LL | fn is_clone<T: Clone>(_: T) {}
4242
| ^^^^^ required by this bound in `is_clone`
4343
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
help: consider borrowing here
45+
|
46+
LL | is_clone(&B { a: 1, b: C });
47+
| +
4448

4549
error[E0277]: the trait bound `D: Copy` is not satisfied
4650
--> $DIR/deriving-copyclone.rs:35:13
4751
|
4852
LL | is_copy(B { a: 1, b: D });
49-
| ------- ^^^^^^^^^^^^^^^^
50-
| | |
51-
| | expected an implementor of trait `Copy`
52-
| | help: consider borrowing here: `&B { a: 1, b: D }`
53+
| ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
54+
| |
5355
| required by a bound introduced by this call
5456
|
5557
note: required because of the requirements on the impl of `Copy` for `B<D>`
@@ -63,6 +65,10 @@ note: required by a bound in `is_copy`
6365
LL | fn is_copy<T: Copy>(_: T) {}
6466
| ^^^^ required by this bound in `is_copy`
6567
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
68+
help: consider borrowing here
69+
|
70+
LL | is_copy(&B { a: 1, b: D });
71+
| +
6672

6773
error: aborting due to 3 previous errors
6874

src/test/ui/issues/issue-20605.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0277]: the size for values of type `dyn Iterator<Item = &'a mut u8>` cann
22
--> $DIR/issue-20605.rs:2:17
33
|
44
LL | for item in *things { *item = 0 }
5-
| ^^^^^^^
6-
| |
7-
| expected an implementor of trait `IntoIterator`
8-
| help: consider mutably borrowing here: `&mut *things`
5+
| ^^^^^^^ expected an implementor of trait `IntoIterator`
96
|
107
= note: the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is not satisfied
118
= note: required because of the requirements on the impl of `IntoIterator` for `dyn Iterator<Item = &'a mut u8>`
@@ -14,6 +11,10 @@ note: required by `into_iter`
1411
|
1512
LL | fn into_iter(self) -> Self::IntoIter;
1613
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: consider mutably borrowing here
15+
|
16+
LL | for item in &mut *things { *item = 0 }
17+
| ++++
1718

1819
error: aborting due to previous error
1920

src/test/ui/suggestions/imm-ref-trait-object-literal.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ error[E0277]: the trait bound `S: Trait` is not satisfied
2222
--> $DIR/imm-ref-trait-object-literal.rs:13:7
2323
|
2424
LL | foo(s);
25-
| --- ^
26-
| | |
27-
| | expected an implementor of trait `Trait`
28-
| | help: consider mutably borrowing here: `&mut s`
25+
| --- ^ expected an implementor of trait `Trait`
26+
| |
2927
| required by a bound introduced by this call
3028
|
3129
note: required by a bound in `foo`
3230
--> $DIR/imm-ref-trait-object-literal.rs:7:11
3331
|
3432
LL | fn foo<X: Trait>(_: X) {}
3533
| ^^^^^ required by this bound in `foo`
34+
help: consider mutably borrowing here
35+
|
36+
LL | foo(&mut s);
37+
| ++++
3638

3739
error: aborting due to 2 previous errors
3840

src/test/ui/suggestions/issue-84973-2.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0277]: the trait bound `i32: Tr` is not satisfied
22
--> $DIR/issue-84973-2.rs:11:9
33
|
44
LL | foo(a);
5-
| --- ^
6-
| | |
7-
| | expected an implementor of trait `Tr`
8-
| | help: consider mutably borrowing here: `&mut a`
5+
| --- ^ expected an implementor of trait `Tr`
6+
| |
97
| required by a bound introduced by this call
108
|
119
note: required by a bound in `foo`
1210
--> $DIR/issue-84973-2.rs:7:11
1311
|
1412
LL | fn foo<T: Tr>(i: T) {}
1513
| ^^ required by this bound in `foo`
14+
help: consider mutably borrowing here
15+
|
16+
LL | foo(&mut a);
17+
| ++++
1618

1719
error: aborting due to previous error
1820

src/test/ui/suggestions/issue-84973-negative.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ error[E0277]: the trait bound `f32: Tr` is not satisfied
1616
--> $DIR/issue-84973-negative.rs:11:9
1717
|
1818
LL | bar(b);
19-
| --- ^
20-
| | |
21-
| | expected an implementor of trait `Tr`
22-
| | help: consider borrowing here: `&b`
19+
| --- ^ expected an implementor of trait `Tr`
20+
| |
2321
| required by a bound introduced by this call
2422
|
2523
note: required by a bound in `bar`
2624
--> $DIR/issue-84973-negative.rs:5:11
2725
|
2826
LL | fn bar<T: Tr>(t: T) {}
2927
| ^^ required by this bound in `bar`
28+
help: consider borrowing here
29+
|
30+
LL | bar(&b);
31+
| +
3032

3133
error: aborting due to 2 previous errors
3234

src/test/ui/suggestions/issue-84973.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0277]: the trait bound `Fancy: SomeTrait` is not satisfied
22
--> $DIR/issue-84973.rs:6:24
33
|
44
LL | let o = Other::new(f);
5-
| ---------- ^
6-
| | |
7-
| | expected an implementor of trait `SomeTrait`
8-
| | help: consider borrowing here: `&f`
5+
| ---------- ^ expected an implementor of trait `SomeTrait`
6+
| |
97
| required by a bound introduced by this call
108
|
119
note: required by `Other::<'a, G>::new`
1210
--> $DIR/issue-84973.rs:27:5
1311
|
1412
LL | pub fn new(g: G) -> Self {
1513
| ^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: consider borrowing here
15+
|
16+
LL | let o = Other::new(&f);
17+
| +
1618

1719
error: aborting due to previous error
1820

src/test/ui/suggestions/slice-issue-87994.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
1414
help: consider borrowing here
1515
|
1616
LL | for _ in &v[1..] {
17-
| ~~~~~~~
17+
| +
1818
LL | for _ in &mut v[1..] {
19-
| ~~~~~~~~~~~
19+
| ++++
2020

2121
error[E0277]: `[i32]` is not an iterator
2222
--> $DIR/slice-issue-87994.rs:3:12
@@ -34,9 +34,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
3434
help: consider borrowing here
3535
|
3636
LL | for _ in &v[1..] {
37-
| ~~~~~~~
37+
| +
3838
LL | for _ in &mut v[1..] {
39-
| ~~~~~~~~~~~
39+
| ++++
4040

4141
error[E0277]: the size for values of type `[K]` cannot be known at compilation time
4242
--> $DIR/slice-issue-87994.rs:11:13
@@ -54,9 +54,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
5454
help: consider borrowing here
5555
|
5656
LL | for i2 in &v2[1..] {
57-
| ~~~~~~~~
57+
| +
5858
LL | for i2 in &mut v2[1..] {
59-
| ~~~~~~~~~~~~
59+
| ++++
6060

6161
error[E0277]: `[K]` is not an iterator
6262
--> $DIR/slice-issue-87994.rs:11:13
@@ -74,9 +74,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
7474
help: consider borrowing here
7575
|
7676
LL | for i2 in &v2[1..] {
77-
| ~~~~~~~~
77+
| +
7878
LL | for i2 in &mut v2[1..] {
79-
| ~~~~~~~~~~~~
79+
| ++++
8080

8181
error: aborting due to 4 previous errors
8282

src/test/ui/suggestions/suggest-both-imm-and-mut-trait-implementation.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ LL | fn foo<X: Trait>(_: X) {}
1414
help: consider borrowing here
1515
|
1616
LL | foo(&s);
17-
| ~~
17+
| +
1818
LL | foo(&mut s);
19-
| ~~~~~~
19+
| ++++
2020

2121
error: aborting due to previous error
2222

src/test/ui/traits/negative-impls/negated-auto-traits-error.stderr

+8-6
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ error[E0277]: `dummy2::TestType` cannot be sent between threads safely
6161
--> $DIR/negated-auto-traits-error.rs:48:13
6262
|
6363
LL | is_send(Box::new(TestType));
64-
| ------- ^^^^^^^^^^^^^^^^^^
65-
| | |
66-
| | expected an implementor of trait `Send`
67-
| | help: consider borrowing here: `&Box::new(TestType)`
64+
| ------- ^^^^^^^^^^^^^^^^^^ expected an implementor of trait `Send`
65+
| |
6866
| required by a bound introduced by this call
6967
|
7068
= note: the trait bound `dummy2::TestType: Send` is not satisfied
@@ -75,6 +73,10 @@ note: required by a bound in `is_send`
7573
|
7674
LL | fn is_send<T: Send>(_: T) {}
7775
| ^^^^ required by this bound in `is_send`
76+
help: consider borrowing here
77+
|
78+
LL | is_send(&Box::new(TestType));
79+
| +
7880

7981
error[E0277]: `dummy3::TestType` cannot be sent between threads safely
8082
--> $DIR/negated-auto-traits-error.rs:56:13
@@ -120,9 +122,9 @@ LL | fn is_sync<T: Sync>(_: T) {}
120122
help: consider borrowing here
121123
|
122124
LL | is_sync(&Outer2(TestType));
123-
| ~~~~~~~~~~~~~~~~~
125+
| +
124126
LL | is_sync(&mut Outer2(TestType));
125-
| ~~~~~~~~~~~~~~~~~~~~~
127+
| ++++
126128

127129
error: aborting due to 7 previous errors
128130

0 commit comments

Comments
 (0)