From 3e7e1b1f838b86701e5539d788b633da1f0f950a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 00:10:35 +0000 Subject: [PATCH 1/3] Avoid InferCtxt::build in suggest_missing_break_or_return_expr --- .../src/fn_ctxt/suggestions.rs | 16 +++++--------- .../return/tail-expr-as-potential-return.rs | 17 ++++++++++++++- .../tail-expr-as-potential-return.stderr | 21 ++++++++++++++++--- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index b9a8d16311c93..1daca464b4772 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -10,7 +10,7 @@ use rustc_hir::{ Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate, }; use rustc_hir_analysis::astconv::AstConv; -use rustc_infer::infer::{self, TyCtxtInferExt}; +use rustc_infer::infer; use rustc_infer::traits::{self, StatementAsExpression}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty}; @@ -921,19 +921,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ty = >::ast_ty_to_ty(self, ty); 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.normalize(expr.span, ty); let ty = match self.tcx.asyncness(fn_id.owner) { - hir::IsAsync::Async => { - let infcx = self.tcx.infer_ctxt().build(); - 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::Async => self.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, }; + let ty = self.normalize(expr.span, ty); if self.can_coerce(found, ty) { err.multipart_suggestion( "you might have meant to return this value", diff --git a/src/test/ui/return/tail-expr-as-potential-return.rs b/src/test/ui/return/tail-expr-as-potential-return.rs index 2c3610fb24d4e..f46e088b85f72 100644 --- a/src/test/ui/return/tail-expr-as-potential-return.rs +++ b/src/test/ui/return/tail-expr-as-potential-return.rs @@ -12,7 +12,6 @@ // edition:2018 fn main() { - let _ = foo(true); } fn foo(x: bool) -> Result { @@ -30,3 +29,19 @@ async fn bar(x: bool) -> Result { } Ok(42.0) } + +trait Identity { + type Out; +} + +impl 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 +} diff --git a/src/test/ui/return/tail-expr-as-potential-return.stderr b/src/test/ui/return/tail-expr-as-potential-return.stderr index dec1cbc4624ef..9183b4599ba6d 100644 --- a/src/test/ui/return/tail-expr-as-potential-return.stderr +++ b/src/test/ui/return/tail-expr-as-potential-return.stderr @@ -1,5 +1,5 @@ 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 | | Err(42) @@ -16,7 +16,22 @@ LL | return Err(42); | ++++++ + 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 | | Err(42) @@ -32,6 +47,6 @@ help: you might have meant to return this value 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`. From e3072a24ada7a90880a288d4edd10db481d7b807 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 00:18:16 +0000 Subject: [PATCH 2/3] Avoid InferCtxt::build in report_similar_impl_candidates --- .../src/traits/error_reporting/mod.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 049b24b39975d..888f0f7c5b296 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -9,7 +9,7 @@ use super::{ }; use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode}; use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; -use crate::infer::{self, InferCtxt, TyCtxtInferExt}; +use crate::infer::{self, InferCtxt}; use crate::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::traits::query::normalize::QueryNormalizeExt as _; use crate::traits::specialize::to_pretty_impl_header; @@ -1930,14 +1930,6 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { return report(normalized_impl_candidates, err); } - let normalize = |candidate| { - let infcx = self.tcx.infer_ctxt().build(); - infcx - .at(&ObligationCause::dummy(), ty::ParamEnv::empty()) - .query_normalize(candidate) - .map_or(candidate, |normalized| normalized.value) - }; - // Sort impl candidates so that ordering is consistent for UI tests. // because the ordering of `impl_candidates` may not be deterministic: // https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507 @@ -1947,7 +1939,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let mut normalized_impl_candidates_and_similarities = impl_candidates .into_iter() .map(|ImplCandidate { trait_ref, similarity }| { - let normalized = normalize(trait_ref); + // FIXME(compiler-errors): This should be using `NormalizeExt::normalize` + let normalized = self + .at(&ObligationCause::dummy(), ty::ParamEnv::empty()) + .query_normalize(trait_ref) + .map_or(trait_ref, |normalized| normalized.value); (similarity, normalized) }) .collect::>(); From a68eae2f70ce58fbf3bc614f19b4570d2c28d54c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 00:23:28 +0000 Subject: [PATCH 3/3] Avoid InferCtxt::build in generic_arg_mismatch_err --- compiler/rustc_hir_analysis/src/astconv/generics.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/generics.rs b/compiler/rustc_hir_analysis/src/astconv/generics.rs index 47915b4bd4e6c..f64d65cc6ad7a 100644 --- a/compiler/rustc_hir_analysis/src/astconv/generics.rs +++ b/compiler/rustc_hir_analysis/src/astconv/generics.rs @@ -11,7 +11,6 @@ use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::GenericArg; -use rustc_infer::infer::TyCtxtInferExt; use rustc_middle::ty::{ self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt, }; @@ -83,9 +82,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { Res::Def(DefKind::TyParam, src_def_id) => { if let Some(param_local_id) = param.def_id.as_local() { let param_name = tcx.hir().ty_param_name(param_local_id); - let infcx = tcx.infer_ctxt().build(); - let param_type = - infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id)); + let param_type = tcx.type_of(param.def_id); if param_type.is_suggestable(tcx, false) { err.span_suggestion( tcx.def_span(src_def_id),