From c47bb6debdea0ad69287328e5d578138fe14b32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 18 Apr 2020 20:51:11 -0700 Subject: [PATCH 1/2] On `FnDef` type annotation suggestion, use fn-pointer output Partly addresses #71209. --- .../infer/error_reporting/need_type_info.rs | 8 +++++++- .../fn-needing-specified-return-type-param.rs | 5 +++++ .../fn-needing-specified-return-type-param.stderr | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/fn-needing-specified-return-type-param.rs create mode 100644 src/test/ui/suggestions/fn-needing-specified-return-type-param.stderr diff --git a/src/librustc_infer/infer/error_reporting/need_type_info.rs b/src/librustc_infer/infer/error_reporting/need_type_info.rs index bb6e5700ccad4..2b4aeb2138240 100644 --- a/src/librustc_infer/infer/error_reporting/need_type_info.rs +++ b/src/librustc_infer/infer/error_reporting/need_type_info.rs @@ -247,7 +247,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { None }; printer.name_resolver = Some(Box::new(&getter)); - let _ = ty.print(printer); + let _ = if let ty::FnDef(..) = ty.kind { + // We don't want the regular output for `fn`s because it inscludes its path in + // invalid pseduo-syntax, we want the `fn`-pointer output instead. + ty.fn_sig(self.tcx).print(printer) + } else { + ty.print(printer) + }; s }; diff --git a/src/test/ui/suggestions/fn-needing-specified-return-type-param.rs b/src/test/ui/suggestions/fn-needing-specified-return-type-param.rs new file mode 100644 index 0000000000000..2f140f823afb9 --- /dev/null +++ b/src/test/ui/suggestions/fn-needing-specified-return-type-param.rs @@ -0,0 +1,5 @@ +fn f() -> A { unimplemented!() } +fn foo() { + let _ = f; //~ ERROR type annotations needed for `fn() -> A` +} +fn main() {} diff --git a/src/test/ui/suggestions/fn-needing-specified-return-type-param.stderr b/src/test/ui/suggestions/fn-needing-specified-return-type-param.stderr new file mode 100644 index 0000000000000..b59a3818e7042 --- /dev/null +++ b/src/test/ui/suggestions/fn-needing-specified-return-type-param.stderr @@ -0,0 +1,11 @@ +error[E0282]: type annotations needed for `fn() -> A` + --> $DIR/fn-needing-specified-return-type-param.rs:3:13 + | +LL | let _ = f; + | - ^ cannot infer type for type parameter `A` declared on the function `f` + | | + | consider giving this pattern the explicit type `fn() -> A`, where the type parameter `A` is specified + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. From 432ab343f8c75e657d25713addc2ee0cd5110542 Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Mon, 27 Apr 2020 10:59:44 -0700 Subject: [PATCH 2/2] fix typo Co-Authored-By: varkor --- src/librustc_infer/infer/error_reporting/need_type_info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_infer/infer/error_reporting/need_type_info.rs b/src/librustc_infer/infer/error_reporting/need_type_info.rs index 2b4aeb2138240..2ec16408465ef 100644 --- a/src/librustc_infer/infer/error_reporting/need_type_info.rs +++ b/src/librustc_infer/infer/error_reporting/need_type_info.rs @@ -248,7 +248,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { }; printer.name_resolver = Some(Box::new(&getter)); let _ = if let ty::FnDef(..) = ty.kind { - // We don't want the regular output for `fn`s because it inscludes its path in + // We don't want the regular output for `fn`s because it includes its path in // invalid pseduo-syntax, we want the `fn`-pointer output instead. ty.fn_sig(self.tcx).print(printer) } else {