Skip to content

Commit d01ac0d

Browse files
committed
add the logic for when other_ty is FnDef
1 parent 199b0ba commit d01ac0d

File tree

4 files changed

+90
-20
lines changed

4 files changed

+90
-20
lines changed

src/librustc_typeck/check/op.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,22 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
333333
lhs_ty);
334334

335335
if !lhs_expr.span.eq(&rhs_expr.span) {
336-
self.add_type_neq_err_label(&mut err,
337-
lhs_expr.span,
338-
lhs_ty,
339-
rhs_ty,
340-
op,
341-
is_assign);
342-
self.add_type_neq_err_label(&mut err,
343-
rhs_expr.span,
344-
rhs_ty,
345-
lhs_ty,
346-
op,
347-
is_assign);
336+
self.add_type_neq_err_label(
337+
&mut err,
338+
lhs_expr.span,
339+
lhs_ty,
340+
rhs_ty,
341+
op,
342+
is_assign
343+
);
344+
self.add_type_neq_err_label(
345+
&mut err,
346+
rhs_expr.span,
347+
rhs_ty,
348+
lhs_ty,
349+
op,
350+
is_assign
351+
);
348352
}
349353

350354
let mut suggested_deref = false;
@@ -447,21 +451,35 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
447451
}
448452
};
449453

454+
let other_ty = if let FnDef(def_id, _) = other_ty.sty {
455+
let hir_id = &self.tcx.hir().as_local_hir_id(def_id).unwrap();
456+
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(*hir_id) {
457+
Some(f) => f.clone().output(),
458+
None => {
459+
bug!("No fn-sig entry for def_id={:?}", def_id);
460+
}
461+
}
462+
} else {
463+
other_ty
464+
};
465+
450466
if self.lookup_op_method(fn_sig.output(),
451467
&[other_ty],
452468
Op::Binary(op, is_assign))
453469
.is_ok() {
454-
let variable_snippet = if fn_sig.inputs().len() > 0 {
455-
format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap())
470+
let (variable_snippet, applicability) = if fn_sig.inputs().len() > 0 {
471+
(format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap()),
472+
Applicability::HasPlaceholders)
456473
} else {
457-
format!("{}()", source_map.span_to_snippet(span).unwrap())
474+
(format!("{}()", source_map.span_to_snippet(span).unwrap()),
475+
Applicability::MaybeIncorrect)
458476
};
459477

460478
err.span_suggestion(
461479
span,
462-
"did you forget",
480+
"you might have forgotten to call this function",
463481
variable_snippet,
464-
Applicability::MachineApplicable,
482+
applicability,
465483
);
466484
}
467485
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ LL | let x = f == g;
77
| fn() {main::f}
88
|
99
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`
10+
help: you might have forgotten to call this function
11+
|
12+
LL | let x = f() == g;
13+
| ^^^
14+
help: you might have forgotten to call this function
15+
|
16+
LL | let x = f == g();
17+
| ^^^
1018

1119
error[E0308]: mismatched types
1220
--> $DIR/fn-compare-mismatch.rs:4:18

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

+7
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ fn main() {
1616
bar > 13;
1717
//~^ ERROR 16:9: 16:10: binary operation `>` cannot be applied to type `fn(i64) -> i64 {bar}` [E0369]
1818
//~| ERROR 16:11: 16:13: mismatched types [E0308]
19+
20+
foo > foo;
21+
//~^ ERROR 20:9: 20:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
22+
23+
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]
1926
}

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

+40-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | foo > 12;
55
| --- ^ -- {integer}
66
| |
77
| fn() -> i32 {foo}
8-
| help: did you forget: `foo()`
8+
| help: you might have forgotten to call this function: `foo()`
99
|
1010
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
1111

@@ -25,7 +25,7 @@ LL | bar > 13;
2525
| --- ^ -- {integer}
2626
| |
2727
| fn(i64) -> i64 {bar}
28-
| help: did you forget: `bar( /* arguments */ )`
28+
| help: you might have forgotten to call this function: `bar( /* arguments */ )`
2929
|
3030
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn(i64) -> i64 {bar}`
3131

@@ -38,7 +38,44 @@ LL | bar > 13;
3838
= note: expected type `fn(i64) -> i64 {bar}`
3939
found type `i64`
4040

41-
error: aborting due to 4 previous errors
41+
error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}`
42+
--> $DIR/issue-59488.rs:20:9
43+
|
44+
LL | foo > foo;
45+
| --- ^ --- fn() -> i32 {foo}
46+
| |
47+
| fn() -> i32 {foo}
48+
|
49+
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
50+
help: you might have forgotten to call this function
51+
|
52+
LL | foo() > foo;
53+
| ^^^^^
54+
help: you might have forgotten to call this function
55+
|
56+
LL | foo > foo();
57+
| ^^^^^
58+
59+
error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}`
60+
--> $DIR/issue-59488.rs:23:9
61+
|
62+
LL | foo > bar;
63+
| --- ^ --- fn(i64) -> i64 {bar}
64+
| |
65+
| fn() -> i32 {foo}
66+
|
67+
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
68+
69+
error[E0308]: mismatched types
70+
--> $DIR/issue-59488.rs:23:11
71+
|
72+
LL | foo > bar;
73+
| ^^^ expected fn item, found a different fn item
74+
|
75+
= note: expected type `fn() -> i32 {foo}`
76+
found type `fn(i64) -> i64 {bar}`
77+
78+
error: aborting due to 7 previous errors
4279

4380
Some errors occurred: E0308, E0369.
4481
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)