From aaf5d5b6633de6e9dc0ed5733f86be927ef3dafc Mon Sep 17 00:00:00 2001 From: Hila Date: Wed, 3 Aug 2016 20:32:47 +0300 Subject: [PATCH] Changed error message for enum variants --- src/librustc/infer/error_reporting.rs | 42 ++++++++++++++++++++++++++- src/test/compile-fail/issue-35241.rs | 40 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/issue-35241.rs diff --git a/src/librustc/infer/error_reporting.rs b/src/librustc/infer/error_reporting.rs index 511cc32d2e1e6..871ffa361d80a 100644 --- a/src/librustc/infer/error_reporting.rs +++ b/src/librustc/infer/error_reporting.rs @@ -521,6 +521,45 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { } } + fn note_possible_function_call(&self, + diag: &mut DiagnosticBuilder<'tcx>, + values: Option>, + span: Span) + { + if let Some(infer::Types(ty::error::ExpectedFound { + expected, found + })) = values { + let (def_id, ret_ty) = match found.sty { + ty::TyFnDef(def, _, fty) => (Some(def), fty.sig.output()), + ty::TyFnPtr(fty) => (None, fty.sig.output()), + _ => return + }; + + let ok = self.probe(|_| { + match self.replace_late_bound_regions_with_fresh_var( + span, + super::LateBoundRegionConversionTime::HigherRankedType, + &ret_ty) + { + (ty::FnConverging(ret_ty), _) => { + self.can_equate(&ret_ty, &expected).is_ok() + } + (ty::FnDiverging, _) => true + } + }); + + if ok { + let message = match def_id { + Some(def_id) => { + format!("`{}` is a function", self.tcx.item_path_str(def_id)) + } + _ => format!("found a function") + }; + diag.help(&format!("{} - maybe try calling it?", message)); + } + } + } + pub fn note_type_err(&self, diag: &mut DiagnosticBuilder<'tcx>, origin: TypeOrigin, @@ -529,7 +568,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { { let expected_found = match values { None => None, - Some(values) => match self.values_str(&values) { + Some(ref values) => match self.values_str(values) { Some((expected, found)) => Some((expected, found)), None => { // Derived error. Cancel the emitter. @@ -563,6 +602,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { self.note_error_origin(diag, &origin); self.check_and_note_conflicting_crates(diag, terr, span); + self.note_possible_function_call(diag, values, span); self.tcx.note_and_explain_type_err(diag, terr, span); } diff --git a/src/test/compile-fail/issue-35241.rs b/src/test/compile-fail/issue-35241.rs new file mode 100644 index 0000000000000..4c3b6fabe4c11 --- /dev/null +++ b/src/test/compile-fail/issue-35241.rs @@ -0,0 +1,40 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum Foo { + Bar(u32), +} + +fn get_foo() -> Foo { + Foo::Bar + //~^ ERROR E0308 + //~| HELP `Foo::Bar` is a function - maybe try calling it? +} + +fn get_u32() -> u32 { + Foo::Bar + //~^ ERROR E0308 +} + +fn abort_2() { + abort + //~^ ERROR E0308 + //~| HELP `abort` is a function - maybe try calling it? +} + +fn abort() -> ! { panic!() } + +fn call(f: fn() -> u32) -> u32 { + f + //~^ ERROR E0308 + //~| HELP found a function - maybe try calling it? +} + +fn main() {} \ No newline at end of file