Apply resolve_vars_if_possible to returned types for more accurate suggestions

This commit is contained in:
Esteban Küber 2020-01-24 11:47:54 -08:00
parent 34d51b3378
commit d493dccef7
3 changed files with 12 additions and 8 deletions

View File

@ -605,11 +605,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
let mut ret_types =
visitor.returns.iter().filter_map(|expr| tables.node_type_opt(expr.hir_id));
let mut ret_types = visitor
.returns
.iter()
.filter_map(|expr| tables.node_type_opt(expr.hir_id))
.map(|ty| self.resolve_vars_if_possible(&ty));
let (last_ty, all_returns_have_same_type) = ret_types.clone().fold(
(None, true),
|(last_ty, mut same): (std::option::Option<Ty<'_>>, bool), ty| {
let ty = self.resolve_vars_if_possible(&ty);
same &= last_ty.map_or(true, |last_ty| last_ty == ty) && ty.kind != ty::Error;
(Some(ty), same)
},

View File

@ -59,15 +59,15 @@ fn baw() -> Box<dyn Trait> {
// Suggest using `impl Trait`
fn bat() -> dyn Trait { //~ ERROR E0746
if true {
return 0u32;
return 0;
}
42u32
42
}
fn bay() -> dyn Trait { //~ ERROR E0746
if true {
0u32
0
} else {
42u32
42
}
}

View File

@ -249,7 +249,7 @@ LL | fn bat() -> dyn Trait {
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
|
LL | fn bat() -> impl Trait {
| ^^^^^^^^^^
@ -261,7 +261,7 @@ LL | fn bay() -> dyn Trait {
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
|
LL | fn bay() -> impl Trait {
| ^^^^^^^^^^