Skip to content

Always generate generics in delegation that match trait in trait impl scenario#153705

Open
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:delegation-impl-trait-generics
Open

Always generate generics in delegation that match trait in trait impl scenario#153705
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:delegation-impl-trait-generics

Conversation

@aerooneqq
Copy link
Contributor

@aerooneqq aerooneqq commented Mar 11, 2026

After #151864 there is a change in delegation code generation in trait impl cases: after #151864 we started to look at user-specified args and generate functions, whose generics may not match the signature of the function that is defined in trait. Such handling of delegation from trait impl is not correct, as the generated function should always have the same generics as its signature function in trait.
Considering propagation of generic params, in this PR we do it only if delegation path resolution matches the signature function, in other cases it is not obvious should we propagate generic params or not. As one option we may check generics of delegation path, and if they match generics of signature trait function then propagate them, but I think it is out of scope of this PR for now.

This addresses the "Fix generic params generation in trait impl case" future work from #151864

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 Mar 11, 2026
@rust-log-analyzer

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the delegation-impl-trait-generics branch from f413a06 to 23a51f5 Compare March 11, 2026 10:33
#![allow(incomplete_features)]
#![allow(late_bound_lifetime_arguments)]

mod test_1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comments to every test case, similarly to the previous PR?

Hir(DelegationGenerics<&'hir hir::Generics<'hir>>),
}

pub(super) enum PropagationKind {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment telling what is being propagated here?

}
}

pub(super) fn can_propagate(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial method, used once, doesn't access anything private, can be inlined.

// method call will be supported (if HIR generics were not obtained
// then it means that we did not propagated them, thus we do not need
// to generate params).
// FIXME(fn_delegation): in impl trait case we still need to generate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"impl trait" usually means impl Trait like in fn foo() -> impl Trait { ... }.
impl Trait for Type { ... } is a "trait impl".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In PR title and description too.)

let prop_kind = if res.is_some_and(|def_id| def_id == root_fn_id) {
PropagationKind::Allowed
} else {
PropagationKind::Forbidden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the diagnostics are very bad.
Perhaps it's better to always propagate, but get some "wrong number of passed generic arguments" errors instead of "type annotations needed"? (At least when the generic arguments are not user-specified?)

@petrochenkov petrochenkov 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 Mar 11, 2026
@aerooneqq aerooneqq changed the title Always generate generics in delegation that match trait in impl trait scenario Always generate generics in delegation that match trait in trait impl scenario Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants