Skip to content

Commit

Permalink
Flip the order of binder instantiation for better diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 30, 2023
1 parent d567e4f commit 473c88d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
17 changes: 7 additions & 10 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ 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
--> $DIR/method-signature-matches.rs:53:28
|
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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Output = Vec<u8>>;
| - this lifetime was captured
...
LL | fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + '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<Output = Vec<u8>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: hidden type inferred to be `impl Future<Output = Vec<u8>> + '_`
= note: hidden type inferred to be `impl Future<Output = Vec<u8>> + 'a`

error: aborting due to previous error

7 changes: 2 additions & 5 deletions tests/ui/impl-trait/in-trait/signature-mismatch.next.stderr
Original file line number Diff line number Diff line change
@@ -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<Output = Vec<u8>>;
| - this lifetime was captured
...
LL | fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + '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<Output = Vec<u8>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: hidden type inferred to be `impl Future<Output = Vec<u8>> + '_`
= note: hidden type inferred to be `impl Future<Output = Vec<u8>> + 'a`

error: aborting due to previous error

0 comments on commit 473c88d

Please sign in to comment.