-
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
feat(Reborrow): Allow multiple lifetimes #163284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ use rustc_errors::ErrorGuaranteed; | |
| use rustc_hir as hir; | ||
| use rustc_hir::ItemKind; | ||
| use rustc_hir::def_id::{DefId, LocalDefId}; | ||
| use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, TyCtxtInferExt}; | ||
| use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin, TyCtxtInferExt}; | ||
| use rustc_infer::traits::{Obligation, TraitErrors}; | ||
| use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized}; | ||
| use rustc_span::Span; | ||
|
|
@@ -239,10 +239,8 @@ pub(super) fn coerce_shared_info<'tcx>( | |
| let source = tcx.type_of(impl_did).instantiate_identity().skip_norm_wip(); | ||
| let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity().skip_norm_wip(); | ||
|
|
||
| if trait_impl_lifetime_params_count(tcx, impl_did) != 1 { | ||
| return Err(tcx | ||
| .dcx() | ||
| .emit_err(diagnostics::CoerceSharedNotSingleLifetimeParam { span, trait_name })); | ||
| if trait_impl_lifetime_params_count(tcx, impl_did) == 0 { | ||
| return Err(tcx.dcx().emit_err(diagnostics::ReborrowNoLifetimes { span, trait_name })); | ||
| } | ||
|
|
||
| assert_eq!(trait_ref.def_id, coerce_shared_trait); | ||
|
|
@@ -266,25 +264,6 @@ pub(super) fn coerce_shared_info<'tcx>( | |
| (&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b)) | ||
| if def_a.is_struct() && def_b.is_struct() => | ||
| { | ||
| let a_lifetime = single_region_arg(args_a); | ||
| let b_lifetime = single_region_arg(args_b); | ||
|
|
||
| if a_lifetime.is_none() || b_lifetime.is_none() { | ||
| return Err(tcx.dcx().emit_err(diagnostics::CoerceSharedMulti { | ||
| span: diagnostic_context.trait_span, | ||
| trait_name, | ||
| })); | ||
| } | ||
|
|
||
| if a_lifetime != b_lifetime { | ||
| return Err(tcx.dcx().emit_err(diagnostics::CoerceSharedLifetimeMismatch { | ||
| span: diagnostic_context.trait_span, | ||
| source_lifetime_span: diagnostic_context.source_lifetime_span, | ||
| target_lifetime_span: diagnostic_context.target_lifetime_span, | ||
| trait_name, | ||
| })); | ||
| } | ||
|
|
||
| validate_reborrow_field_access( | ||
| tcx, | ||
| impl_did, | ||
|
|
@@ -341,12 +320,6 @@ enum CoerceSharedFieldPairError<'tcx> { | |
| MissingSourceField { target: ReborrowDataField<'tcx> }, | ||
| } | ||
|
|
||
| fn single_region_arg<'tcx>(args: ty::GenericArgsRef<'tcx>) -> Option<ty::Region<'tcx>> { | ||
| let mut lifetimes = args.iter().filter_map(|arg| arg.as_region()); | ||
| let lifetime = lifetimes.next()?; | ||
| lifetimes.next().is_none().then_some(lifetime) | ||
| } | ||
|
|
||
| // This is a coherence/WF check only. It verifies that the CoerceShared impl | ||
| // describes a structurally valid field-wise relation. Runtime lowering of the | ||
| // operation is not modeled here. | ||
|
|
@@ -780,7 +753,7 @@ fn field_tys_satisfy_relation_after_normalization_and_resolution<'tcx>( | |
| FieldRelation::Equal => { | ||
| if infcx | ||
| .at(&cause, param_env) | ||
| .relate(DefineOpaqueTypes::Yes, source_ty, ty::Variance::Invariant, target_ty) | ||
| .relate(DefineOpaqueTypes::Yes, source_ty, ty::Variance::Covariant, target_ty) | ||
| .is_err() | ||
| { | ||
| return false; | ||
|
|
@@ -794,9 +767,12 @@ fn field_tys_satisfy_relation_after_normalization_and_resolution<'tcx>( | |
| else { | ||
| return false; | ||
| }; | ||
| if source_region != target_region { | ||
| return false; | ||
| } | ||
| infcx.sub_regions( | ||
| SubregionOrigin::RelateObjectBound(span), | ||
| target_region, | ||
| source_region, | ||
| ty::VisibleForLeakCheck::Yes, | ||
| ); | ||
| if ocx.sup(&cause, param_env, target_referent_ty, source_referent_ty).is_err() { | ||
| return false; | ||
| } | ||
|
Comment on lines
754
to
778
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find any in-tree docs about the lifetime handling. Should probably add some now that you aren't requiring equality anymore. I had to grab a variance table and poke at the code and tests here until I was sure it was all in the right direction 😆
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where should they be located in? Just here in the code? |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
View changes since the review