Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement for comparision against fn #59798

Merged
merged 5 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -333,8 +333,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
lhs_ty);

if !lhs_expr.span.eq(&rhs_expr.span) {
err.span_label(lhs_expr.span, lhs_ty.to_string());
err.span_label(rhs_expr.span, rhs_ty.to_string());
self.add_type_neq_err_label(&mut err, lhs_expr.span, lhs_ty);
self.add_type_neq_err_label(&mut err, rhs_expr.span, rhs_ty);
}

let mut suggested_deref = false;
Expand Down Expand Up @@ -415,6 +415,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
(lhs_ty, rhs_ty, return_ty)
}

fn add_type_neq_err_label(
&self,
err: &mut errors::DiagnosticBuilder<'_>,
span: Span,
ty: Ty<'tcx>,
) {
err.span_label(span, ty.to_string());
if let FnDef(..) = ty.sty {
err.span_label(span, "did you forget `()`?");
}
estebank marked this conversation as resolved.
Show resolved Hide resolved
}

fn check_str_addition(
&self,
expr: &'gcx hir::Expr,
Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/fn/fn-compare-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-59488.rs
Original file line number Diff line number Diff line change
@@ -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]
}
24 changes: 24 additions & 0 deletions src/test/ui/issues/issue-59488.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ LL | f<X>();
| -^- X
| |
| fn() {f::<_>}
| did you forget `()`?
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() {f::<_>}`

Expand Down