From fa5cf90b8dd335b53056e3af96a468905b482226 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 24 Oct 2022 18:45:50 +0000 Subject: [PATCH] Delay span bug when we can't map lifetimes back in collect_trait_impl_trait_tys --- .../src/check/compare_method.rs | 12 +++++++++-- .../impl-trait/in-trait/signature-mismatch.rs | 21 +++++++++++++++++++ .../in-trait/signature-mismatch.stderr | 16 ++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/impl-trait/in-trait/signature-mismatch.rs create mode 100644 src/test/ui/impl-trait/in-trait/signature-mismatch.stderr diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index 3469ec4767b7c..e72f18012ab33 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -598,8 +598,16 @@ pub fn collect_trait_impl_trait_tys<'tcx>( let num_impl_substs = tcx.generics_of(impl_m.container_id(tcx)).params.len(); let ty = tcx.fold_regions(ty, |region, _| { let ty::ReFree(_) = region.kind() else { return region; }; - let ty::ReEarlyBound(e) = map[®ion.into()].expect_region().kind() - else { bug!("expected ReFree to map to ReEarlyBound"); }; + let Some(ty::ReEarlyBound(e)) = map.get(®ion.into()).map(|r| r.expect_region().kind()) + else { + tcx + .sess + .delay_span_bug( + return_span, + "expected ReFree to map to ReEarlyBound" + ); + return tcx.lifetimes.re_static; + }; tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion { def_id: e.def_id, name: e.name, diff --git a/src/test/ui/impl-trait/in-trait/signature-mismatch.rs b/src/test/ui/impl-trait/in-trait/signature-mismatch.rs new file mode 100644 index 0000000000000..90682631aa032 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/signature-mismatch.rs @@ -0,0 +1,21 @@ +// edition:2021 + +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +pub trait AsyncTrait { + fn async_fn(&self, buff: &[u8]) -> impl Future>; +} + +pub struct Struct; + +impl AsyncTrait for Struct { + fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future> + 'a { + //~^ ERROR `impl` item signature doesn't match `trait` item signature + async move { buff.to_vec() } + } +} + +fn main() {} diff --git a/src/test/ui/impl-trait/in-trait/signature-mismatch.stderr b/src/test/ui/impl-trait/in-trait/signature-mismatch.stderr new file mode 100644 index 0000000000000..6663d7faa1e57 --- /dev/null +++ b/src/test/ui/impl-trait/in-trait/signature-mismatch.stderr @@ -0,0 +1,16 @@ +error: `impl` item signature doesn't match `trait` item signature + --> $DIR/signature-mismatch.rs:15:5 + | +LL | fn async_fn(&self, buff: &[u8]) -> impl Future>; + | ----------------------------------------------------------------- expected `fn(&'1 Struct, &'2 [u8]) -> impl Future> + 'static` +... +LL | fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future> + 'a { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Struct, &'2 [u8]) -> impl Future> + '2` + | + = note: expected `fn(&'1 Struct, &'2 [u8]) -> impl Future> + 'static` + found `fn(&'1 Struct, &'2 [u8]) -> impl Future> + '2` + = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` + = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output + +error: aborting due to previous error +