From 473c88dfb69f95b2e8c5f71ba7f6b7b448d22dc2 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 30 Jun 2023 00:27:03 +0000 Subject: [PATCH] Flip the order of binder instantiation for better diagnostics --- .../src/check/compare_impl_item.rs | 17 +++++++---------- .../in-trait/method-signature-matches.lt.stderr | 6 +++--- .../in-trait/signature-mismatch.current.stderr | 7 ++----- .../in-trait/signature-mismatch.next.stderr | 7 ++----- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 6873fa0bf27f3..76edeccaf29f5 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -651,11 +651,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( let impl_sig = ocx.normalize( &norm_cause, param_env, - infcx.instantiate_binder_with_fresh_vars( - return_span, - infer::HigherRankedType, - tcx.fn_sig(impl_m.def_id).subst_identity(), - ), + tcx.liberate_late_bound_regions(impl_m.def_id, tcx.fn_sig(impl_m.def_id).subst_identity()), ); impl_sig.error_reported()?; let impl_return_ty = impl_sig.output(); @@ -665,9 +661,10 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( // them with inference variables. // We will use these inference variables to collect the hidden types of RPITITs. let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_def_id); - let unnormalized_trait_sig = tcx - .liberate_late_bound_regions( - impl_m.def_id, + let unnormalized_trait_sig = infcx + .instantiate_binder_with_fresh_vars( + return_span, + infer::HigherRankedType, tcx.fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs), ) .fold_with(&mut collector); @@ -760,8 +757,8 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( let mut collected_tys = FxHashMap::default(); for (def_id, (ty, substs)) in collected_types { - match infcx.fully_resolve(ty) { - Ok(ty) => { + match infcx.fully_resolve((ty, substs)) { + Ok((ty, substs)) => { // `ty` contains free regions that we created earlier while liberating the // trait fn signature. However, projection normalization expects `ty` to // contains `def_id`'s early-bound regions. diff --git a/tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr b/tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr index f604ada6ac760..239c4b35c7207 100644 --- a/tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr +++ b/tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr @@ -5,7 +5,7 @@ LL | fn early<'late, T>(_: &'late ()) {} | - ^^^^^^^^^ | | | | | expected type parameter `T`, found `()` - | | help: change the parameter type to match the trait: `&'early T` + | | help: change the parameter type to match the trait: `&T` | this type parameter | note: type in trait @@ -13,8 +13,8 @@ note: type in trait | LL | fn early<'early, T>(x: &'early T) -> impl Sized; | ^^^^^^^^^ - = note: expected signature `fn(&'early T)` - found signature `fn(&())` + = note: expected signature `fn(&T)` + found signature `fn(&'late ())` error: aborting due to previous error diff --git a/tests/ui/impl-trait/in-trait/signature-mismatch.current.stderr b/tests/ui/impl-trait/in-trait/signature-mismatch.current.stderr index 2db7dd3421c89..0d805383da144 100644 --- a/tests/ui/impl-trait/in-trait/signature-mismatch.current.stderr +++ b/tests/ui/impl-trait/in-trait/signature-mismatch.current.stderr @@ -1,18 +1,15 @@ error: return type captures more lifetimes than trait definition --> $DIR/signature-mismatch.rs:17:47 | -LL | fn async_fn(&self, buff: &[u8]) -> impl Future>; - | - this lifetime was captured -... LL | fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future> + 'a { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | -- this lifetime was captured ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: hidden type must only reference lifetimes captured by this impl trait --> $DIR/signature-mismatch.rs:11:40 | LL | fn async_fn(&self, buff: &[u8]) -> impl Future>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: hidden type inferred to be `impl Future> + '_` + = note: hidden type inferred to be `impl Future> + 'a` error: aborting due to previous error diff --git a/tests/ui/impl-trait/in-trait/signature-mismatch.next.stderr b/tests/ui/impl-trait/in-trait/signature-mismatch.next.stderr index 2db7dd3421c89..0d805383da144 100644 --- a/tests/ui/impl-trait/in-trait/signature-mismatch.next.stderr +++ b/tests/ui/impl-trait/in-trait/signature-mismatch.next.stderr @@ -1,18 +1,15 @@ error: return type captures more lifetimes than trait definition --> $DIR/signature-mismatch.rs:17:47 | -LL | fn async_fn(&self, buff: &[u8]) -> impl Future>; - | - this lifetime was captured -... LL | fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future> + 'a { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | -- this lifetime was captured ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: hidden type must only reference lifetimes captured by this impl trait --> $DIR/signature-mismatch.rs:11:40 | LL | fn async_fn(&self, buff: &[u8]) -> impl Future>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: hidden type inferred to be `impl Future> + '_` + = note: hidden type inferred to be `impl Future> + 'a` error: aborting due to previous error