Skip to content

Commit 3e7e1b1

Browse files
Avoid InferCtxt::build in suggest_missing_break_or_return_expr
1 parent 56c241c commit 3e7e1b1

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{
1010
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
1111
};
1212
use rustc_hir_analysis::astconv::AstConv;
13-
use rustc_infer::infer::{self, TyCtxtInferExt};
13+
use rustc_infer::infer;
1414
use rustc_infer::traits::{self, StatementAsExpression};
1515
use rustc_middle::lint::in_external_macro;
1616
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty};
@@ -921,19 +921,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
921921
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
922922
let bound_vars = self.tcx.late_bound_vars(fn_id);
923923
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
924-
let ty = self.normalize(expr.span, ty);
925924
let ty = match self.tcx.asyncness(fn_id.owner) {
926-
hir::IsAsync::Async => {
927-
let infcx = self.tcx.infer_ctxt().build();
928-
infcx.get_impl_future_output_ty(ty).unwrap_or_else(|| {
929-
span_bug!(
930-
fn_decl.output.span(),
931-
"failed to get output type of async function"
932-
)
933-
})
934-
}
925+
hir::IsAsync::Async => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
926+
span_bug!(fn_decl.output.span(), "failed to get output type of async function")
927+
}),
935928
hir::IsAsync::NotAsync => ty,
936929
};
930+
let ty = self.normalize(expr.span, ty);
937931
if self.can_coerce(found, ty) {
938932
err.multipart_suggestion(
939933
"you might have meant to return this value",

src/test/ui/return/tail-expr-as-potential-return.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// edition:2018
1313

1414
fn main() {
15-
let _ = foo(true);
1615
}
1716

1817
fn foo(x: bool) -> Result<f64, i32> {
@@ -30,3 +29,19 @@ async fn bar(x: bool) -> Result<f64, i32> {
3029
}
3130
Ok(42.0)
3231
}
32+
33+
trait Identity {
34+
type Out;
35+
}
36+
37+
impl<T> Identity for T {
38+
type Out = T;
39+
}
40+
41+
async fn foo2() -> i32 {
42+
if true {
43+
1i32 //~ ERROR mismatched types
44+
//| HELP you might have meant to return this value
45+
}
46+
0
47+
}

src/test/ui/return/tail-expr-as-potential-return.stderr

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/tail-expr-as-potential-return.rs:28:9
2+
--> $DIR/tail-expr-as-potential-return.rs:27:9
33
|
44
LL | / if x {
55
LL | | Err(42)
@@ -16,7 +16,22 @@ LL | return Err(42);
1616
| ++++++ +
1717

1818
error[E0308]: mismatched types
19-
--> $DIR/tail-expr-as-potential-return.rs:20:9
19+
--> $DIR/tail-expr-as-potential-return.rs:43:9
20+
|
21+
LL | / if true {
22+
LL | | 1i32
23+
| | ^^^^ expected `()`, found `i32`
24+
LL | | //| HELP you might have meant to return this value
25+
LL | | }
26+
| |_____- expected this to be `()`
27+
|
28+
help: you might have meant to return this value
29+
|
30+
LL | return 1i32;
31+
| ++++++ +
32+
33+
error[E0308]: mismatched types
34+
--> $DIR/tail-expr-as-potential-return.rs:19:9
2035
|
2136
LL | / if x {
2237
LL | | Err(42)
@@ -32,6 +47,6 @@ help: you might have meant to return this value
3247
LL | return Err(42);
3348
| ++++++ +
3449

35-
error: aborting due to 2 previous errors
50+
error: aborting due to 3 previous errors
3651

3752
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)