Skip to content

Commit b9341bf

Browse files
committed
Auto merge of rust-lang#104920 - compiler-errors:avoid-infcx-build, r=jackh726
Avoid some `InferCtxt::build` calls Either because we're inside of an `InferCtxt` already, or because we're not in a place where we'd ever see inference vars. r? types
2 parents d144956 + a68eae2 commit b9341bf

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed

compiler/rustc_hir_analysis/src/astconv/generics.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::DefId;
1313
use rustc_hir::GenericArg;
14-
use rustc_infer::infer::TyCtxtInferExt;
1514
use rustc_middle::ty::{
1615
self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
1716
};
@@ -83,9 +82,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8382
Res::Def(DefKind::TyParam, src_def_id) => {
8483
if let Some(param_local_id) = param.def_id.as_local() {
8584
let param_name = tcx.hir().ty_param_name(param_local_id);
86-
let infcx = tcx.infer_ctxt().build();
87-
let param_type =
88-
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id));
85+
let param_type = tcx.type_of(param.def_id);
8986
if param_type.is_suggestable(tcx, false) {
9087
err.span_suggestion(
9188
tcx.def_span(src_def_id),

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",

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
};
1010
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1111
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
12-
use crate::infer::{self, InferCtxt, TyCtxtInferExt};
12+
use crate::infer::{self, InferCtxt};
1313
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
1414
use crate::traits::query::normalize::QueryNormalizeExt as _;
1515
use crate::traits::specialize::to_pretty_impl_header;
@@ -1934,14 +1934,6 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19341934
return report(normalized_impl_candidates, err);
19351935
}
19361936

1937-
let normalize = |candidate| {
1938-
let infcx = self.tcx.infer_ctxt().build();
1939-
infcx
1940-
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
1941-
.query_normalize(candidate)
1942-
.map_or(candidate, |normalized| normalized.value)
1943-
};
1944-
19451937
// Sort impl candidates so that ordering is consistent for UI tests.
19461938
// because the ordering of `impl_candidates` may not be deterministic:
19471939
// https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507
@@ -1951,7 +1943,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19511943
let mut normalized_impl_candidates_and_similarities = impl_candidates
19521944
.into_iter()
19531945
.map(|ImplCandidate { trait_ref, similarity }| {
1954-
let normalized = normalize(trait_ref);
1946+
// FIXME(compiler-errors): This should be using `NormalizeExt::normalize`
1947+
let normalized = self
1948+
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
1949+
.query_normalize(trait_ref)
1950+
.map_or(trait_ref, |normalized| normalized.value);
19551951
(similarity, normalized)
19561952
})
19571953
.collect::<Vec<_>>();

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)