diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2020-01-24 11:47:54 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2020-01-24 11:48:17 -0800 |
| commit | d493dccef7ae1d2ca739fe828bf9556b44dc460a (patch) | |
| tree | 350d4814d8a6c05ddca9661f3a1097043be41705 /src | |
| parent | 34d51b33787c9b2f59cb3283c8b57a290ab86437 (diff) | |
| download | rust-d493dccef7ae1d2ca739fe828bf9556b44dc460a.tar.gz rust-d493dccef7ae1d2ca739fe828bf9556b44dc460a.zip | |
Apply `resolve_vars_if_possible` to returned types for more accurate suggestions
Diffstat (limited to 'src')
3 files changed, 12 insertions, 8 deletions
diff --git a/src/librustc/traits/error_reporting/suggestions.rs b/src/librustc/traits/error_reporting/suggestions.rs index 84f12603855..cc2c97096e9 100644 --- a/src/librustc/traits/error_reporting/suggestions.rs +++ b/src/librustc/traits/error_reporting/suggestions.rs @@ -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) }, diff --git a/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs b/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs index dfcc22aee34..08bab5734fd 100644 --- a/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs +++ b/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs @@ -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 } } diff --git a/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr b/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr index fbad7ec124c..664a897c593 100644 --- a/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr +++ b/src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr @@ -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 { | ^^^^^^^^^^ |
