Skip to content

Commit bd7ee07

Browse files
Check lifetime param count in collect_trait_impl_trait_tys
1 parent 56c241c commit bd7ee07

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

compiler/rustc_hir_analysis/src/check/compare_method.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,11 @@ fn compare_predicate_entailment<'tcx>(
173173
impl_to_placeholder_substs.rebase_onto(tcx, impl_m.container_id(tcx), trait_to_impl_substs);
174174
debug!("compare_impl_method: trait_to_placeholder_substs={:?}", trait_to_placeholder_substs);
175175

176-
let impl_m_generics = tcx.generics_of(impl_m.def_id);
177-
let trait_m_generics = tcx.generics_of(trait_m.def_id);
178176
let impl_m_predicates = tcx.predicates_of(impl_m.def_id);
179177
let trait_m_predicates = tcx.predicates_of(trait_m.def_id);
180178

181179
// Check region bounds.
182-
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, &trait_m_generics, &impl_m_generics)?;
180+
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, false)?;
183181

184182
// Create obligations for each predicate declared by the impl
185183
// definition in the context of the trait's parameter
@@ -338,6 +336,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
338336
// First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
339337
compare_number_of_generics(tcx, impl_m, trait_m, tcx.hir().span_if_local(impl_m.def_id), true)?;
340338
compare_generic_param_kinds(tcx, impl_m, trait_m, true)?;
339+
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, true)?;
341340

342341
let trait_to_impl_substs = impl_trait_ref.substs;
343342

@@ -722,12 +721,14 @@ fn check_region_bounds_on_impl_item<'tcx>(
722721
tcx: TyCtxt<'tcx>,
723722
impl_m: &ty::AssocItem,
724723
trait_m: &ty::AssocItem,
725-
trait_generics: &ty::Generics,
726-
impl_generics: &ty::Generics,
724+
delay: bool,
727725
) -> Result<(), ErrorGuaranteed> {
728-
let trait_params = trait_generics.own_counts().lifetimes;
726+
let impl_generics = tcx.generics_of(impl_m.def_id);
729727
let impl_params = impl_generics.own_counts().lifetimes;
730728

729+
let trait_generics = tcx.generics_of(trait_m.def_id);
730+
let trait_params = trait_generics.own_counts().lifetimes;
731+
731732
debug!(
732733
"check_region_bounds_on_impl_item: \
733734
trait_generics={:?} \
@@ -761,12 +762,16 @@ fn check_region_bounds_on_impl_item<'tcx>(
761762
None
762763
};
763764

764-
let reported = tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait {
765-
span,
766-
item_kind: assoc_item_kind_str(impl_m),
767-
ident: impl_m.ident(tcx),
768-
generics_span,
769-
});
765+
let reported = tcx
766+
.sess
767+
.create_err(LifetimesOrBoundsMismatchOnTrait {
768+
span,
769+
item_kind: assoc_item_kind_str(impl_m),
770+
ident: impl_m.ident(tcx),
771+
generics_span,
772+
})
773+
.emit_unless(delay);
774+
770775
return Err(reported);
771776
}
772777

@@ -1504,18 +1509,10 @@ fn compare_type_predicate_entailment<'tcx>(
15041509
let trait_to_impl_substs =
15051510
impl_substs.rebase_onto(tcx, impl_ty.container_id(tcx), impl_trait_ref.substs);
15061511

1507-
let impl_ty_generics = tcx.generics_of(impl_ty.def_id);
1508-
let trait_ty_generics = tcx.generics_of(trait_ty.def_id);
15091512
let impl_ty_predicates = tcx.predicates_of(impl_ty.def_id);
15101513
let trait_ty_predicates = tcx.predicates_of(trait_ty.def_id);
15111514

1512-
check_region_bounds_on_impl_item(
1513-
tcx,
1514-
impl_ty,
1515-
trait_ty,
1516-
&trait_ty_generics,
1517-
&impl_ty_generics,
1518-
)?;
1515+
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
15191516

15201517
let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_substs);
15211518

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// edition:2021
2+
3+
#![feature(async_fn_in_trait)]
4+
//~^ WARN the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
5+
6+
trait MyTrait {
7+
async fn foo<'a>(&self);
8+
async fn bar(&self);
9+
}
10+
11+
impl MyTrait for i32 {
12+
async fn foo(&self) {}
13+
//~^ ERROR lifetime parameters or bounds on method `foo` do not match the trait declaration
14+
15+
async fn bar(&self) {
16+
self.foo();
17+
}
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/lifetime-mismatch.rs:3:12
3+
|
4+
LL | #![feature(async_fn_in_trait)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0195]: lifetime parameters or bounds on method `foo` do not match the trait declaration
11+
--> $DIR/lifetime-mismatch.rs:12:17
12+
|
13+
LL | async fn foo<'a>(&self);
14+
| ---- lifetimes in impl do not match this method in trait
15+
...
16+
LL | async fn foo(&self) {}
17+
| ^ lifetimes do not match method in trait
18+
19+
error: aborting due to previous error; 1 warning emitted
20+
21+
For more information about this error, try `rustc --explain E0195`.

0 commit comments

Comments
 (0)