Skip to content

delegation: generate only used parent generics in method call scenario#155906

Open
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:delegation-generics-propagation-method-call-generics-2
Open

delegation: generate only used parent generics in method call scenario#155906
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:delegation-generics-propagation-method-call-generics-2

Conversation

@aerooneqq
Copy link
Copy Markdown
Contributor

@aerooneqq aerooneqq commented Apr 28, 2026

This PR supports generating only used parent generics in method call scenario. At the current state we always generated all generic params in method call scenario, even if some of them were not used in signature or predicates. Now we determine the set of generic params which are either used in function signature or in predicates and generate only them.

trait Bound<B> {}
trait Trait<'a, 'b: 'static + 'a, A: Bound<B>, B, C> {
    fn foo<U, const M: bool>(&self, b: &'b str, f: impl FnOnce() -> usize) {}
}

impl<'a, 'b: 'a + 'static, A: Bound<B>, B, C> Trait<'a, 'b, A, B, C> for () {}

struct X;
impl X {
  fn get_self() -> () {
      ()
  }
  
  // `C` is not generated so type annotation needed error.
  reuse Trait::foo { Self::get_self() }

  // Desugaring
  // `C` is not generated so type annotation needed error. Also `A` and `B` are not generated.
  #[attr = Inline(Hint)]
  fn foo<'a, 'b, U, const M: _,
      impl FnOnce() -> usize>(self: _, arg1: _, arg2: _) -> _ where
      'a:'a, 'b:'b { Self::get_self().foo::<U, M>(arg1, arg2) }
}

As we see from an example we did not generate C param (also A and B) as it is not used in signature and predicates, but we generated lifetimes a and b as b is used in signature (b: &'b str) and a is used in b's predicate ('b: 'static + 'a).

If we use A param somewhere in foo function (i.e., instead of f: impl FnOnce() -> usize we would have f: impl FnOnce() -> A), then we generate both A and B, as B participates in predicate A: Bound<B>.

Also this PR finally removed into_hir_generics calls from all_params and all_predicates, now it works as designed.

Part of #118212.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 28, 2026
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Apr 28, 2026
@petrochenkov
Copy link
Copy Markdown
Contributor

We talked with @aerooneqq and decided to try always lowering into free function calls instead of method calls, this way we'll always be able to propagate generics.
Lowering to method calls was used to quickly get the autoref/autoderef (and two phase borrowing) behavior for the first argument, but we should be able to provide the same behavior for free function calls as well by some flags in type checker.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 28, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 28, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 30, 2026

☔ The latest upstream changes (presumably #155018) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants