-
-
Notifications
You must be signed in to change notification settings - Fork 17.3k
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 |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| use rustc_ast::{self as ast, AttrArgs, GenericArg, GenericParamKind, Generics, ItemKind, token}; | ||
| use rustc_ast::{ | ||
| self as ast, AngleBracketedArg, AttrArgs, DUMMY_NODE_ID, GenericArg, GenericBound, | ||
| GenericParam, GenericParamKind, Generics, ItemKind, WherePredicate, WherePredicateKind, | ||
| WhereRegionPredicate, token, | ||
| }; | ||
| use rustc_errors::E0802; | ||
| use rustc_expand::base::ExtCtxt; | ||
| use rustc_macros::Diagnostic; | ||
| use rustc_span::{Ident, Span, Symbol, sym}; | ||
| use thin_vec::ThinVec; | ||
| use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym}; | ||
| use thin_vec::{ThinVec, thin_vec}; | ||
|
|
||
| macro_rules! path { | ||
| ($span:expr, $($part:ident)::*) => { vec![$(Ident::new(sym::$part, $span),)*] } | ||
|
|
@@ -33,15 +37,15 @@ pub(crate) fn expand_deriving_coerce_shared( | |
| let Some((ident, generics)) = struct_def(cx, span, item, sym::CoerceShared) else { | ||
| return; | ||
| }; | ||
| let Some(target) = coerce_shared_target(cx, span, item) else { | ||
| let Some((target, generics)) = coerce_shared_target(cx, span, item, generics) else { | ||
| return; | ||
| }; | ||
|
|
||
| push_marker_impl( | ||
| cx, | ||
| span, | ||
| ident, | ||
| generics, | ||
| &generics, | ||
| sym::CoerceShared, | ||
| vec![GenericArg::Type(target)], | ||
| push, | ||
|
|
@@ -71,7 +75,12 @@ fn struct_def<'a>( | |
| } | ||
| } | ||
|
|
||
| fn coerce_shared_target(cx: &ExtCtxt<'_>, span: Span, item: &ast::Item) -> Option<Box<ast::Ty>> { | ||
| fn coerce_shared_target( | ||
| cx: &ExtCtxt<'_>, | ||
| span: Span, | ||
| item: &ast::Item, | ||
| generics: &Generics, | ||
| ) -> Option<(Box<ast::Ty>, Generics)> { | ||
| let mut attrs = item.attrs.iter().filter(|attr| attr.has_name(sym::coerce_shared)); | ||
| let Some(attr) = attrs.next() else { | ||
| cx.dcx().emit_err(MissingTarget { span }); | ||
|
|
@@ -92,7 +101,7 @@ fn coerce_shared_target(cx: &ExtCtxt<'_>, span: Span, item: &ast::Item) -> Optio | |
| } | ||
|
|
||
| let mut parser = cx.new_parser_from_tts(args.tokens.clone()); | ||
| let target = match parser.parse_ty() { | ||
| let mut target = match parser.parse_ty() { | ||
| Ok(target) => target, | ||
| Err(err) => { | ||
| err.cancel(); | ||
|
|
@@ -105,7 +114,79 @@ fn coerce_shared_target(cx: &ExtCtxt<'_>, span: Span, item: &ast::Item) -> Optio | |
| return None; | ||
| } | ||
|
|
||
| Some(target) | ||
| let rustc_ast::TyKind::Path(_, path) = &mut target.kind else { | ||
| cx.dcx().emit_err(MalformedTarget { span: attr.span }); | ||
| return None; | ||
| }; | ||
|
|
||
| let Some(last) = path.segments.last_mut() else { | ||
| // FIXME(reborrow): we might want to support CoerceShared<Foo> for Bar at some point. | ||
| cx.dcx().emit_err(MalformedTarget { span: attr.span }); | ||
| return None; | ||
| }; | ||
|
|
||
| let Some(args) = last.args.as_deref_mut() else { | ||
| // FIXME(reborrow): same as above. | ||
| cx.dcx().emit_err(MalformedTarget { span: attr.span }); | ||
| return None; | ||
| }; | ||
|
|
||
| let rustc_ast::GenericArgs::AngleBracketed(args) = args else { | ||
| cx.dcx().emit_err(MalformedTarget { span: attr.span }); | ||
| return None; | ||
| }; | ||
|
|
||
| let mut own_params = generics.params.clone(); | ||
| let mut param_insert_index = own_params | ||
| .iter() | ||
| .enumerate() | ||
| .find(|(_, arg)| { | ||
| matches!( | ||
| arg.kind, | ||
| rustc_ast::GenericParamKind::Lifetime | rustc_ast::GenericParamKind::Type { .. } | ||
| ) | ||
| }) | ||
| .map(|(i, _)| i) | ||
| .unwrap_or(own_params.len()); | ||
| let mut own_where_clause = generics.where_clause.clone(); | ||
|
|
||
| for arg in args.args.iter_mut() { | ||
| // Replace all lifetime parameters 'a with a new 'a_ where 'a: 'a_. | ||
| let AngleBracketedArg::Arg(GenericArg::Lifetime(arg)) = arg else { | ||
| continue; | ||
| }; | ||
| let lt = rustc_ast::Lifetime { | ||
| id: DUMMY_NODE_ID, | ||
| ident: Ident::with_dummy_span(Symbol::intern(&format!("{}_", arg.ident.as_str()))), | ||
| }; | ||
| // FIXME(reborrow): this is terribly inefficient. | ||
| own_params.insert( | ||
| param_insert_index, | ||
| GenericParam { | ||
| id: DUMMY_NODE_ID, | ||
| ident: lt.ident, | ||
| attrs: Default::default(), | ||
| bounds: Default::default(), | ||
| is_placeholder: false, | ||
| kind: GenericParamKind::Lifetime, | ||
| colon_span: None, | ||
| }, | ||
| ); | ||
| param_insert_index += 1; | ||
| own_where_clause.predicates.push(WherePredicate { | ||
| attrs: Default::default(), | ||
| kind: WherePredicateKind::RegionPredicate(WhereRegionPredicate { | ||
| lifetime: *arg, | ||
| bounds: thin_vec![GenericBound::Outlives(lt)], | ||
| }), | ||
| id: DUMMY_NODE_ID, | ||
| span: DUMMY_SP, | ||
| is_placeholder: false, | ||
| }); | ||
| *arg = lt; | ||
| } | ||
|
|
||
| Some((target, Generics { params: own_params, where_clause: own_where_clause, span })) | ||
| } | ||
|
|
||
| fn push_marker_impl( | ||
|
|
@@ -125,15 +206,19 @@ fn push_marker_impl( | |
| let self_params: Vec<_> = generics | ||
| .params | ||
| .iter() | ||
| .map(|param| match param.kind { | ||
| .filter_map(|param| match param.kind { | ||
| GenericParamKind::Lifetime => { | ||
| GenericArg::Lifetime(cx.lifetime(param.span(), param.ident)) | ||
| if trait_name == sym::CoerceShared && !param.ident.as_str().ends_with("_") { | ||
|
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. What would happen if the lifetime generic already has a |
||
| None | ||
| } else { | ||
| Some(GenericArg::Lifetime(cx.lifetime(param.span(), param.ident))) | ||
| } | ||
| } | ||
| GenericParamKind::Type { .. } => { | ||
| GenericArg::Type(cx.ty_ident(param.span(), param.ident)) | ||
| Some(GenericArg::Type(cx.ty_ident(param.span(), param.ident))) | ||
| } | ||
| GenericParamKind::Const { .. } => { | ||
| GenericArg::Const(cx.const_ident(param.span(), param.ident)) | ||
| Some(GenericArg::Const(cx.const_ident(param.span(), param.ident))) | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| 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,7 +239,7 @@ 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 { | ||
| if trait_impl_lifetime_params_count(tcx, impl_did) == 0 { | ||
| return Err(tcx | ||
| .dcx() | ||
| .emit_err(diagnostics::CoerceSharedNotSingleLifetimeParam { span, trait_name })); | ||
|
Comment on lines
-242
to
245
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. probably needs updating of the diagnostic (and at minimum of the diagnostic struct name) |
||
|
|
@@ -266,25 +266,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 +322,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. | ||
|
|
@@ -789,7 +764,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; | ||
|
|
@@ -803,9 +778,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
765
to
789
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 😆 |
||
|
|
||
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.
probably want to use more precise for all the
MalformedTargeterrors, usually you have a specific item available which has its own spanView changes since the review