Skip to content

Commit

Permalink
Auto merge of #126161 - Bryanskiy:delegation-generics-4, r=petrochenkov
Browse files Browse the repository at this point in the history
Delegation: support generics in associated delegation items

This is a continuation of #125929.

[design](https://github.com/Bryanskiy/posts/blob/master/delegation%20in%20generic%20contexts.md)

Generic parameters inheritance was implemented in all contexts. Generic arguments are not yet supported.

r? `@petrochenkov`
  • Loading branch information
bors committed Sep 7, 2024
2 parents 9afe713 + 588dce1 commit a445d0a
Show file tree
Hide file tree
Showing 16 changed files with 777 additions and 445 deletions.
16 changes: 6 additions & 10 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
let hir_id = tcx.local_def_id_to_hir_id(def_id);

let node = tcx.hir_node(hir_id);
if let Some(sig) = node.fn_sig()
&& let Some(sig_id) = sig.decl.opt_delegation_sig_id()
{
return inherit_generics_for_delegation_item(tcx, def_id, sig_id);
}

let parent_def_id = match node {
Node::ImplItem(_)
| Node::TraitItem(_)
Expand Down Expand Up @@ -229,16 +235,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
// inherit the generics of the item.
Some(parent.to_def_id())
}
ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit generics from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(generics) =
inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
}
None
}
_ => None,
},
_ => None,
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
let hir_id = tcx.local_def_id_to_hir_id(def_id);
let node = tcx.hir_node(hir_id);

if let Some(sig) = node.fn_sig()
&& let Some(sig_id) = sig.decl.opt_delegation_sig_id()
{
return inherit_predicates_for_delegation_item(tcx, def_id, sig_id);
}

let mut is_trait = None;
let mut is_default_impl_trait = None;

Expand All @@ -164,16 +170,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
is_trait = Some(self_bounds);
}

ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit predicates from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(predicates) =
inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
}
}
_ => {}
}
};
Expand Down
Loading

0 comments on commit a445d0a

Please sign in to comment.