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

Lines changed: 38 additions & 38 deletions
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

Lines changed: 14 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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);

0 commit comments

Comments
 (0)