From 89eac91e0576bbf81af4e10a8be47b935592a922 Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Sun, 7 Apr 2019 22:51:33 +0900 Subject: [PATCH] Improvement for comparision against fn --- src/librustc_typeck/check/op.rs | 10 +++++++- src/test/ui/fn/fn-compare-mismatch.stderr | 7 ++++-- src/test/ui/issues/issue-59488.rs | 9 +++++++ src/test/ui/issues/issue-59488.stderr | 24 +++++++++++++++++++ ...quire-parens-for-chained-comparison.stderr | 1 + 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/issues/issue-59488.rs create mode 100644 src/test/ui/issues/issue-59488.stderr diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index d6932094dddb..70c9d9ddee3d 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -3,7 +3,7 @@ use super::{FnCtxt, Needs}; use super::method::MethodCallee; use rustc::ty::{self, Ty, TypeFoldable}; -use rustc::ty::TyKind::{Ref, Adt, Str, Uint, Never, Tuple, Char, Array}; +use rustc::ty::TyKind::{Ref, Adt, FnDef, Str, Uint, Never, Tuple, Char, Array}; use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability}; use rustc::infer::type_variable::TypeVariableOrigin; use errors::{self,Applicability}; @@ -334,9 +334,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if !lhs_expr.span.eq(&rhs_expr.span) { err.span_label(lhs_expr.span, lhs_ty.to_string()); + if let FnDef(..) = lhs_ty.sty { + err.span_label(lhs_expr.span, "did you forget `()`?"); + } + err.span_label(rhs_expr.span, rhs_ty.to_string()); + if let FnDef(..) = rhs_ty.sty { + err.span_label(rhs_expr.span, "did you forget `()`?"); + } } + let mut suggested_deref = false; if let Ref(_, mut rty, _) = lhs_ty.sty { if { diff --git a/src/test/ui/fn/fn-compare-mismatch.stderr b/src/test/ui/fn/fn-compare-mismatch.stderr index 07b93d9aae7e..6f1155d597e6 100644 --- a/src/test/ui/fn/fn-compare-mismatch.stderr +++ b/src/test/ui/fn/fn-compare-mismatch.stderr @@ -2,9 +2,12 @@ error[E0369]: binary operation `==` cannot be applied to type `fn() {main::f}` --> $DIR/fn-compare-mismatch.rs:4:15 | LL | let x = f == g; - | - ^^ - fn() {main::g} - | | + | - ^^ - + | | | + | | fn() {main::g} + | | did you forget `()`? | fn() {main::f} + | did you forget `()`? | = note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}` diff --git a/src/test/ui/issues/issue-59488.rs b/src/test/ui/issues/issue-59488.rs new file mode 100644 index 000000000000..937eb72fa22c --- /dev/null +++ b/src/test/ui/issues/issue-59488.rs @@ -0,0 +1,9 @@ +fn foo() -> i32 { + 42 +} + +fn main() { + foo > 12; + //~^ ERROR 6:9: 6:10: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` [E0369] + //~| ERROR 6:11: 6:13: mismatched types [E0308] +} diff --git a/src/test/ui/issues/issue-59488.stderr b/src/test/ui/issues/issue-59488.stderr new file mode 100644 index 000000000000..7bd7700026cc --- /dev/null +++ b/src/test/ui/issues/issue-59488.stderr @@ -0,0 +1,24 @@ +error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}` + --> $DIR/issue-59488.rs:6:9 + | +LL | foo > 12; + | --- ^ -- {integer} + | | + | fn() -> i32 {foo} + | did you forget `()`? + | + = note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}` + +error[E0308]: mismatched types + --> $DIR/issue-59488.rs:6:11 + | +LL | foo > 12; + | ^^ expected fn item, found integer + | + = note: expected type `fn() -> i32 {foo}` + found type `{integer}` + +error: aborting due to 2 previous errors + +Some errors occurred: E0308, E0369. +For more information about an error, try `rustc --explain E0308`. diff --git a/src/test/ui/parser/require-parens-for-chained-comparison.stderr b/src/test/ui/parser/require-parens-for-chained-comparison.stderr index 8899b0d43cd8..d44e2242cc23 100644 --- a/src/test/ui/parser/require-parens-for-chained-comparison.stderr +++ b/src/test/ui/parser/require-parens-for-chained-comparison.stderr @@ -44,6 +44,7 @@ LL | f(); | -^- X | | | fn() {f::<_>} + | did you forget `()`? | = note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() {f::<_>}`