Skip to content

Commit fb1b222

Browse files
committed
Auto merge of #60051 - estebank:fn-sugg, r=davidtwco
Do not mention missing `PartialOrd` impl when involving uncalled fns
2 parents 9387927 + 84af684 commit fb1b222

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

src/librustc_typeck/check/op.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
332332
op.node.as_str(),
333333
lhs_ty);
334334

335+
let mut involves_fn = false;
335336
if !lhs_expr.span.eq(&rhs_expr.span) {
336-
self.add_type_neq_err_label(
337+
involves_fn |= self.add_type_neq_err_label(
337338
&mut err,
338339
lhs_expr.span,
339340
lhs_ty,
340341
rhs_ty,
341342
op,
342343
is_assign
343344
);
344-
self.add_type_neq_err_label(
345+
involves_fn |= self.add_type_neq_err_label(
345346
&mut err,
346347
rhs_expr.span,
347348
rhs_ty,
@@ -410,7 +411,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
410411
"`{}` might need a bound for `{}`",
411412
lhs_ty, missing_trait
412413
));
413-
} else if !suggested_deref {
414+
} else if !suggested_deref && !involves_fn {
414415
err.note(&format!(
415416
"an implementation of `{}` might \
416417
be missing for `{}`",
@@ -429,6 +430,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
429430
(lhs_ty, rhs_ty, return_ty)
430431
}
431432

433+
/// If one of the types is an uncalled function and calling it would yield the other type,
434+
/// suggest calling the function. Returns wether a suggestion was given.
432435
fn add_type_neq_err_label(
433436
&self,
434437
err: &mut errors::DiagnosticBuilder<'_>,
@@ -437,7 +440,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
437440
other_ty: Ty<'tcx>,
438441
op: hir::BinOp,
439442
is_assign: IsAssign,
440-
) {
443+
) -> bool /* did we suggest to call a function because of missing parenthesis? */ {
441444
err.span_label(span, ty.to_string());
442445
if let FnDef(def_id, _) = ty.sty {
443446
let source_map = self.tcx.sess.source_map();
@@ -481,8 +484,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
481484
variable_snippet,
482485
applicability,
483486
);
487+
return true;
484488
}
485489
}
490+
false
486491
}
487492

488493
fn check_str_addition(

src/test/ui/fn/fn-compare-mismatch.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let x = f == g;
55
| - ^^ - fn() {main::g}
66
| |
77
| fn() {main::f}
8-
|
9-
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`
108
help: you might have forgotten to call this function
119
|
1210
LL | let x = f() == g;

src/test/ui/issues/issue-59488.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ fn bar(a: i64) -> i64 {
1010

1111
fn main() {
1212
foo > 12;
13-
//~^ ERROR 12:9: 12:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
14-
//~| ERROR 12:11: 12:13: mismatched types [E0308]
13+
//~^ ERROR binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
14+
//~| ERROR mismatched types [E0308]
1515

1616
bar > 13;
17-
//~^ ERROR 16:9: 16:10: binary operation `>` cannot be applied to type `fn(i64) -> i64 {bar}` [E0369]
18-
//~| ERROR 16:11: 16:13: mismatched types [E0308]
17+
//~^ ERROR binary operation `>` cannot be applied to type `fn(i64) -> i64 {bar}` [E0369]
18+
//~| ERROR mismatched types [E0308]
1919

2020
foo > foo;
21-
//~^ ERROR 20:9: 20:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
21+
//~^ ERROR binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
2222

2323
foo > bar;
24-
//~^ ERROR 23:9: 23:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
25-
//~| ERROR 23:11: 23:14: mismatched types [E0308]
24+
//~^ ERROR binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
25+
//~| ERROR mismatched types [E0308]
2626
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ LL | foo > 12;
66
| |
77
| fn() -> i32 {foo}
88
| help: you might have forgotten to call this function: `foo()`
9-
|
10-
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
119

1210
error[E0308]: mismatched types
1311
--> $DIR/issue-59488.rs:12:11
@@ -26,8 +24,6 @@ LL | bar > 13;
2624
| |
2725
| fn(i64) -> i64 {bar}
2826
| help: you might have forgotten to call this function: `bar( /* arguments */ )`
29-
|
30-
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn(i64) -> i64 {bar}`
3127

3228
error[E0308]: mismatched types
3329
--> $DIR/issue-59488.rs:16:11
@@ -45,8 +41,6 @@ LL | foo > foo;
4541
| --- ^ --- fn() -> i32 {foo}
4642
| |
4743
| fn() -> i32 {foo}
48-
|
49-
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
5044
help: you might have forgotten to call this function
5145
|
5246
LL | foo() > foo;

0 commit comments

Comments
 (0)