Skip to content

Commit 89eac91

Browse files
committed
Improvement for comparision against fn
1 parent 4fb888b commit 89eac91

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::{FnCtxt, Needs};
44
use super::method::MethodCallee;
55
use rustc::ty::{self, Ty, TypeFoldable};
6-
use rustc::ty::TyKind::{Ref, Adt, Str, Uint, Never, Tuple, Char, Array};
6+
use rustc::ty::TyKind::{Ref, Adt, FnDef, Str, Uint, Never, Tuple, Char, Array};
77
use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
88
use rustc::infer::type_variable::TypeVariableOrigin;
99
use errors::{self,Applicability};
@@ -334,9 +334,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
334334

335335
if !lhs_expr.span.eq(&rhs_expr.span) {
336336
err.span_label(lhs_expr.span, lhs_ty.to_string());
337+
if let FnDef(..) = lhs_ty.sty {
338+
err.span_label(lhs_expr.span, "did you forget `()`?");
339+
}
340+
337341
err.span_label(rhs_expr.span, rhs_ty.to_string());
342+
if let FnDef(..) = rhs_ty.sty {
343+
err.span_label(rhs_expr.span, "did you forget `()`?");
344+
}
338345
}
339346

347+
340348
let mut suggested_deref = false;
341349
if let Ref(_, mut rty, _) = lhs_ty.sty {
342350
if {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0369]: binary operation `==` cannot be applied to type `fn() {main::f}`
22
--> $DIR/fn-compare-mismatch.rs:4:15
33
|
44
LL | let x = f == g;
5-
| - ^^ - fn() {main::g}
6-
| |
5+
| - ^^ -
6+
| | |
7+
| | fn() {main::g}
8+
| | did you forget `()`?
79
| fn() {main::f}
10+
| did you forget `()`?
811
|
912
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`
1013

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo() -> i32 {
2+
42
3+
}
4+
5+
fn main() {
6+
foo > 12;
7+
//~^ ERROR 6:9: 6:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369]
8+
//~| ERROR 6:11: 6:13: mismatched types [E0308]
9+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}`
2+
--> $DIR/issue-59488.rs:6:9
3+
|
4+
LL | foo > 12;
5+
| --- ^ -- {integer}
6+
| |
7+
| fn() -> i32 {foo}
8+
| did you forget `()`?
9+
|
10+
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/issue-59488.rs:6:11
14+
|
15+
LL | foo > 12;
16+
| ^^ expected fn item, found integer
17+
|
18+
= note: expected type `fn() -> i32 {foo}`
19+
found type `{integer}`
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors occurred: E0308, E0369.
24+
For more information about an error, try `rustc --explain E0308`.

src/test/ui/parser/require-parens-for-chained-comparison.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ LL | f<X>();
4444
| -^- X
4545
| |
4646
| fn() {f::<_>}
47+
| did you forget `()`?
4748
|
4849
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() {f::<_>}`
4950

0 commit comments

Comments
 (0)