| //! Support inheriting generic parameters and predicates for function delegation. | |
| //! | |
| //! For more information about delegation design, see the tracking issue #118212. | |
| use std::assert_matches; | |
| use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | |
| use rustc_hir::def::DefKind; | |
| use rustc_hir::def_id::{DefId, LocalDefId}; | |
| use rustc_hir::{DelegationSelfTyPropagationKind, PathSegment}; | |
| use rustc_middle::ty::{ | |
| self, ConstKind, EarlyBinder, GenericArg, GenericArgKind, RegionKind, Ty, TyCtxt, TypeFoldable, | |
| TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast, | |
| }; | |
| use rustc_span::{ErrorGuaranteed, Span, kw}; | |
| use crate::collect::ItemCtxt; | |
| use crate::hir_ty_lowering::HirTyLowerer; | |
| type RemapTable = FxHashMap<u32, u32>; | |
| struct ParamIndexRemapper<'tcx> { | |
| tcx: TyCtxt<'tcx>, | |
| remap_table: RemapTable, | |
| delegation_parent_consts: FxHashSet<ty::ParamConst>, | |
| arg_kinds: Vec<ty::GenericArgKind<'tcx>>, | |
| } | |
| impl<'tcx> ParamIndexRemapper<'tcx> { | |
| fn try_remap( | |
| &self, | |
| source_index: u32, | |
| expected_kind: ty::GenericArgKind<'tcx>, | |
| ) -> Result<u32, ()> { | |
| self.remap_table | |
| .get(&source_index) | |
| .copied() | |
| .filter(|&idx| { | |
| self.arg_kinds.get(idx as usize).is_some_and(|kind| { | |
| std::mem::discriminant(&expected_kind) == std::mem::discriminant(kind) | |
| }) | |
| }) | |
| .ok_or(()) | |
| } | |
| } | |
| impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for ParamIndexRemapper<'tcx> { | |
| type Error = (); | |
| fn cx(&self) -> TyCtxt<'tcx> { | |
| self.tcx | |
| } | |
| fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> { | |
| if !ty.has_param() { | |
| return Ok(ty); | |
| } | |
| if let ty::Param(param) = ty.kind() { | |
| let index = self.try_remap(param.index, ty::GenericArgKind::Type(ty))?; | |
| return Ok(Ty::new_param(self.tcx, index, param.name)); | |
| } | |
| ty.try_super_fold_with(self) | |
| } | |
| fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> { | |
| if let ty::ReEarlyParam(param) = r.kind() { | |
| let index = self.try_remap(param.index, ty::GenericArgKind::Lifetime(r))?; | |
| return Ok(ty::Region::new_early_param( | |
| self.tcx, | |
| ty::EarlyParamRegion { index, name: param.name }, | |
| )); | |
| } | |
| Ok(r) | |
| } | |
| fn try_fold_const(&mut self, ct: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> { | |
| if let ty::ConstKind::Param(param) = ct.kind() { | |
| let idx = self.try_remap(param.index, ty::GenericArgKind::Const(ct))?; | |
| let param = ty::ParamConst::new(idx, param.name); | |
| return Ok(ty::Const::new_param(self.tcx, param)); | |
| } | |
| ct.try_super_fold_with(self) | |
| } | |
| } | |
| #[derive(Debug)] | |
| enum SelfPositionKind { | |
| AfterLifetimes(Option<DelegationSelfTyPropagationKind>), | |
| Zero, | |
| None, | |
| } | |
| fn create_self_param_position_kind( | |
| tcx: TyCtxt<'_>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| ) -> SelfPositionKind { | |
| match fn_kinds(tcx, def_id, sig_id) { | |
| (FnKind::Free, FnKind::AssocTrait) => { | |
| let kind = tcx.hir_delegation_info(def_id).self_ty_propagation_kind; | |
| SelfPositionKind::AfterLifetimes(kind) | |
| } | |
| (_, FnKind::AssocTraitImpl) => unreachable!(), | |
| (_, FnKind::AssocTrait) | (FnKind::AssocTrait, _) => SelfPositionKind::Zero, | |
| (FnKind::AssocTraitImpl, _) => unreachable!(), | |
| _ => SelfPositionKind::None, | |
| } | |
| } | |
| #[derive(Clone, Copy, Debug, PartialEq)] | |
| enum FnKind { | |
| Free, | |
| AssocInherentImpl, | |
| AssocTrait, | |
| AssocTraitImpl, | |
| } | |
| fn fn_kind<'tcx>(tcx: TyCtxt<'tcx>, def_id: impl Into<DefId>) -> FnKind { | |
| let def_id = def_id.into(); | |
| match tcx.def_kind(def_id) { | |
| DefKind::Fn => FnKind::Free, | |
| DefKind::AssocFn => match tcx.def_kind(tcx.parent(def_id)) { | |
| DefKind::Trait => FnKind::AssocTrait, | |
| DefKind::Impl { of_trait } => match of_trait { | |
| true => FnKind::AssocTraitImpl, | |
| false => FnKind::AssocInherentImpl, | |
| }, | |
| _ => unreachable!("associated function can only be in trait or impl"), | |
| }, | |
| _ => unreachable!("delegation/signature can be either free or associated function"), | |
| } | |
| } | |
| fn fn_kinds(tcx: TyCtxt<'_>, def_id: LocalDefId, sig_id: DefId) -> (FnKind, FnKind) { | |
| let kinds = (fn_kind(tcx, def_id), fn_kind(tcx, sig_id)); | |
| // For trait impl's `sig_id` is always equal to the corresponding trait method. | |
| assert!(!matches!(kinds, (_, FnKind::AssocTraitImpl))); | |
| kinds | |
| } | |
| /// Given the current context(caller and callee `FnKind`), it specifies | |
| /// the policy of predicates and generic parameters inheritance. | |
| #[derive(Clone, Copy, Debug, PartialEq)] | |
| enum InheritanceKind { | |
| /// Copying all predicates and parameters, including those of the parent | |
| /// container. | |
| /// | |
| /// Boolean value defines whether the `Self` parameter or `Self: Trait` | |
| /// predicate are copied. It's always equal to `false` except when | |
| /// delegating from a free function to a trait method. | |
| /// | |
| /// FIXME(fn_delegation): This often leads to type inference | |
| /// errors. Support providing generic arguments or restrict use sites. | |
| WithParent(bool), | |
| /// The trait implementation should be compatible with the original trait. | |
| /// Therefore, for trait implementations only the method's own parameters | |
| /// and predicates are copied. | |
| Own, | |
| } | |
| /// Maps sig generics into generic args of delegation. Delegation generics has the following pattern: | |
| /// | |
| /// [SELF | maybe self in the beginning] | |
| /// [PARENT | args of delegation parent] | |
| /// [SIG PARENT LIFETIMES] | |
| /// [SIG LIFETIMES] | |
| /// [SELF | maybe self after lifetimes, when we reuse trait fn in free context] | |
| /// [SIG PARENT TYPES/CONSTS] | |
| /// [SIG TYPES/CONSTS] | |
| fn create_mapping<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| sig_id: DefId, | |
| def_id: LocalDefId, | |
| ) -> FxHashMap<u32, u32> { | |
| let mut mapping: FxHashMap<u32, u32> = Default::default(); | |
| let self_pos_kind = create_self_param_position_kind(tcx, def_id, sig_id); | |
| let is_self_at_zero = matches!(self_pos_kind, SelfPositionKind::Zero); | |
| // Is self at zero? If so insert mapping, self in sig parent is always at 0. | |
| if is_self_at_zero { | |
| mapping.insert(0, 0); | |
| } | |
| let mut args_index = 0; | |
| args_index += is_self_at_zero as usize; | |
| args_index += get_delegation_parent_args_count_without_self(tcx, def_id, sig_id); | |
| let parent_kind = fn_kind(tcx, sig_id); | |
| let process_parent = matches!(parent_kind, FnKind::AssocTrait | FnKind::AssocInherentImpl); | |
| let parent_generics = process_parent.then(|| tcx.generics_of(tcx.parent(sig_id))); | |
| // In case of delegations to inherent impls indices of generic params which are passed | |
| // to ADT can be random numbers not from range 0..parent_params_count, so we need to | |
| // use original indices in mapping: | |
| // impl<'a, 'b, 'c, A: 'a, const C: usize> S<'a, A, C> { | |
| // fn foo_static<'d: 'd, 'e, T, const B: bool>() {} | |
| // fn foo_self<'d: 'd, 'e, T, const B: bool>(self) {} | |
| // }, | |
| // 'a has index 0, A index 3, C index 4. If we encounter not a generic param as generic arg, | |
| // then we do not need to map it (i.e. consts like `1`, `2`, `3`; `'static`, etc.). | |
| let parent_params = match parent_kind { | |
| FnKind::AssocInherentImpl => { | |
| let ty::Adt(_, args) = tcx.type_of(tcx.parent(sig_id)).skip_binder().kind() else { | |
| unreachable!("parent of inherent function in delegation can be only struct or enum") | |
| }; | |
| let opt_param_info = |arg: GenericArg<'_>| match arg.kind() { | |
| GenericArgKind::Lifetime(r) => ( | |
| match r.kind() { | |
| RegionKind::ReEarlyParam(p) => Some(p.index), | |
| _ => None, | |
| }, | |
| false, | |
| ), | |
| GenericArgKind::Type(t) => ( | |
| match t.kind() { | |
| ty::Param(p) => Some(p.index), | |
| _ => None, | |
| }, | |
| true, | |
| ), | |
| GenericArgKind::Const(c) => ( | |
| match c.kind() { | |
| ConstKind::Param(p) => Some(p.index), | |
| _ => None, | |
| }, | |
| true, | |
| ), | |
| }; | |
| args.iter().map(opt_param_info).collect::<Vec<_>>() | |
| } | |
| FnKind::AssocTrait => parent_generics | |
| .expect("trait must have generics") | |
| .own_params | |
| .iter() | |
| .map(|p| (Some(p.index as u32), p.kind.is_ty_or_const())) | |
| .collect::<Vec<_>>(), | |
| _ => vec![], | |
| }; | |
| let has_self = match parent_kind { | |
| FnKind::AssocTrait => parent_generics.expect("trait must have generics").has_self, | |
| _ => false, | |
| }; | |
| if process_parent { | |
| for i in (has_self as usize)..parent_params.len() { | |
| let (index, is_ty_or_const) = parent_params[i]; | |
| if !is_ty_or_const { | |
| if let Some(index) = index { | |
| mapping.insert(index, args_index as u32); | |
| } | |
| args_index += 1; | |
| } | |
| } | |
| } | |
| let child_generics = tcx.generics_of(sig_id); | |
| for param in &child_generics.own_params { | |
| if !param.kind.is_ty_or_const() { | |
| mapping.insert(param.index, args_index as u32); | |
| args_index += 1; | |
| } | |
| } | |
| // If self after lifetimes insert mapping, relying that self is at 0 in sig parent. | |
| // If self ty is propagated (meaning there is no generic param `Self`), the specified | |
| // self ty will be inserted in args in `create_generic_args`. | |
| if matches!(self_pos_kind, SelfPositionKind::AfterLifetimes { .. }) { | |
| mapping.insert(0, args_index as u32); | |
| args_index += 1; | |
| } | |
| if process_parent { | |
| for i in (has_self as usize)..parent_params.len() { | |
| let (index, is_ty_or_const) = parent_params[i]; | |
| if is_ty_or_const { | |
| if let Some(index) = index { | |
| mapping.insert(index, args_index as u32); | |
| } | |
| args_index += 1; | |
| } | |
| } | |
| } | |
| for param in &child_generics.own_params { | |
| if param.kind.is_ty_or_const() { | |
| mapping.insert(param.index, args_index as u32); | |
| args_index += 1; | |
| } | |
| } | |
| mapping | |
| } | |
| fn get_delegation_parent_args_count_without_self<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| ) -> usize { | |
| let kinds @ (def_kind, _) = fn_kinds(tcx, def_id, sig_id); | |
| match kinds { | |
| (FnKind::AssocTraitImpl, FnKind::AssocTrait) => 0, | |
| (FnKind::AssocTraitImpl, _) | (_, FnKind::AssocTraitImpl) => unreachable!(), | |
| (FnKind::Free, _) => 0, | |
| (_, _) => { | |
| let delegation_parent_args_count = tcx.generics_of(def_id).parent_count; | |
| let has_self = def_kind == FnKind::AssocTrait; | |
| delegation_parent_args_count - usize::from(has_self) | |
| } | |
| } | |
| } | |
| fn get_parent_and_inheritance_kind<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| ) -> (Option<DefId>, InheritanceKind) { | |
| let kinds @ (_, sig_kind) = fn_kinds(tcx, def_id, sig_id); | |
| match kinds { | |
| (FnKind::AssocTraitImpl, FnKind::AssocTrait) => { | |
| (Some(tcx.parent(def_id.to_def_id())), InheritanceKind::Own) | |
| } | |
| (FnKind::AssocTraitImpl, _) | (_, FnKind::AssocTraitImpl) => unreachable!(), | |
| (FnKind::Free, _) => { | |
| let copy_self_clauses = sig_kind == FnKind::AssocTrait; | |
| (None, InheritanceKind::WithParent(copy_self_clauses)) | |
| } | |
| (_, _) => (Some(tcx.parent(def_id.to_def_id())), InheritanceKind::WithParent(false)), | |
| } | |
| } | |
| fn get_delegation_self_ty<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option<Ty<'tcx>> { | |
| let sig_id = tcx.hir_opt_delegation_sig_id(def_id).expect("processing delegation"); | |
| let (caller_kind, callee_kind) = fn_kinds(tcx, def_id, sig_id); | |
| match (caller_kind, callee_kind) { | |
| (FnKind::AssocTraitImpl, FnKind::AssocTrait) | (FnKind::AssocInherentImpl, _) => { | |
| Some(tcx.type_of(tcx.local_parent(def_id)).instantiate_identity().skip_norm_wip()) | |
| } | |
| // For trait impl's `sig_id` is always equal to the corresponding trait method. | |
| (FnKind::AssocTraitImpl, _) | (_, FnKind::AssocTraitImpl) => unreachable!(), | |
| (_, _) => match create_self_param_position_kind(tcx, def_id, sig_id) { | |
| SelfPositionKind::None => None, | |
| SelfPositionKind::AfterLifetimes(propagation_kind) => Some(match propagation_kind { | |
| Some(kind) => match kind { | |
| DelegationSelfTyPropagationKind::SelfTy(self_ty_id) => { | |
| let ctx = ItemCtxt::new(tcx, def_id); | |
| ctx.lower_ty(tcx.hir_node(self_ty_id).expect_ty()) | |
| } | |
| DelegationSelfTyPropagationKind::SelfParam => { | |
| let index = tcx.generics_of(def_id).own_counts().lifetimes; | |
| Ty::new_param(tcx, index as u32, kw::SelfUpper) | |
| } | |
| }, | |
| None => Ty::new_error_with_message( | |
| tcx, | |
| tcx.def_span(def_id), | |
| "self propagation kind must be specified for `AfterLifetimes` variant", | |
| ), | |
| }), | |
| SelfPositionKind::Zero => Some(Ty::new_param(tcx, 0, kw::SelfUpper)), | |
| }, | |
| } | |
| } | |
| /// Creates generic arguments for further delegation signature and predicates instantiation. | |
| /// Arguments can be user-specified (in this case they are in `parent_args` and `child_args`) | |
| /// or propagated. User can specify either both `parent_args` and `child_args`, one of them or none, | |
| /// that is why we firstly create generic arguments from generic params and then adjust them with | |
| /// user-specified args. | |
| /// | |
| /// The order of produced list is important, it must be of this pattern: | |
| /// | |
| /// [SELF | maybe self in the beginning] | |
| /// [PARENT | args of delegation parent] | |
| /// [SIG PARENT LIFETIMES] <- `lifetimes_end_pos` | |
| /// [SIG LIFETIMES] | |
| /// [SELF | maybe self after lifetimes, when we reuse trait fn in free context] | |
| /// [SIG PARENT TYPES/CONSTS] | |
| /// [SIG TYPES/CONSTS] | |
| fn create_generic_args<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| sig_id: DefId, | |
| def_id: LocalDefId, | |
| mut parent_args: ty::GenericArgs<'tcx>, | |
| mut child_args: ty::GenericArgs<'tcx>, | |
| ) -> (Vec<ty::GenericArg<'tcx>>, ty::GenericArgs<'tcx>) { | |
| let delegation_generics = tcx.generics_of(def_id); | |
| let delegation_args = ty::GenericArgs::identity_for_item(tcx, def_id); | |
| let real_args_count = delegation_args.len() - delegation_generics.own_synthetic_params_count(); | |
| let synth_args = &delegation_args.as_slice()[real_args_count..]; | |
| let mut delegation_parent_args = tcx.mk_args( | |
| &delegation_args.as_slice() | |
| [delegation_generics.has_self as usize..delegation_generics.parent_count], | |
| ); | |
| let delegation_args = &delegation_args.as_slice()[delegation_generics.parent_count..]; | |
| let kinds @ (_, parent_kind) = fn_kinds(tcx, def_id, sig_id); | |
| if matches!(kinds, (FnKind::AssocTraitImpl, FnKind::AssocTrait)) { | |
| // Special case, as user specifies Trait args in trait impl header, we want to treat | |
| // them as parent args. We always generate a function whose generics match | |
| // child generics in trait. | |
| let parent = tcx.local_parent(def_id); | |
| parent_args = | |
| tcx.impl_trait_header(parent).trait_ref.instantiate_identity().skip_norm_wip().args; | |
| child_args = tcx.mk_args( | |
| &delegation_args[delegation_args.len() - delegation_generics.own_params.len()..], | |
| ); | |
| delegation_parent_args = ty::GenericArgs::empty(); | |
| } | |
| let self_type = get_delegation_self_ty(tcx, def_id).map(ty::GenericArg::from); | |
| if self_type.is_some() && !parent_args.is_empty() { | |
| parent_args = match parent_kind { | |
| FnKind::AssocInherentImpl => parent_args, | |
| // Remove `Self` from parent args (it is always at the `0th` index) as it is | |
| // added manually. | |
| FnKind::AssocTrait => tcx.mk_args(&parent_args.as_slice()[1..]), | |
| _ => unreachable!("if parent args are non-empty then the parent must exist"), | |
| } | |
| } | |
| let (zero_self, after_lifetimes_self) = | |
| match create_self_param_position_kind(tcx, def_id, sig_id) { | |
| SelfPositionKind::AfterLifetimes(_) => { | |
| assert!(self_type.is_some()); | |
| (None, self_type) | |
| } | |
| SelfPositionKind::Zero => { | |
| assert!(self_type.is_some()); | |
| (self_type, None) | |
| } | |
| SelfPositionKind::None => (None, None), | |
| }; | |
| let zero_self = zero_self.as_ref().into_iter(); | |
| let after_lifetimes_self = after_lifetimes_self.as_ref().into_iter(); | |
| let args = zero_self | |
| .copied() | |
| .chain(delegation_parent_args.iter()) | |
| .chain(parent_args.iter().filter(|a| a.as_region().is_some())) | |
| .chain(child_args.iter().filter(|a| a.as_region().is_some())) | |
| .chain(after_lifetimes_self.copied()) | |
| .chain(parent_args.iter().filter(|a| a.as_region().is_none())) | |
| .chain(child_args.iter().filter(|a| a.as_region().is_none())) | |
| .chain(synth_args.iter().copied()) | |
| .collect::<Vec<_>>(); | |
| (args, delegation_parent_args) | |
| } | |
| pub(crate) fn inherit_clauses_for_delegation_item<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| ) -> ty::GenericClauses<'tcx> { | |
| struct ClausesCollector<'tcx> { | |
| tcx: TyCtxt<'tcx>, | |
| clauses: Vec<(ty::Clause<'tcx>, Span)>, | |
| args: Vec<ty::GenericArg<'tcx>>, | |
| folder: ParamIndexRemapper<'tcx>, | |
| filter_self_clauses: bool, | |
| } | |
| impl<'tcx> ClausesCollector<'tcx> { | |
| fn with_own_clauses( | |
| mut self, | |
| f: impl Fn(DefId) -> ty::GenericClauses<'tcx>, | |
| def_id: DefId, | |
| ) -> Result<Self, ()> { | |
| let clauses = f(def_id); | |
| let args = self.args.as_slice(); | |
| for clause in clauses.clauses { | |
| // If self ty is specified then there will be no generic param `Self`, | |
| // so we do not need its clauses. | |
| if self.filter_self_clauses | |
| && let Some(trait_clause) = clause.0.as_trait_clause() | |
| // Rely that `Self` has zero index. | |
| && trait_clause.self_ty().skip_binder().is_param(0) | |
| { | |
| continue; | |
| } | |
| // If we have a constant in parent or child args that came from delegation | |
| // parent: | |
| // ```rust | |
| // trait Trait<T, const B: bool> { /* .. */} | |
| // impl<const N: usize> S<N> { | |
| // reuse Trait::<S<N>, N>::foo; | |
| // } | |
| // ``` | |
| // Then if we inherit const clause from `Trait` then we end up with | |
| // two `ConstArgHasType` for `N` constant: | |
| // 1) ConstArgHasType(N/#0, bool) from `Trait` | |
| // 2) ConstArgHasType(N/#0, usize) from delegation parent | |
| // So in case the constant came from delegation parent we will not inherit | |
| // ConstArgHasType from signature. | |
| // The check is so complicated because we build generic args for signature | |
| // and clauses inheritance, for the example above it will be | |
| // `args = [S<N/#0>, N/#0, S<N/#0>, N/#0]`, where | |
| // args[0] - Self type, args[1] - delegation parent const, args[2] - first | |
| // arg of callee path, args[3] - second arg of callee path. | |
| // When processing clause ConstArgHasType(B/#2, bool) | |
| // from delegation signature (`Trait::foo`), we need to map `B/#2` into some | |
| // arg from `args`. The mapping which is built by `create_mapping` function is: | |
| // `{0: 0, 2: 3, 1: 2}`, so as `B/#2` has index `2` it is mapped into third | |
| // arg from `args` - `N/#0`. After we obtained mapped const param, we check if | |
| // it came from delegation parent, and if so we do not process its `ConstArgHasType` | |
| // clause. | |
| // (Issue #158675). | |
| let new_clause = clause.0.try_fold_with(&mut self.folder)?; | |
| if let ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, _)) = | |
| new_clause.as_predicate().kind().skip_binder() | |
| { | |
| let unnorm_const = EarlyBinder::bind(self.tcx, ct).instantiate(self.tcx, args); | |
| if let ty::ConstKind::Param(param) = unnorm_const.skip_norm_wip().kind() | |
| && self.folder.delegation_parent_consts.contains(¶m) | |
| { | |
| continue; | |
| } | |
| } | |
| self.clauses.push(( | |
| EarlyBinder::bind(self.tcx, new_clause) | |
| .instantiate(self.tcx, args) | |
| .skip_norm_wip(), | |
| clause.1, | |
| )); | |
| } | |
| Ok(self) | |
| } | |
| fn with_clauses( | |
| mut self, | |
| f: impl Fn(DefId) -> ty::GenericClauses<'tcx> + Copy, | |
| def_id: DefId, | |
| ) -> Result<Self, ()> { | |
| let preds = f(def_id); | |
| if let Some(parent_def_id) = preds.parent { | |
| self = self.with_own_clauses(f, parent_def_id)?; | |
| } | |
| self.with_own_clauses(f, def_id) | |
| } | |
| } | |
| let (parent_args, child_args) = tcx.delegation_user_specified_args(def_id); | |
| let (folder, args) = create_folder_and_args(tcx, def_id, sig_id, parent_args, child_args); | |
| let self_pos_kind = create_self_param_position_kind(tcx, def_id, sig_id); | |
| let filter_self_clauses = matches!( | |
| self_pos_kind, | |
| SelfPositionKind::AfterLifetimes(Some(DelegationSelfTyPropagationKind::SelfTy(..))) | |
| ); | |
| let collector = ClausesCollector { tcx, clauses: vec![], args, folder, filter_self_clauses }; | |
| let (parent, inh_kind) = get_parent_and_inheritance_kind(tcx, def_id, sig_id); | |
| // `explicit_clauses_of` is used here to avoid copying `Self: Trait` clause. | |
| // Note: `clauses_of` query can also add inferred outlives clauses, but that | |
| // is not the case here as `sig_id` is either a trait or a function. | |
| let collector = match inh_kind { | |
| InheritanceKind::WithParent(false) => { | |
| collector.with_clauses(|def_id| tcx.explicit_clauses_of(def_id), sig_id) | |
| } | |
| InheritanceKind::WithParent(true) => { | |
| collector.with_clauses(|def_id| tcx.clauses_of(def_id), sig_id) | |
| } | |
| InheritanceKind::Own => collector.with_own_clauses(|def_id| tcx.clauses_of(def_id), sig_id), | |
| }; | |
| let clauses = match collector { | |
| Ok(collector) => collector.clauses, | |
| Err(()) => { | |
| tcx.dcx().emit_err(crate::diagnostics::DelegationInheritanceFailed { | |
| span: tcx.def_span(def_id), | |
| kind: "predicates", | |
| callee_span: tcx.def_span(sig_id), | |
| }); | |
| // Even on error, the delegated item's const parameters need their | |
| // types in the parameter environment. | |
| tcx.generics_of(def_id) | |
| .own_params | |
| .iter() | |
| .filter_map(|param| { | |
| if !matches!(param.kind, ty::GenericParamDefKind::Const { .. }) { | |
| return None; | |
| } | |
| let ct = | |
| ty::Const::new_param(tcx, ty::ParamConst::new(param.index, param.name)); | |
| let ct_ty = tcx.type_of(param.def_id).instantiate_identity().skip_norm_wip(); | |
| Some(( | |
| ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), | |
| tcx.def_span(param.def_id), | |
| )) | |
| }) | |
| .collect::<Vec<_>>() | |
| } | |
| }; | |
| ty::GenericClauses { parent, clauses: tcx.arena.alloc_from_iter(clauses) } | |
| } | |
| fn create_folder_and_args<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| parent_args: ty::GenericArgs<'tcx>, | |
| child_args: ty::GenericArgs<'tcx>, | |
| ) -> (ParamIndexRemapper<'tcx>, Vec<ty::GenericArg<'tcx>>) { | |
| let (args, delegation_parent_args) = | |
| create_generic_args(tcx, sig_id, def_id, parent_args, child_args); | |
| let remap_table = create_mapping(tcx, sig_id, def_id); | |
| let delegation_parent_consts = delegation_parent_args | |
| .iter() | |
| .filter_map(|a| { | |
| a.as_const().and_then(|c| { | |
| if let ty::ConstKind::Param(param) = c.kind() { Some(param) } else { None } | |
| }) | |
| }) | |
| .collect(); | |
| let arg_kinds = args.iter().map(|arg| arg.kind()).collect(); | |
| (ParamIndexRemapper { tcx, remap_table, delegation_parent_consts, arg_kinds }, args) | |
| } | |
| fn check_constraints<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| sig_id: DefId, | |
| ) -> Result<(), ErrorGuaranteed> { | |
| let mut ret = Ok(()); | |
| let mut emit = |descr| { | |
| ret = Err(tcx.dcx().emit_err(crate::diagnostics::UnsupportedDelegation { | |
| span: tcx.def_span(def_id), | |
| descr, | |
| callee_span: tcx.def_span(sig_id), | |
| })); | |
| }; | |
| if tcx.fn_sig(sig_id).skip_binder().skip_binder().c_variadic() { | |
| // See issue #127443 for explanation. | |
| emit("delegation to C-variadic functions is not allowed"); | |
| } | |
| ret | |
| } | |
| fn error_sig_for_delegation<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| sig_id: DefId, | |
| err: ErrorGuaranteed, | |
| ) -> &'tcx [Ty<'tcx>] { | |
| let sig_len = tcx.fn_sig(sig_id).instantiate_identity().skip_binder().inputs().len() + 1; | |
| let err_type = Ty::new_error(tcx, err); | |
| tcx.arena.alloc_from_iter((0..sig_len).map(|_| err_type)) | |
| } | |
| pub(crate) fn inherit_sig_for_delegation_item<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| ) -> &'tcx [Ty<'tcx>] { | |
| let sig_id = tcx.hir_opt_delegation_sig_id(def_id).expect("Delegation must have sig_id"); | |
| let caller_sig = tcx.fn_sig(sig_id); | |
| if let Err(err) = check_constraints(tcx, def_id, sig_id) { | |
| return error_sig_for_delegation(tcx, sig_id, err); | |
| } | |
| let (parent_args, child_args) = tcx.delegation_user_specified_args(def_id); | |
| let (mut folder, args) = create_folder_and_args(tcx, def_id, sig_id, parent_args, child_args); | |
| let folded_sig = match caller_sig.skip_binder().try_fold_with(&mut folder) { | |
| Ok(sig) => sig, | |
| Err(()) => { | |
| let err = tcx.dcx().emit_err(crate::diagnostics::DelegationInheritanceFailed { | |
| span: tcx.def_span(def_id), | |
| kind: "signature", | |
| callee_span: tcx.def_span(sig_id), | |
| }); | |
| return error_sig_for_delegation(tcx, sig_id, err); | |
| } | |
| }; | |
| let caller_sig = EarlyBinder::bind(tcx, folded_sig); | |
| let sig = caller_sig.instantiate(tcx, args.as_slice()).skip_binder(); | |
| let output = std::iter::once(sig.output()); | |
| let mut sig = sig.inputs().iter().cloned().chain(output).collect::<Vec<_>>(); | |
| adjust_sig_in_inherent_impl_cases(tcx, sig_id, def_id, parent_args, &mut sig); | |
| tcx.arena.alloc_from_iter(sig) | |
| } | |
| /// We need to replace `Self` type of the signature function parent with | |
| /// either type of parent of delegation (which is either `Self` param in case of trait) | |
| /// and other ADT in case of inherent impl. We do the same thing when delegating to trait, | |
| /// in this case replacement happens during signature instantiation (as we can replace `Self` | |
| /// generic param with other type from `args` when instantiating). | |
| fn adjust_sig_in_inherent_impl_cases<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| sig_id: DefId, | |
| def_id: LocalDefId, | |
| parent_args: ty::GenericArgs<'tcx>, | |
| sig: &mut [Ty<'tcx>], | |
| ) { | |
| if !tcx.is_method(sig_id) { | |
| return; | |
| } | |
| let kinds @ (def_kind, _) = fn_kinds(tcx, def_id, sig_id); | |
| if def_kind == FnKind::Free || !matches!(kinds, (_, FnKind::AssocInherentImpl)) { | |
| return; | |
| } | |
| let ty::Adt(def, _) = tcx.type_of(tcx.parent(sig_id)).skip_binder().kind() else { | |
| unreachable!("delegation is supported only to struct or enums") | |
| }; | |
| for i in 0..sig.len() { | |
| let to_replace = Ty::new_adt(tcx, *def, parent_args); | |
| let replacement = match def_kind { | |
| FnKind::Free => unreachable!(), | |
| FnKind::AssocTrait => Ty::new_param(tcx, 0, kw::SelfUpper), | |
| _ => tcx.type_of(tcx.parent(def_id.to_def_id())).instantiate_identity().skip_norm_wip(), | |
| }; | |
| struct Replacer<'tcx> { | |
| tcx: TyCtxt<'tcx>, | |
| to_replace: Ty<'tcx>, | |
| replacement: Ty<'tcx>, | |
| } | |
| impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Replacer<'tcx> { | |
| fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
| if t == self.to_replace { self.replacement } else { t.super_fold_with(self) } | |
| } | |
| fn cx(&self) -> TyCtxt<'tcx> { | |
| self.tcx | |
| } | |
| } | |
| sig[i] = sig[i].fold_with(&mut Replacer { tcx, to_replace, replacement }) | |
| } | |
| } | |
| // Creates user-specified generic arguments from delegation path, | |
| // they will be used during delegation signature and predicates inheritance. | |
| // Example: reuse Trait::<'static, i32, 1>::foo::<A, B> | |
| // we want to extract [Self, 'static, i32, 1] for parent and [A, B] for child. | |
| pub(crate) fn delegation_user_specified_args<'tcx>( | |
| tcx: TyCtxt<'tcx>, | |
| def_id: LocalDefId, | |
| ) -> (ty::GenericArgs<'tcx>, ty::GenericArgs<'tcx>) { | |
| let info = tcx.hir_delegation_info(def_id); | |
| let get_segment = |hir_id| -> Option<(&'tcx PathSegment<'tcx>, DefId)> { | |
| let segment = tcx.hir_node(hir_id).expect_path_segment(); | |
| segment.res.opt_def_id().map(|def_id| (segment, def_id)) | |
| }; | |
| let ctx = ItemCtxt::new_for_delegation(tcx, def_id); | |
| let lowerer = ctx.lowerer(); | |
| let parent_args = info | |
| .parent_seg_id_for_sig | |
| .and_then(get_segment) | |
| .filter(|(_, def_id)| !matches!(tcx.def_kind(*def_id), DefKind::Mod)) | |
| .map(|(segment, def_id)| { | |
| // After lowering parent segment can be resolved only to those variants (and `DefKind::Mod`), | |
| // which we do not process here. | |
| assert_matches!(tcx.def_kind(def_id), DefKind::Trait | DefKind::Struct | DefKind::Enum); | |
| let self_ty = (tcx.def_kind(def_id) == DefKind::Trait) | |
| .then(|| Ty::new_param(tcx, 0, kw::SelfUpper)); | |
| tcx.mk_args( | |
| lowerer | |
| .lower_generic_args_of_path(segment.ident.span, def_id, &[], segment, self_ty) | |
| .0 | |
| .as_slice(), | |
| ) | |
| }); | |
| let child_args = info.child_seg_id_for_sig.and_then(get_segment).map(|(segment, def_id)| { | |
| assert_matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn); | |
| let parent = tcx.parent(def_id); | |
| let parent_args = | |
| if matches!(tcx.def_kind(parent), DefKind::Impl { of_trait: false } | DefKind::Trait) { | |
| ty::GenericArgs::identity_for_item(tcx, parent) | |
| } else if let Some(parent_args) = parent_args { | |
| parent_args | |
| } else { | |
| ty::GenericArgs::empty() | |
| }; | |
| let args = lowerer | |
| .lower_generic_args_of_path( | |
| segment.ident.span, | |
| def_id, | |
| parent_args.as_slice(), | |
| segment, | |
| None, | |
| ) | |
| .0; | |
| let synth_params_count = tcx.generics_of(def_id).own_synthetic_params_count(); | |
| tcx.mk_args(&args.as_slice()[parent_args.len()..args.len() - synth_params_count]) | |
| }); | |
| (parent_args.unwrap_or_default(), child_args.unwrap_or_default()) | |
| } |