From 61daee66a89f52eb5fa6f103d5ac8dbcaa885709 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 26 Feb 2024 19:52:52 +0000 Subject: [PATCH] Get rid of some sub_exp and eq_exp --- compiler/rustc_borrowck/src/type_check/mod.rs | 1 - compiler/rustc_hir_typeck/src/coercion.rs | 38 +++++++++---------- compiler/rustc_infer/src/infer/mod.rs | 6 ++- .../rustc_infer/src/infer/opaque_types.rs | 12 ++---- .../src/solve/eval_ctxt/mod.rs | 1 - .../src/traits/engine.rs | 18 --------- .../error_reporting/type_err_ctxt_ext.rs | 13 +++++-- .../opaque-type-unsatisfied-bound.rs | 6 +-- .../opaque-type-unsatisfied-bound.stderr | 6 +-- .../opaque-type-unsatisfied-fn-bound.rs | 2 +- .../opaque-type-unsatisfied-fn-bound.stderr | 2 +- .../itiat-allow-nested-closures.bad.stderr | 3 ++ 12 files changed, 48 insertions(+), 60 deletions(-) diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 75cc28bcab0..26e1e24d1a1 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1066,7 +1066,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &cause, param_env, hidden_ty.ty, - true, &mut obligations, )?; diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 179255993b4..792359c9dda 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1493,6 +1493,21 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { return; } + let (expected, found) = if label_expression_as_expected { + // In the case where this is a "forced unit", like + // `break`, we want to call the `()` "expected" + // since it is implied by the syntax. + // (Note: not all force-units work this way.)" + (expression_ty, self.merged_ty()) + } else { + // Otherwise, the "expected" type for error + // reporting is the current unification type, + // which is basically the LUB of the expressions + // we've seen so far (combined with the expected + // type) + (self.merged_ty(), expression_ty) + }; + // Handle the actual type unification etc. let result = if let Some(expression) = expression { if self.pushed == 0 { @@ -1540,12 +1555,11 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // Another example is `break` with no argument expression. assert!(expression_ty.is_unit(), "if let hack without unit type"); fcx.at(cause, fcx.param_env) - // needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs - .eq_exp( + .eq( + // needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs DefineOpaqueTypes::Yes, - label_expression_as_expected, - expression_ty, - self.merged_ty(), + expected, + found, ) .map(|infer_ok| { fcx.register_infer_ok_obligations(infer_ok); @@ -1579,20 +1593,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { fcx.set_tainted_by_errors( fcx.dcx().span_delayed_bug(cause.span, "coercion error but no error emitted"), ); - let (expected, found) = if label_expression_as_expected { - // In the case where this is a "forced unit", like - // `break`, we want to call the `()` "expected" - // since it is implied by the syntax. - // (Note: not all force-units work this way.)" - (expression_ty, self.merged_ty()) - } else { - // Otherwise, the "expected" type for error - // reporting is the current unification type, - // which is basically the LUB of the expressions - // we've seen so far (combined with the expected - // type) - (self.merged_ty(), expression_ty) - }; let (expected, found) = fcx.resolve_vars_if_possible((expected, found)); let mut err; diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index b99ea35c22c..73a25637e1a 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1032,7 +1032,11 @@ impl<'tcx> InferCtxt<'tcx> { } self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| { - Ok(self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b)) + if a_is_expected { + Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::No, a, b)) + } else { + Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::No, b, a)) + } }) } diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index ec674407e52..d381c77ec66 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -102,7 +102,7 @@ impl<'tcx> InferCtxt<'tcx> { return Ok(InferOk { value: (), obligations: vec![] }); } let (a, b) = if a_is_expected { (a, b) } else { (b, a) }; - let process = |a: Ty<'tcx>, b: Ty<'tcx>, a_is_expected| match *a.kind() { + let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() { ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) if def_id.is_local() => { let def_id = def_id.expect_local(); match self.defining_use_anchor { @@ -169,14 +169,13 @@ impl<'tcx> InferCtxt<'tcx> { cause.clone(), param_env, b, - a_is_expected, )) } _ => None, }; - if let Some(res) = process(a, b, true) { + if let Some(res) = process(a, b) { res - } else if let Some(res) = process(b, a, false) { + } else if let Some(res) = process(b, a) { res } else { let (a, b) = self.resolve_vars_if_possible((a, b)); @@ -520,7 +519,6 @@ impl<'tcx> InferCtxt<'tcx> { cause: ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, hidden_ty: Ty<'tcx>, - a_is_expected: bool, ) -> InferResult<'tcx, ()> { let mut obligations = Vec::new(); @@ -529,7 +527,6 @@ impl<'tcx> InferCtxt<'tcx> { &cause, param_env, hidden_ty, - a_is_expected, &mut obligations, )?; @@ -558,7 +555,6 @@ impl<'tcx> InferCtxt<'tcx> { cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, hidden_ty: Ty<'tcx>, - a_is_expected: bool, obligations: &mut Vec>, ) -> Result<(), TypeError<'tcx>> { // Ideally, we'd get the span where *this specific `ty` came @@ -586,7 +582,7 @@ impl<'tcx> InferCtxt<'tcx> { if let Some(prev) = prev { obligations.extend( self.at(cause, param_env) - .eq_exp(DefineOpaqueTypes::Yes, a_is_expected, prev, hidden_ty)? + .eq(DefineOpaqueTypes::Yes, prev, hidden_ty)? .obligations, ); } diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs index df4dcaff1e7..4a86f708632 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs @@ -904,7 +904,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { &ObligationCause::dummy(), param_env, hidden_ty, - true, &mut obligations, )?; self.add_goals(GoalSource::Misc, obligations.into_iter().map(|o| o.into())); diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index e789e9c2b6e..9fbec174ce8 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -116,24 +116,6 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> { self.infcx.at(cause, param_env).deeply_normalize(value, &mut **self.engine.borrow_mut()) } - /// Makes `expected <: actual`. - pub fn eq_exp( - &self, - cause: &ObligationCause<'tcx>, - param_env: ty::ParamEnv<'tcx>, - a_is_expected: bool, - a: T, - b: T, - ) -> Result<(), TypeError<'tcx>> - where - T: ToTrace<'tcx>, - { - self.infcx - .at(cause, param_env) - .eq_exp(DefineOpaqueTypes::Yes, a_is_expected, a, b) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) - } - pub fn eq>( &self, cause: &ObligationCause<'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 3275a4f3527..fa8edd11594 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -1528,6 +1528,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { | ObligationCauseCode::Coercion { .. } ); + let (expected, actual) = if is_normalized_term_expected { + (normalized_term, data.term) + } else { + (data.term, normalized_term) + }; + // constrain inference variables a bit more to nested obligations from normalize so // we can have more helpful errors. // @@ -1535,12 +1541,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // since the normalization is just done to improve the error message. let _ = ocx.select_where_possible(); - if let Err(new_err) = ocx.eq_exp( + if let Err(new_err) = ocx.eq( &obligation.cause, obligation.param_env, - is_normalized_term_expected, - normalized_term, - data.term, + expected, + actual, ) { (Some((data, is_normalized_term_expected, normalized_term, data.term)), new_err) } else { diff --git a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.rs b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.rs index 05b167326d4..2607f047024 100644 --- a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.rs +++ b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.rs @@ -13,8 +13,8 @@ fn main() { } fn weird0() -> impl Sized + !Sized {} -//~^ ERROR type mismatch resolving `() == impl !Sized + Sized` +//~^ ERROR type mismatch resolving `impl !Sized + Sized == ()` fn weird1() -> impl !Sized + Sized {} -//~^ ERROR type mismatch resolving `() == impl !Sized + Sized` +//~^ ERROR type mismatch resolving `impl !Sized + Sized == ()` fn weird2() -> impl !Sized {} -//~^ ERROR type mismatch resolving `() == impl !Sized` +//~^ ERROR type mismatch resolving `impl !Sized == ()` diff --git a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.stderr b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.stderr index d803e56e817..ceaf42431fe 100644 --- a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.stderr +++ b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-bound.stderr @@ -1,16 +1,16 @@ -error[E0271]: type mismatch resolving `() == impl !Sized + Sized` +error[E0271]: type mismatch resolving `impl !Sized + Sized == ()` --> $DIR/opaque-type-unsatisfied-bound.rs:15:16 | LL | fn weird0() -> impl Sized + !Sized {} | ^^^^^^^^^^^^^^^^^^^ types differ -error[E0271]: type mismatch resolving `() == impl !Sized + Sized` +error[E0271]: type mismatch resolving `impl !Sized + Sized == ()` --> $DIR/opaque-type-unsatisfied-bound.rs:17:16 | LL | fn weird1() -> impl !Sized + Sized {} | ^^^^^^^^^^^^^^^^^^^ types differ -error[E0271]: type mismatch resolving `() == impl !Sized` +error[E0271]: type mismatch resolving `impl !Sized == ()` --> $DIR/opaque-type-unsatisfied-bound.rs:19:16 | LL | fn weird2() -> impl !Sized {} diff --git a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.rs b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.rs index d714d781c88..9951826a846 100644 --- a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.rs +++ b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.rs @@ -3,6 +3,6 @@ #![feature(negative_bounds, unboxed_closures)] fn produce() -> impl !Fn<(u32,)> {} -//~^ ERROR type mismatch resolving `() == impl !Fn<(u32,)>` +//~^ ERROR type mismatch resolving `impl !Fn<(u32,)> == ()` fn main() {} diff --git a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.stderr b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.stderr index 1fd30410b00..e1b84e0df7a 100644 --- a/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.stderr +++ b/tests/ui/traits/negative-bounds/opaque-type-unsatisfied-fn-bound.stderr @@ -1,4 +1,4 @@ -error[E0271]: type mismatch resolving `() == impl !Fn<(u32,)>` +error[E0271]: type mismatch resolving `impl !Fn<(u32,)> == ()` --> $DIR/opaque-type-unsatisfied-fn-bound.rs:5:17 | LL | fn produce() -> impl !Fn<(u32,)> {} diff --git a/tests/ui/type-alias-impl-trait/itiat-allow-nested-closures.bad.stderr b/tests/ui/type-alias-impl-trait/itiat-allow-nested-closures.bad.stderr index 4acc47eaef2..9d38e8f36b1 100644 --- a/tests/ui/type-alias-impl-trait/itiat-allow-nested-closures.bad.stderr +++ b/tests/ui/type-alias-impl-trait/itiat-allow-nested-closures.bad.stderr @@ -8,6 +8,9 @@ LL | let _: i32 = closure(); | --- ^^^^^^^^^ expected `i32`, found opaque type | | | expected due to this + | + = note: expected type `i32` + found opaque type `<() as Foo>::Assoc` error[E0308]: mismatched types --> $DIR/itiat-allow-nested-closures.rs:22:9