Skip to content

Commit d2f54b1

Browse files
Adjust messages, address some nits
1 parent 2a16a12 commit d2f54b1

23 files changed

+91
-85
lines changed

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

+38-38
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
973973
|| ref_inner_ty_satisfies_pred
974974
{
975975
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
976+
// We don't want a borrowing suggestion on the fields in structs,
977+
// ```
978+
// struct Foo {
979+
// the_foos: Vec<Foo>
980+
// }
981+
// ```
982+
if !matches!(
983+
span.ctxt().outer_expn_data().kind,
984+
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
985+
) {
986+
return false;
987+
}
988+
if snippet.starts_with('&') {
989+
// This is already a literal borrow and the obligation is failing
990+
// somewhere else in the obligation chain. Do not suggest non-sense.
991+
return false;
992+
}
976993
// We have a very specific type of error, where just borrowing this argument
977994
// might solve the problem. In cases like this, the important part is the
978995
// original type obligation, not the last one that failed, which is arbitrary.
@@ -986,50 +1003,33 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
9861003
err.message =
9871004
vec![(rustc_errors::DiagnosticMessage::Str(msg), Style::NoStyle)];
9881005
}
989-
if snippet.starts_with('&') {
990-
// This is already a literal borrow and the obligation is failing
991-
// somewhere else in the obligation chain. Do not suggest non-sense.
992-
return false;
993-
}
9941006
err.span_label(
9951007
span,
996-
&format!(
997-
"expected an implementor of trait `{}`",
1008+
format!(
1009+
"the trait `{}` is not implemented for `{}`",
9981010
old_pred.print_modifiers_and_trait_path(),
1011+
old_pred.self_ty().skip_binder(),
9991012
),
10001013
);
10011014

1002-
// This if is to prevent a special edge-case
1003-
if matches!(
1004-
span.ctxt().outer_expn_data().kind,
1005-
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1006-
) {
1007-
// We don't want a borrowing suggestion on the fields in structs,
1008-
// ```
1009-
// struct Foo {
1010-
// the_foos: Vec<Foo>
1011-
// }
1012-
// ```
1013-
1014-
if imm_ref_self_ty_satisfies_pred && mut_ref_self_ty_satisfies_pred {
1015-
err.span_suggestions(
1016-
span.shrink_to_lo(),
1017-
"consider borrowing here",
1018-
["&".to_string(), "&mut ".to_string()].into_iter(),
1019-
Applicability::MaybeIncorrect,
1020-
);
1021-
} else {
1022-
let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
1023-
err.span_suggestion_verbose(
1024-
span.shrink_to_lo(),
1025-
&format!(
1026-
"consider{} borrowing here",
1027-
if is_mut { " mutably" } else { "" }
1028-
),
1029-
format!("&{}", if is_mut { "mut " } else { "" }),
1030-
Applicability::MaybeIncorrect,
1031-
);
1032-
}
1015+
if imm_ref_self_ty_satisfies_pred && mut_ref_self_ty_satisfies_pred {
1016+
err.span_suggestions(
1017+
span.shrink_to_lo(),
1018+
"consider borrowing here",
1019+
["&".to_string(), "&mut ".to_string()].into_iter(),
1020+
Applicability::MaybeIncorrect,
1021+
);
1022+
} else {
1023+
let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
1024+
err.span_suggestion_verbose(
1025+
span.shrink_to_lo(),
1026+
&format!(
1027+
"consider{} borrowing here",
1028+
if is_mut { " mutably" } else { "" }
1029+
),
1030+
format!("&{}", if is_mut { "mut " } else { "" }),
1031+
Applicability::MaybeIncorrect,
1032+
);
10331033
}
10341034
return true;
10351035
}

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1753,9 +1753,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17531753
{
17541754
for param in
17551755
[param_to_point_at, fallback_param_to_point_at, self_param_to_point_at]
1756+
.into_iter()
1757+
.flatten()
17561758
{
1757-
if let Some(param) = param
1758-
&& self.point_at_arg_if_possible(
1759+
if self.point_at_arg_if_possible(
17591760
error,
17601761
def_id,
17611762
param,
@@ -1784,17 +1785,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17841785
..
17851786
}) => {
17861787
for param in [param_to_point_at, fallback_param_to_point_at, self_param_to_point_at]
1788+
.into_iter()
1789+
.flatten()
17871790
{
1788-
if let Some(param) = param
1789-
&& self.point_at_arg_if_possible(
1790-
error,
1791-
def_id,
1792-
param,
1793-
hir_id,
1794-
segment.ident.span,
1795-
args,
1796-
)
1797-
{
1791+
if self.point_at_arg_if_possible(
1792+
error,
1793+
def_id,
1794+
param,
1795+
hir_id,
1796+
segment.ident.span,
1797+
args,
1798+
) {
17981799
return true;
17991800
}
18001801
}
@@ -1903,6 +1904,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19031904
if self.tcx.adjust_ident(expr_field.ident, variant_def_id) == field.ident(self.tcx)
19041905
{
19051906
error.obligation.cause.span = expr_field
1907+
.expr
19061908
.span
19071909
.find_ancestor_in_same_ctxt(error.obligation.cause.span)
19081910
.unwrap_or(expr_field.span);

src/test/ui/chalkify/type_wf.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `{float}: Foo` is not satisfied
2-
--> $DIR/type_wf.rs:19:9
2+
--> $DIR/type_wf.rs:19:12
33
|
44
LL | x: 5.0,
5-
| ^^^^^^ the trait `Foo` is not implemented for `{float}`
5+
| ^^^ the trait `Foo` is not implemented for `{float}`
66
|
77
= help: the trait `Foo` is implemented for `i32`
88
note: required by a bound in `S`

src/test/ui/consts/const-block-const-bound.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: can't drop `UnconstDrop` in const contexts
22
--> $DIR/const-block-const-bound.rs:20:11
33
|
44
LL | f(UnconstDrop);
5-
| - ^^^^^^^^^^^ expected an implementor of trait `~const Destruct`
5+
| - ^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `UnconstDrop`
66
| |
77
| required by a bound introduced by this call
88
|
@@ -23,7 +23,7 @@ error[E0277]: can't drop `NonDrop` in const contexts
2323
--> $DIR/const-block-const-bound.rs:22:11
2424
|
2525
LL | f(NonDrop);
26-
| - ^^^^^^^ expected an implementor of trait `~const Destruct`
26+
| - ^^^^^^^ the trait `~const Destruct` is not implemented for `NonDrop`
2727
| |
2828
| required by a bound introduced by this call
2929
|

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `B<C>: Copy` is not satisfied
22
--> $DIR/deriving-copyclone.rs:31:13
33
|
44
LL | is_copy(B { a: 1, b: C });
5-
| ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
5+
| ------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `B<C>`
66
| |
77
| required by a bound introduced by this call
88
|
@@ -26,7 +26,7 @@ error[E0277]: the trait bound `B<C>: Clone` is not satisfied
2626
--> $DIR/deriving-copyclone.rs:32:14
2727
|
2828
LL | is_clone(B { a: 1, b: C });
29-
| -------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Clone`
29+
| -------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `B<C>`
3030
| |
3131
| required by a bound introduced by this call
3232
|
@@ -50,7 +50,7 @@ error[E0277]: the trait bound `B<D>: Copy` is not satisfied
5050
--> $DIR/deriving-copyclone.rs:35:13
5151
|
5252
LL | is_copy(B { a: 1, b: D });
53-
| ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
53+
| ------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `B<D>`
5454
| |
5555
| required by a bound introduced by this call
5656
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +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-
| ^^^^^^^ expected an implementor of trait `IntoIterator`
5+
| ^^^^^^^ the trait `IntoIterator` is not implemented for `dyn Iterator<Item = &'a mut u8>`
66
|
77
= note: the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is not satisfied
88
= note: required for `dyn Iterator<Item = &'a mut u8>` to implement `IntoIterator`

src/test/ui/kindck/kindck-impl-type-params-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ fn take_param<T:Foo>(foo: &T) { }
1111
fn main() {
1212
let x: Box<_> = Box::new(3);
1313
take_param(&x);
14-
//~^ ERROR the trait bound `Box<{integer}>: Foo` is not satisfied
14+
//~^ ERROR the trait bound `Box<{integer}>: Copy` is not satisfied
1515
}

src/test/ui/kindck/kindck-impl-type-params-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `Box<{integer}>: Foo` is not satisfied
1+
error[E0277]: the trait bound `Box<{integer}>: Copy` is not satisfied
22
--> $DIR/kindck-impl-type-params-2.rs:13:16
33
|
44
LL | take_param(&x);

src/test/ui/kindck/kindck-inherited-copy-bound.curr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `Box<{integer}>: Foo` is not satisfied
1+
error[E0277]: the trait bound `Box<{integer}>: Copy` is not satisfied
22
--> $DIR/kindck-inherited-copy-bound.rs:21:16
33
|
44
LL | take_param(&x);

src/test/ui/kindck/kindck-inherited-copy-bound.object_safe_for_dispatch.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `Box<{integer}>: Foo` is not satisfied
1+
error[E0277]: the trait bound `Box<{integer}>: Copy` is not satisfied
22
--> $DIR/kindck-inherited-copy-bound.rs:21:16
33
|
44
LL | take_param(&x);

src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const _: () = check($exp);
55
| ----- required by a bound introduced by this call
66
...
77
LL | NonTrivialDrop,
8-
| ^^^^^^^^^^^^^^ expected an implementor of trait `~const Destruct`
8+
| ^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `NonTrivialDrop`
99
|
1010
= note: the trait bound `NonTrivialDrop: ~const Destruct` is not satisfied
1111
note: required by a bound in `check`
@@ -52,7 +52,7 @@ LL | const _: () = check($exp);
5252
| ----- required by a bound introduced by this call
5353
...
5454
LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData),
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an implementor of trait `~const Destruct`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `ConstDropImplWithBounds<NonTrivialDrop>`
5656
|
5757
note: required for `ConstDropImplWithBounds<NonTrivialDrop>` to implement `~const Destruct`
5858
--> $DIR/const-drop-fail.rs:28:25

src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const _: () = check($exp);
55
| ----- required by a bound introduced by this call
66
...
77
LL | NonTrivialDrop,
8-
| ^^^^^^^^^^^^^^ expected an implementor of trait `~const Destruct`
8+
| ^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `NonTrivialDrop`
99
|
1010
= note: the trait bound `NonTrivialDrop: ~const Destruct` is not satisfied
1111
note: required by a bound in `check`
@@ -52,7 +52,7 @@ LL | const _: () = check($exp);
5252
| ----- required by a bound introduced by this call
5353
...
5454
LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData),
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an implementor of trait `~const Destruct`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `ConstDropImplWithBounds<NonTrivialDrop>`
5656
|
5757
note: required for `ConstDropImplWithBounds<NonTrivialDrop>` to implement `~const Destruct`
5858
--> $DIR/const-drop-fail.rs:28:25

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ error[E0277]: the trait bound `S: Trait` is not satisfied
2121
--> $DIR/imm-ref-trait-object-literal.rs:13:7
2222
|
2323
LL | foo(s);
24-
| --- ^ expected an implementor of trait `Trait`
24+
| --- ^ the trait `Trait` is not implemented for `S`
2525
| |
2626
| required by a bound introduced by this call
2727
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: expected a `FnMut<(char,)>` closure, found `String`
22
--> $DIR/issue-62843.rs:4:32
33
|
44
LL | println!("{:?}", line.find(pattern));
5-
| ---- ^^^^^^^ expected an implementor of trait `Pattern<'_>`
5+
| ---- ^^^^^^^ the trait `Pattern<'_>` is not implemented for `String`
66
| |
77
| required by a bound introduced by this call
88
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `i32: Tr` is not satisfied
22
--> $DIR/issue-84973-2.rs:11:9
33
|
44
LL | foo(a);
5-
| --- ^ expected an implementor of trait `Tr`
5+
| --- ^ the trait `Tr` is not implemented for `i32`
66
| |
77
| required by a bound introduced by this call
88
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ error[E0277]: the trait bound `f32: Tr` is not satisfied
1717
--> $DIR/issue-84973-negative.rs:11:9
1818
|
1919
LL | bar(b);
20-
| --- ^ expected an implementor of trait `Tr`
20+
| --- ^ the trait `Tr` is not implemented for `f32`
2121
| |
2222
| required by a bound introduced by this call
2323
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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-
| ---------- ^ expected an implementor of trait `SomeTrait`
5+
| ---------- ^ the trait `SomeTrait` is not implemented for `Fancy`
66
| |
77
| required by a bound introduced by this call
88
|

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the size for values of type `[i32]` cannot be known at compilation
22
--> $DIR/slice-issue-87994.rs:3:12
33
|
44
LL | for _ in v[1..] {
5-
| ^^^^^^ expected an implementor of trait `IntoIterator`
5+
| ^^^^^^ the trait `IntoIterator` is not implemented for `[i32]`
66
|
77
= note: the trait bound `[i32]: IntoIterator` is not satisfied
88
= note: required for `[i32]` to implement `IntoIterator`
@@ -17,7 +17,7 @@ error[E0277]: `[i32]` is not an iterator
1717
--> $DIR/slice-issue-87994.rs:3:12
1818
|
1919
LL | for _ in v[1..] {
20-
| ^^^^^^ expected an implementor of trait `IntoIterator`
20+
| ^^^^^^ the trait `IntoIterator` is not implemented for `[i32]`
2121
|
2222
= note: the trait bound `[i32]: IntoIterator` is not satisfied
2323
= note: required for `[i32]` to implement `IntoIterator`
@@ -32,7 +32,7 @@ error[E0277]: the size for values of type `[K]` cannot be known at compilation t
3232
--> $DIR/slice-issue-87994.rs:11:13
3333
|
3434
LL | for i2 in v2[1..] {
35-
| ^^^^^^^ expected an implementor of trait `IntoIterator`
35+
| ^^^^^^^ the trait `IntoIterator` is not implemented for `[K]`
3636
|
3737
= note: the trait bound `[K]: IntoIterator` is not satisfied
3838
= note: required for `[K]` to implement `IntoIterator`
@@ -47,7 +47,7 @@ error[E0277]: `[K]` is not an iterator
4747
--> $DIR/slice-issue-87994.rs:11:13
4848
|
4949
LL | for i2 in v2[1..] {
50-
| ^^^^^^^ expected an implementor of trait `IntoIterator`
50+
| ^^^^^^^ the trait `IntoIterator` is not implemented for `[K]`
5151
|
5252
= note: the trait bound `[K]: IntoIterator` is not satisfied
5353
= note: required for `[K]` to implement `IntoIterator`

src/test/ui/suggestions/suggest-imm-mut-trait-implementations.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `A: Trait` is not satisfied
22
--> $DIR/suggest-imm-mut-trait-implementations.rs:20:9
33
|
44
LL | foo(a);
5-
| --- ^ expected an implementor of trait `Trait`
5+
| --- ^ the trait `Trait` is not implemented for `A`
66
| |
77
| required by a bound introduced by this call
88
|
@@ -22,7 +22,7 @@ error[E0277]: the trait bound `B: Trait` is not satisfied
2222
--> $DIR/suggest-imm-mut-trait-implementations.rs:21:9
2323
|
2424
LL | foo(b);
25-
| --- ^ expected an implementor of trait `Trait`
25+
| --- ^ the trait `Trait` is not implemented for `B`
2626
| |
2727
| required by a bound introduced by this call
2828
|
@@ -40,7 +40,7 @@ error[E0277]: the trait bound `C: Trait` is not satisfied
4040
--> $DIR/suggest-imm-mut-trait-implementations.rs:22:9
4141
|
4242
LL | foo(c);
43-
| --- ^ expected an implementor of trait `Trait`
43+
| --- ^ the trait `Trait` is not implemented for `C`
4444
| |
4545
| required by a bound introduced by this call
4646
|

src/test/ui/traits/bound/on-structs-and-enums-locals.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | struct Foo<T:Trait> {
1111
| ^^^^^ required by this bound in `Foo`
1212

1313
error[E0277]: the trait bound `{integer}: Trait` is not satisfied
14-
--> $DIR/on-structs-and-enums-locals.rs:11:9
14+
--> $DIR/on-structs-and-enums-locals.rs:11:12
1515
|
1616
LL | x: 3
17-
| ^^^^ the trait `Trait` is not implemented for `{integer}`
17+
| ^ the trait `Trait` is not implemented for `{integer}`
1818
|
1919
note: required by a bound in `Foo`
2020
--> $DIR/on-structs-and-enums-locals.rs:5:14

0 commit comments

Comments
 (0)