Skip to content

Commit d493dcc

Browse files
committed
Apply resolve_vars_if_possible to returned types for more accurate suggestions
1 parent 34d51b3 commit d493dcc

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/librustc/traits/error_reporting/suggestions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
605605

606606
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
607607

608-
let mut ret_types =
609-
visitor.returns.iter().filter_map(|expr| tables.node_type_opt(expr.hir_id));
608+
let mut ret_types = visitor
609+
.returns
610+
.iter()
611+
.filter_map(|expr| tables.node_type_opt(expr.hir_id))
612+
.map(|ty| self.resolve_vars_if_possible(&ty));
610613
let (last_ty, all_returns_have_same_type) = ret_types.clone().fold(
611614
(None, true),
612615
|(last_ty, mut same): (std::option::Option<Ty<'_>>, bool), ty| {
616+
let ty = self.resolve_vars_if_possible(&ty);
613617
same &= last_ty.map_or(true, |last_ty| last_ty == ty) && ty.kind != ty::Error;
614618
(Some(ty), same)
615619
},

src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ fn baw() -> Box<dyn Trait> {
5959
// Suggest using `impl Trait`
6060
fn bat() -> dyn Trait { //~ ERROR E0746
6161
if true {
62-
return 0u32;
62+
return 0;
6363
}
64-
42u32
64+
42
6565
}
6666
fn bay() -> dyn Trait { //~ ERROR E0746
6767
if true {
68-
0u32
68+
0
6969
} else {
70-
42u32
70+
42
7171
}
7272
}
7373

src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ LL | fn bat() -> dyn Trait {
249249
| ^^^^^^^^^ doesn't have a size known at compile-time
250250
|
251251
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
252-
help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
252+
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
253253
|
254254
LL | fn bat() -> impl Trait {
255255
| ^^^^^^^^^^
@@ -261,7 +261,7 @@ LL | fn bay() -> dyn Trait {
261261
| ^^^^^^^^^ doesn't have a size known at compile-time
262262
|
263263
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
264-
help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
264+
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
265265
|
266266
LL | fn bay() -> impl Trait {
267267
| ^^^^^^^^^^

0 commit comments

Comments
 (0)