From bb502c488915253a261f837cce7a920bf49a1666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 8 Apr 2021 15:54:26 -0700 Subject: [PATCH] Provide verbose suggestion for new output type --- .../rustc_typeck/src/check/compare_method.rs | 16 +++++++--------- src/test/ui/compare-method/bad-self-type.stderr | 9 +++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index 9fd3a170ece..60ca562f992 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -338,20 +338,18 @@ fn compare_predicate_entailment<'tcx>( ImplItemKind::Fn(ref sig, _) if sig.header.asyncness == hir::IsAsync::NotAsync => { - let (span, sugg) = match sig.decl.output { + let msg = "change the output type to match the trait"; + let ap = Applicability::MachineApplicable; + match sig.decl.output { hir::FnRetTy::DefaultReturn(sp) => { - (sp, format!(" -> {} ", trait_sig.output())) + let sugg = format!("-> {} ", trait_sig.output()); + diag.span_suggestion_verbose(sp, msg, sugg, ap); } hir::FnRetTy::Return(hir_ty) => { - (hir_ty.span, trait_sig.output().to_string()) + let sugg = trait_sig.output().to_string(); + diag.span_suggestion(hir_ty.span, msg, sugg, ap); } }; - diag.span_suggestion( - span, - "change the output type to match the trait", - sugg, - Applicability::MachineApplicable, - ); } _ => {} }; diff --git a/src/test/ui/compare-method/bad-self-type.stderr b/src/test/ui/compare-method/bad-self-type.stderr index 74e7c562aa7..76f91fbf241 100644 --- a/src/test/ui/compare-method/bad-self-type.stderr +++ b/src/test/ui/compare-method/bad-self-type.stderr @@ -32,13 +32,14 @@ LL | fn bar(self) -> Option<()>; | ---------- type in trait ... LL | fn bar(self) {} - | ^ - | | - | expected enum `Option`, found `()` - | help: change the output type to match the trait: `-> Option<()>` + | ^ expected enum `Option`, found `()` | = note: expected fn pointer `fn(MyFuture) -> Option<()>` found fn pointer `fn(MyFuture)` +help: change the output type to match the trait + | +LL | fn bar(self) -> Option<()> {} + | ^^^^^^^^^^^^^ error: aborting due to 3 previous errors