1
Fork 0

Clarify implicit captures for RPITIT

This commit is contained in:
Michael Goulet 2024-09-29 13:49:32 -04:00 committed by Jubilee Young
parent a7dc98733d
commit 36076ecdc7
4 changed files with 30 additions and 11 deletions

View file

@ -259,6 +259,9 @@ hir_analysis_late_bound_lifetime_in_apit = `impl Trait` can only mention lifetim
hir_analysis_late_bound_type_in_apit = `impl Trait` can only mention type parameters from an fn or impl hir_analysis_late_bound_type_in_apit = `impl Trait` can only mention type parameters from an fn or impl
.label = type parameter declared here .label = type parameter declared here
hir_analysis_lifetime_implicitly_captured = `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
.param_label = all lifetime parameters originating from a trait are captured implicitly
hir_analysis_lifetime_must_be_first = lifetime parameter `{$name}` must be listed before non-lifetime parameters hir_analysis_lifetime_must_be_first = lifetime parameter `{$name}` must be listed before non-lifetime parameters
.label = move the lifetime before this parameter .label = move the lifetime before this parameter

View file

@ -588,6 +588,12 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
use_span, use_span,
param_span: tcx.def_span(def_id), param_span: tcx.def_span(def_id),
}); });
} else {
if tcx.def_kind(tcx.parent(param.def_id)) == DefKind::Trait {
tcx.dcx().emit_err(errors::LifetimeImplicitlyCaptured {
opaque_span,
param_span: tcx.def_span(param.def_id),
});
} else { } else {
// If the `use_span` is actually just the param itself, then we must // If the `use_span` is actually just the param itself, then we must
// have not duplicated the lifetime but captured the original. // have not duplicated the lifetime but captured the original.
@ -599,6 +605,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
param_span: use_span, param_span: use_span,
}); });
} }
}
continue; continue;
} }
} }

View file

@ -34,6 +34,15 @@ pub(crate) struct LifetimeNotCaptured {
pub opaque_span: Span, pub opaque_span: Span,
} }
#[derive(Diagnostic)]
#[diag(hir_analysis_lifetime_implicitly_captured)]
pub(crate) struct LifetimeImplicitlyCaptured {
#[primary_span]
pub opaque_span: Span,
#[label(hir_analysis_param_label)]
pub param_span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(hir_analysis_bad_precise_capture)] #[diag(hir_analysis_bad_precise_capture)]
pub(crate) struct BadPreciseCapture { pub(crate) struct BadPreciseCapture {

View file

@ -2,9 +2,9 @@ error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use
--> $DIR/rpitit.rs:11:19 --> $DIR/rpitit.rs:11:19
| |
LL | trait TraitLt<'a: 'a> { LL | trait TraitLt<'a: 'a> {
| -- this lifetime parameter is captured | -- all lifetime parameters originating from a trait are captured implicitly
LL | fn hello() -> impl Sized + use<Self>; LL | fn hello() -> impl Sized + use<Self>;
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime captured due to being mentioned in the bounds of the `impl Trait` | ^^^^^^^^^^^^^^^^^^^^^^
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/rpitit.rs:15:5 --> $DIR/rpitit.rs:15:5