Avoid InferCtxt::build in suggest_missing_break_or_return_expr

This commit is contained in:
Michael Goulet 2022-11-26 00:10:35 +00:00
parent 56c241c862
commit 3e7e1b1f83
3 changed files with 39 additions and 15 deletions

View file

@ -10,7 +10,7 @@ use rustc_hir::{
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate, Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
}; };
use rustc_hir_analysis::astconv::AstConv; use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer::{self, TyCtxtInferExt}; use rustc_infer::infer;
use rustc_infer::traits::{self, StatementAsExpression}; use rustc_infer::traits::{self, StatementAsExpression};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty}; use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty};
@ -921,19 +921,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty); let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
let bound_vars = self.tcx.late_bound_vars(fn_id); let bound_vars = self.tcx.late_bound_vars(fn_id);
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars)); let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
let ty = self.normalize(expr.span, ty);
let ty = match self.tcx.asyncness(fn_id.owner) { let ty = match self.tcx.asyncness(fn_id.owner) {
hir::IsAsync::Async => { hir::IsAsync::Async => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
let infcx = self.tcx.infer_ctxt().build(); span_bug!(fn_decl.output.span(), "failed to get output type of async function")
infcx.get_impl_future_output_ty(ty).unwrap_or_else(|| { }),
span_bug!(
fn_decl.output.span(),
"failed to get output type of async function"
)
})
}
hir::IsAsync::NotAsync => ty, hir::IsAsync::NotAsync => ty,
}; };
let ty = self.normalize(expr.span, ty);
if self.can_coerce(found, ty) { if self.can_coerce(found, ty) {
err.multipart_suggestion( err.multipart_suggestion(
"you might have meant to return this value", "you might have meant to return this value",

View file

@ -12,7 +12,6 @@
// edition:2018 // edition:2018
fn main() { fn main() {
let _ = foo(true);
} }
fn foo(x: bool) -> Result<f64, i32> { fn foo(x: bool) -> Result<f64, i32> {
@ -30,3 +29,19 @@ async fn bar(x: bool) -> Result<f64, i32> {
} }
Ok(42.0) Ok(42.0)
} }
trait Identity {
type Out;
}
impl<T> Identity for T {
type Out = T;
}
async fn foo2() -> i32 {
if true {
1i32 //~ ERROR mismatched types
//| HELP you might have meant to return this value
}
0
}

View file

@ -1,5 +1,5 @@
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:28:9 --> $DIR/tail-expr-as-potential-return.rs:27:9
| |
LL | / if x { LL | / if x {
LL | | Err(42) LL | | Err(42)
@ -16,7 +16,22 @@ LL | return Err(42);
| ++++++ + | ++++++ +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:20:9 --> $DIR/tail-expr-as-potential-return.rs:43:9
|
LL | / if true {
LL | | 1i32
| | ^^^^ expected `()`, found `i32`
LL | | //| HELP you might have meant to return this value
LL | | }
| |_____- expected this to be `()`
|
help: you might have meant to return this value
|
LL | return 1i32;
| ++++++ +
error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:19:9
| |
LL | / if x { LL | / if x {
LL | | Err(42) LL | | Err(42)
@ -32,6 +47,6 @@ help: you might have meant to return this value
LL | return Err(42); LL | return Err(42);
| ++++++ + | ++++++ +
error: aborting due to 2 previous errors error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.