Skip to content

Commit 199b0ba

Browse files
committed
improve error messages
1 parent cbcbd2c commit 199b0ba

File tree

5 files changed

+83
-17
lines changed

5 files changed

+83
-17
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,18 @@ 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, lhs_expr.span, lhs_ty);
337-
self.add_type_neq_err_label(&mut err, rhs_expr.span, rhs_ty);
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);
338348
}
339349

340350
let mut suggested_deref = false;
@@ -420,10 +430,40 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
420430
err: &mut errors::DiagnosticBuilder<'_>,
421431
span: Span,
422432
ty: Ty<'tcx>,
433+
other_ty: Ty<'tcx>,
434+
op: hir::BinOp,
435+
is_assign: IsAssign,
423436
) {
424437
err.span_label(span, ty.to_string());
425-
if let FnDef(..) = ty.sty {
426-
err.span_label(span, "did you forget `()`?");
438+
if let FnDef(def_id, _) = ty.sty {
439+
let source_map = self.tcx.sess.source_map();
440+
let hir_id = &self.tcx.hir().as_local_hir_id(def_id).unwrap();
441+
let fn_sig = {
442+
match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(*hir_id) {
443+
Some(f) => f.clone(),
444+
None => {
445+
bug!("No fn-sig entry for def_id={:?}", def_id);
446+
}
447+
}
448+
};
449+
450+
if self.lookup_op_method(fn_sig.output(),
451+
&[other_ty],
452+
Op::Binary(op, is_assign))
453+
.is_ok() {
454+
let variable_snippet = if fn_sig.inputs().len() > 0 {
455+
format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap())
456+
} else {
457+
format!("{}()", source_map.span_to_snippet(span).unwrap())
458+
};
459+
460+
err.span_suggestion(
461+
span,
462+
"did you forget",
463+
variable_snippet,
464+
Applicability::MachineApplicable,
465+
);
466+
}
427467
}
428468
}
429469

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ 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-
| - ^^ -
6-
| | |
7-
| | fn() {main::g}
8-
| | did you forget `()`?
5+
| - ^^ - fn() {main::g}
6+
| |
97
| fn() {main::f}
10-
| did you forget `()`?
118
|
129
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`
1310

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
// ignore-tidy-linelength
2+
13
fn foo() -> i32 {
24
42
35
}
46

7+
fn bar(a: i64) -> i64 {
8+
43
9+
}
10+
511
fn main() {
612
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]
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]
15+
16+
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]
919
}

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
error[E0369]: binary operation `>` cannot be applied to type `fn() -> i32 {foo}`
2-
--> $DIR/issue-59488.rs:6:9
2+
--> $DIR/issue-59488.rs:12:9
33
|
44
LL | foo > 12;
55
| --- ^ -- {integer}
66
| |
77
| fn() -> i32 {foo}
8-
| did you forget `()`?
8+
| help: did you forget: `foo()`
99
|
1010
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
1111

1212
error[E0308]: mismatched types
13-
--> $DIR/issue-59488.rs:6:11
13+
--> $DIR/issue-59488.rs:12:11
1414
|
1515
LL | foo > 12;
1616
| ^^ expected fn item, found integer
1717
|
1818
= note: expected type `fn() -> i32 {foo}`
19-
found type `{integer}`
19+
found type `i32`
2020

21-
error: aborting due to 2 previous errors
21+
error[E0369]: binary operation `>` cannot be applied to type `fn(i64) -> i64 {bar}`
22+
--> $DIR/issue-59488.rs:16:9
23+
|
24+
LL | bar > 13;
25+
| --- ^ -- {integer}
26+
| |
27+
| fn(i64) -> i64 {bar}
28+
| help: did you forget: `bar( /* arguments */ )`
29+
|
30+
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn(i64) -> i64 {bar}`
31+
32+
error[E0308]: mismatched types
33+
--> $DIR/issue-59488.rs:16:11
34+
|
35+
LL | bar > 13;
36+
| ^^ expected fn item, found integer
37+
|
38+
= note: expected type `fn(i64) -> i64 {bar}`
39+
found type `i64`
40+
41+
error: aborting due to 4 previous errors
2242

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

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

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

0 commit comments

Comments
 (0)