delegation: add support for infers in generics#157960
Conversation
|
HIR ty lowering was modified cc @fmease |
| (kw::Underscore, kw::UnderscoreLifetime) | ||
| }; | ||
|
|
||
| self.tcx.dcx().emit_err(DelegationInfersMismatch { |
There was a problem hiding this comment.
Do we need this diagnostic? Other option is to skip incorrect infers and error will be emitted later during signature inheritance.
| let generate_self = free_to_trait_delegation && is_method && delegation.qself.is_none(); | ||
|
|
||
| let qself_is_infer = | ||
| delegation.qself.as_ref().is_some_and(|qself| qself.ty.is_maybe_parenthesised_infer()); |
There was a problem hiding this comment.
Do we need to care about ast::TyKind::Paren or we can just use matches!(ty.kind, ast::TyKind::Infer)?
7d84842 to
1da6864
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| //~| ERROR: function takes 3 generic arguments but 2 generic arguments were supplied | ||
| //~| ERROR: inferred lifetimes are not allowed in delegations as we need to inherit signature | ||
| //~| ERROR: function takes 3 generic arguments but 1 generic argument was supplied | ||
| //~| ERROR: function takes 2 lifetime arguments but 6 lifetime arguments were supplied |
There was a problem hiding this comment.
Nit: the annotations were reordered, and it makes the changes look larger than they actually are.
(Same below.)
| #[derive(Debug, Clone, Copy, PartialEq, Eq, StableHash)] | ||
| pub enum DelegationSelfTyPropagationKind { | ||
| SelfTy(HirId /* Self ty id */), | ||
| SelfParam, |
There was a problem hiding this comment.
Could you add comments telling what the variants mean?
| pub propagate_self_ty: bool, | ||
| pub child_seg_id: HirId, | ||
| pub parent_seg_id_for_sig: Option<HirId>, | ||
| pub child_seg_id_for_sig: Option<HirId>, |
There was a problem hiding this comment.
Could you add comments telling what the fields mean?
E.g. what is the difference between child_seg_id and child_seg_id_for_sig.
|
(I started reviewing, will continue tomorrow.) |
This PR adds support for generating generic params for lifetimes and types/consts infers (
'_._). We support only single infers, if they are nested then we do nothing and eventually an error will be emitted after inheriting this unsound signature (i.e.,reuse foo::<Vec<_>>is not supported).The basic idea is:
So we generated params and lifetimes for provided infers. Note that in case of lifetimes we may have early and late bound lifetimes in child segment. We process only early-bound lifetimes as they are present in signature function generics (
tcx.generics_of(sig_id)). Moreover since this PR we started to explicitly propagate early-bound lifetimes in generated delegation's call, so the warning about specifying lifetimes when late-bound lifetimes are present is suppressed for delegation segments.Next, we limit the number of processed infers with the number of generic params in the signature function, so if we have
fn foo<X, Y>() {}and we wrotereuse foo::<_, _, _, _>;we will not generate 4 generic params in delegation, we will generate 2 (XandY), two remaining infers will not be processed and this will result in an error.Considering free-to-trait reuses this PR extends the number of supported cases, as now we always generate
Selfgeneric param when needed:Note that we generated
Selfinfoo5reuse. With that done the error about self-type specification is removed.Finally this PR greatly simplifies generic args for signature inheritance generation code.
Part of #118212.
r? @petrochenkov