]> git.lizzy.rs Git - rust.git/commitdiff
Apply `resolve_vars_if_possible` to returned types for more accurate suggestions
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 24 Jan 2020 19:47:54 +0000 (11:47 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 24 Jan 2020 19:48:17 +0000 (11:48 -0800)
src/librustc/traits/error_reporting/suggestions.rs
src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs
src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr

index 84f1260385553f9f839f79d552556a7191408a6b..cc2c97096e966d092b0bf3e5f067f4a9e9652cca 100644 (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)
             },
index dfcc22aee34855d23bc8ae1e03d0fcc21e896f9d..08bab5734fd7a0a3c01d0d7bfb9969da8a2c20e7 100644 (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
     }
 }
 
index fbad7ec124cb9853cb6e6379686b09f7c4344734..664a897c593fc90f252dc951c3b50bcec23f4c7f 100644 (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 {
    |             ^^^^^^^^^^