From 9c0826902f0f8c479d1622d63abadcfe4f64e86e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 3 Apr 2020 02:28:23 +0200 Subject: [PATCH] add_type_neq_err_label: don't .unwrap --- src/librustc_typeck/check/op.rs | 32 +++++++-------- ...sue-70724-add_type_neq_err_label-unwrap.rs | 10 +++++ ...70724-add_type_neq_err_label-unwrap.stderr | 41 +++++++++++++++++++ 3 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.rs create mode 100644 src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index 7367338bddf54..cac9113fd5d30 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -481,7 +481,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } /// If one of the types is an uncalled function and calling it would yield the other type, - /// suggest calling the function. Returns whether a suggestion was given. + /// suggest calling the function. Returns `true` if suggestion would apply (even if not given). fn add_type_neq_err_label( &self, err: &mut rustc_errors::DiagnosticBuilder<'_>, @@ -514,24 +514,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .lookup_op_method(fn_sig.output(), &[other_ty], Op::Binary(op, is_assign)) .is_ok() { - let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() { - ( - format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap()), - Applicability::HasPlaceholders, - ) - } else { - ( - format!("{}()", source_map.span_to_snippet(span).unwrap()), - Applicability::MaybeIncorrect, - ) - }; + if let Ok(snippet) = source_map.span_to_snippet(span) { + let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() { + (format!("{}( /* arguments */ )", snippet), Applicability::HasPlaceholders) + } else { + (format!("{}()", snippet), Applicability::MaybeIncorrect) + }; - err.span_suggestion( - span, - "you might have forgotten to call this function", - variable_snippet, - applicability, - ); + err.span_suggestion( + span, + "you might have forgotten to call this function", + variable_snippet, + applicability, + ); + } return true; } } diff --git a/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.rs b/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.rs new file mode 100644 index 0000000000000..c2683157f797f --- /dev/null +++ b/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.rs @@ -0,0 +1,10 @@ +fn a() -> i32 { + 3 +} + +pub fn main() { + assert_eq!(a, 0); + //~^ ERROR binary operation `==` cannot + //~| ERROR mismatched types + //~| ERROR doesn't implement +} diff --git a/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr b/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr new file mode 100644 index 0000000000000..467c15cc52d45 --- /dev/null +++ b/src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr @@ -0,0 +1,41 @@ +error[E0369]: binary operation `==` cannot be applied to type `fn() -> i32 {a}` + --> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5 + | +LL | assert_eq!(a, 0); + | ^^^^^^^^^^^^^^^^^ + | | + | fn() -> i32 {a} + | {integer} + | help: you might have forgotten to call this function: `*left_val()` + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5 + | +LL | assert_eq!(a, 0); + | ^^^^^^^^^^^^^^^^^ expected fn item, found integer + | + = note: expected fn item `fn() -> i32 {a}` + found type `i32` + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: `fn() -> i32 {a}` doesn't implement `std::fmt::Debug` + --> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5 + | +LL | fn a() -> i32 { + | - consider calling this function +... +LL | assert_eq!(a, 0); + | ^^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug` + | + = help: the trait `std::fmt::Debug` is not implemented for `fn() -> i32 {a}` + = help: use parentheses to call the function: `a()` + = note: required because of the requirements on the impl of `std::fmt::Debug` for `&fn() -> i32 {a}` + = note: required by `std::fmt::Debug::fmt` + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0277, E0308, E0369. +For more information about an error, try `rustc --explain E0277`.