From 95a045835882e0f9590115ad992b650b71e8197c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 9 Dec 2017 13:04:27 -0800 Subject: [PATCH] Resolve type on return type suggestion --- src/librustc_errors/emitter.rs | 2 +- src/librustc_typeck/check/mod.rs | 3 ++- src/test/ui/codemap_tests/tab.stderr | 2 +- ...issue-43106-gating-of-builtin-attrs.stderr | 2 +- src/test/ui/lint/suggestions.stderr | 4 ++-- .../ui/mismatched_types/issue-19109.stderr | 2 +- src/test/ui/suggestions/return-type.rs | 24 +++++++++++++++++++ src/test/ui/suggestions/return-type.stderr | 19 +++++++++++++++ 8 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/suggestions/return-type.rs create mode 100644 src/test/ui/suggestions/return-type.stderr diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 16bbd755b8869..439ded40ec1c7 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -46,7 +46,7 @@ impl Emitter for EmitterWriter { sugg.msg.split_whitespace().count() < 10 && // don't display multiline suggestions as labels !sugg.substitutions[0].parts[0].snippet.contains('\n') { - let substitution = &sugg.substitutions[0].parts[0].snippet; + let substitution = &sugg.substitutions[0].parts[0].snippet.trim(); let msg = if substitution.len() == 0 || !sugg.show_code_when_inline { // This substitution is only removal or we explicitly don't want to show the // code inline, don't show it diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index efcf498b72c14..be595c7126496 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4503,7 +4503,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { (&hir::FunctionRetTy::DefaultReturn(span), true, true) => { err.span_suggestion(span, "try adding a return type", - format!("-> {} ", found)); + format!("-> {} ", + self.resolve_type_vars_with_obligations(found))); } (&hir::FunctionRetTy::DefaultReturn(span), false, true) => { err.span_label(span, "possibly return type missing here?"); diff --git a/src/test/ui/codemap_tests/tab.stderr b/src/test/ui/codemap_tests/tab.stderr index e95078f25474a..c887821c6d11a 100644 --- a/src/test/ui/codemap_tests/tab.stderr +++ b/src/test/ui/codemap_tests/tab.stderr @@ -8,7 +8,7 @@ error[E0308]: mismatched types --> $DIR/tab.rs:18:2 | 17 | fn foo() { - | - help: try adding a return type: `-> &'static str ` + | - help: try adding a return type: `-> &'static str` 18 | "bar boo" //~ ERROR mismatched types | ^^^^^^^^^^^^^^^^^^^^ expected (), found reference | diff --git a/src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr b/src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr index 0e72442143b07..be340b1ab9aee 100644 --- a/src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr +++ b/src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr @@ -548,7 +548,7 @@ warning: function is marked #[no_mangle], but not exported 426 | #[no_mangle = "3500"] fn f() { } | -^^^^^^^^^ | | - | help: try making it public: `pub ` + | help: try making it public: `pub` | = note: #[warn(private_no_mangle_fns)] on by default diff --git a/src/test/ui/lint/suggestions.stderr b/src/test/ui/lint/suggestions.stderr index 7b84cc1f4b490..44cfb1a82a244 100644 --- a/src/test/ui/lint/suggestions.stderr +++ b/src/test/ui/lint/suggestions.stderr @@ -38,7 +38,7 @@ warning: static is marked #[no_mangle], but not exported 14 | #[no_mangle] static SHENZHOU: usize = 1; // should suggest `pub` | -^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | help: try making it public: `pub ` + | help: try making it public: `pub` | = note: #[warn(private_no_mangle_statics)] on by default @@ -68,7 +68,7 @@ warning: function is marked #[no_mangle], but not exported 24 | fn rio_grande() {} // should suggest `pub` | -^^^^^^^^^^^^^^^^^ | | - | help: try making it public: `pub ` + | help: try making it public: `pub` | = note: #[warn(private_no_mangle_fns)] on by default diff --git a/src/test/ui/mismatched_types/issue-19109.stderr b/src/test/ui/mismatched_types/issue-19109.stderr index 714d1b397334f..2942619f64936 100644 --- a/src/test/ui/mismatched_types/issue-19109.stderr +++ b/src/test/ui/mismatched_types/issue-19109.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/issue-19109.rs:14:5 | 13 | fn function(t: &mut Trait) { - | - help: try adding a return type: `-> *mut Trait ` + | - help: try adding a return type: `-> *mut Trait` 14 | t as *mut Trait | ^^^^^^^^^^^^^^^ expected (), found *-ptr | diff --git a/src/test/ui/suggestions/return-type.rs b/src/test/ui/suggestions/return-type.rs new file mode 100644 index 0000000000000..e63787949a41b --- /dev/null +++ b/src/test/ui/suggestions/return-type.rs @@ -0,0 +1,24 @@ +// Copyright 2017 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. + +struct S { + t: T, +} + +fn foo(x: T) -> S { + S { t: x } +} + +fn bar() { + foo(4 as usize) + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/src/test/ui/suggestions/return-type.stderr b/src/test/ui/suggestions/return-type.stderr new file mode 100644 index 0000000000000..19c5d72dd7b36 --- /dev/null +++ b/src/test/ui/suggestions/return-type.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/return-type.rs:20:5 + | +20 | foo(4 as usize) + | ^^^^^^^^^^^^^^^ expected (), found struct `S` + | + = note: expected type `()` + found type `S` +help: try adding a semicolon + | +20 | foo(4 as usize); + | ^ +help: try adding a return type + | +19 | fn bar() -> S { + | ^^^^^^^^^^^ + +error: aborting due to previous error +