Rollup merge of #153705 - aerooneqq:delegation-impl-trait-generics, r=petrochenkov

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

After rust-lang/rust#151864 there is a change in delegation code generation in `trait impl` cases: after rust-lang/rust#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.

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

r? @petrochenkov
This commit is contained in:
Jonathan Brouwer
2026-03-12 15:52:10 +01:00
committed by GitHub
7 changed files with 340 additions and 136 deletions

View File

@@ -725,9 +725,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
result: &mut GenericsGenerationResult<'hir>,
add_lifetimes: bool,
) -> hir::PathSegment<'hir> {
let details = result.generics.args_propagation_details();
// The first condition is needed when there is SelfAndUserSpecified case,
// we don't want to propagate generics params in this situation.
let segment = if !result.generics.is_user_specified()
let segment = if details.should_propagate
&& let Some(args) = result
.generics
.into_hir_generics(self, item_id, span)
@@ -738,7 +740,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
segment.clone()
};
if result.generics.is_user_specified() {
if details.use_args_in_sig_inheritance {
result.args_segment_id = Some(segment.hir_id);
}

View File

@@ -20,6 +20,10 @@ pub(super) enum DelegationGenerics<T> {
/// In free-to-trait reuse, when user specified args for trait `reuse Trait::<i32>::foo;`
/// in this case we need to both generate `Self` and process user args.
SelfAndUserSpecified(Option<T>),
/// In delegations from trait impl to other entities like free functions or trait functions,
/// we want to generate a function whose generics matches generics of signature function
/// in trait.
TraitImpl(Option<T>, bool /* Has user-specified args */),
}
/// Used for storing either AST generics or their lowered HIR version. Firstly we obtain
@@ -48,12 +52,29 @@ pub(super) struct GenericsGenerationResults<'hir> {
pub(super) child: GenericsGenerationResult<'hir>,
}
pub(super) struct GenericArgsPropagationDetails {
pub(super) should_propagate: bool,
pub(super) use_args_in_sig_inheritance: bool,
}
impl<T> DelegationGenerics<T> {
fn is_user_specified(&self) -> bool {
matches!(
self,
DelegationGenerics::UserSpecified | DelegationGenerics::SelfAndUserSpecified { .. }
)
fn args_propagation_details(&self) -> GenericArgsPropagationDetails {
match self {
DelegationGenerics::UserSpecified | DelegationGenerics::SelfAndUserSpecified { .. } => {
GenericArgsPropagationDetails {
should_propagate: false,
use_args_in_sig_inheritance: true,
}
}
DelegationGenerics::TraitImpl(_, user_specified) => GenericArgsPropagationDetails {
should_propagate: !*user_specified,
use_args_in_sig_inheritance: false,
},
DelegationGenerics::Default(_) => GenericArgsPropagationDetails {
should_propagate: true,
use_args_in_sig_inheritance: false,
},
}
}
}
@@ -77,6 +98,12 @@ impl<'hir> HirOrAstGenerics<'hir> {
DelegationGenerics::SelfAndUserSpecified(generics) => {
DelegationGenerics::SelfAndUserSpecified(generics.as_mut().map(process_params))
}
DelegationGenerics::TraitImpl(generics, user_specified) => {
DelegationGenerics::TraitImpl(
generics.as_mut().map(process_params),
*user_specified,
)
}
};
*self = HirOrAstGenerics::Hir(hir_generics);
@@ -91,7 +118,8 @@ impl<'hir> HirOrAstGenerics<'hir> {
HirOrAstGenerics::Hir(hir_generics) => match hir_generics {
DelegationGenerics::UserSpecified => hir::Generics::empty(),
DelegationGenerics::Default(generics)
| DelegationGenerics::SelfAndUserSpecified(generics) => {
| DelegationGenerics::SelfAndUserSpecified(generics)
| DelegationGenerics::TraitImpl(generics, _) => {
generics.unwrap_or(hir::Generics::empty())
}
},
@@ -111,17 +139,18 @@ impl<'hir> HirOrAstGenerics<'hir> {
HirOrAstGenerics::Hir(hir_generics) => match hir_generics {
DelegationGenerics::UserSpecified => None,
DelegationGenerics::Default(generics)
| DelegationGenerics::SelfAndUserSpecified(generics) => generics.map(|generics| {
| DelegationGenerics::SelfAndUserSpecified(generics)
| DelegationGenerics::TraitImpl(generics, _) => generics.map(|generics| {
ctx.create_generics_args_from_params(generics.params, add_lifetimes, span)
}),
},
}
}
pub(super) fn is_user_specified(&self) -> bool {
pub(super) fn args_propagation_details(&self) -> GenericArgsPropagationDetails {
match self {
HirOrAstGenerics::Ast(ast_generics) => ast_generics.is_user_specified(),
HirOrAstGenerics::Hir(hir_generics) => hir_generics.is_user_specified(),
HirOrAstGenerics::Ast(ast_generics) => ast_generics.args_propagation_details(),
HirOrAstGenerics::Hir(hir_generics) => hir_generics.args_propagation_details(),
}
}
}
@@ -215,10 +244,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
item_id: NodeId,
span: Span,
) -> GenericsGenerationResults<'hir> {
let delegation_in_free_ctx = !matches!(
self.tcx.def_kind(self.tcx.local_parent(self.local_def_id(item_id))),
DefKind::Trait | DefKind::Impl { .. }
);
let delegation_parent_kind =
self.tcx.def_kind(self.tcx.local_parent(self.local_def_id(item_id)));
let segments = &delegation.path.segments;
let len = segments.len();
let child_user_specified = segments[len - 1].args.is_some();
// If we are in trait impl always generate function whose generics matches
// those that are defined in trait.
if matches!(delegation_parent_kind, DefKind::Impl { of_trait: true }) {
// Considering parent generics, during signature inheritance
// we will take those args that are in trait impl header trait ref.
let parent = GenericsGenerationResult::new(DelegationGenerics::Default(None));
let generics = self.get_fn_like_generics(root_fn_id, span);
let child = DelegationGenerics::TraitImpl(generics, child_user_specified);
let child = GenericsGenerationResult::new(child);
return GenericsGenerationResults { parent, child };
}
let delegation_in_free_ctx =
!matches!(delegation_parent_kind, DefKind::Trait | DefKind::Impl { .. });
let root_function_in_trait =
matches!(self.tcx.def_kind(self.tcx.parent(root_fn_id)), DefKind::Trait);
@@ -234,9 +282,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
};
let segments = &delegation.path.segments;
let len = segments.len();
let can_add_generics_to_parent = len >= 2
&& self.get_resolution_id(segments[len - 2].id).is_some_and(|def_id| {
matches!(self.tcx.def_kind(def_id), DefKind::Trait | DefKind::TraitAlias)
@@ -256,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
DelegationGenerics::Default(None)
};
let child_generics = if segments[len - 1].args.is_some() {
let child_generics = if child_user_specified {
DelegationGenerics::UserSpecified
} else {
DelegationGenerics::Default(self.get_fn_like_generics(root_fn_id, span))

View File

@@ -339,10 +339,14 @@ fn create_generic_args<'tcx>(
| (FnKind::AssocTrait, FnKind::AssocTrait) => delegation_args,
(FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
// Special case, as user specifies Trait args in impl trait header, we want to treat
// them as parent args.
// 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(delegation_id);
parent_args = tcx.impl_trait_header(parent).trait_ref.instantiate_identity().args;
assert!(child_args.is_empty(), "Child args can not be used in trait impl case");
tcx.mk_args(&delegation_args[delegation_parent_args_count..])
}

View File

@@ -1,40 +0,0 @@
#![feature(fn_delegation)]
#![allow(incomplete_features)]
mod to_reuse {
pub fn bar<'a: 'a, 'b: 'b, A, B>(x: &super::XX) {}
pub fn bar1(x: &super::XX) {}
pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
}
trait Trait<'a, 'b, 'c, A, B, const N: usize>: Sized {
fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
fn bar1<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
fn bar2(&self) {}
fn bar3(&self) {}
fn bar4<X, Y, Z>(&self) {}
}
struct X<'x1, 'x2, 'x3, 'x4, X1, X2, const X3: usize>(
&'x1 X1, &'x2 X2, &'x3 X1, &'x4 [usize; X3]);
type XX = X::<'static, 'static, 'static, 'static, i32, i32, 3>;
impl<'a, 'b, 'c, A, B, const N: usize> Trait<'a, 'b, 'c, A, B, N> for XX {
reuse to_reuse::bar;
//~^ ERROR: function takes at most 2 generic arguments but 3 generic arguments were supplied
reuse to_reuse::bar1;
//~^ ERROR: function takes 0 generic arguments but 3 generic arguments were supplied
reuse to_reuse::bar2;
//~^ ERROR: type annotations needed
//~| ERROR: type annotations needed
reuse to_reuse::bar2::<i32, i32, i32, i32, i32, i32, 123, true> as bar3;
reuse to_reuse::bar2::<i32, i32, i32, i32, i32, i32, 123, true> as bar4;
//~^ ERROR: method `bar4` has 0 type parameters but its trait declaration has 3 type parameters
}
fn main() {
}

View File

@@ -1,74 +0,0 @@
error[E0049]: method `bar4` has 0 type parameters but its trait declaration has 3 type parameters
--> $DIR/impl-trait-wrong-args-count.rs:35:21
|
LL | fn bar4<X, Y, Z>(&self) {}
| - - -
| |
| expected 3 type parameters
...
LL | reuse to_reuse::bar2::<i32, i32, i32, i32, i32, i32, 123, true> as bar4;
| ^^^^ found 0 type parameters
error[E0107]: function takes at most 2 generic arguments but 3 generic arguments were supplied
--> $DIR/impl-trait-wrong-args-count.rs:23:21
|
LL | fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
| ----------------- help: remove the unnecessary generic argument
...
LL | reuse to_reuse::bar;
| ^^^ expected at most 2 generic arguments
|
note: function defined here, with at most 2 generic parameters: `A`, `B`
--> $DIR/impl-trait-wrong-args-count.rs:5:12
|
LL | pub fn bar<'a: 'a, 'b: 'b, A, B>(x: &super::XX) {}
| ^^^ - -
error[E0107]: function takes 0 generic arguments but 3 generic arguments were supplied
--> $DIR/impl-trait-wrong-args-count.rs:26:21
|
LL | reuse to_reuse::bar1;
| ^^^^ expected 0 generic arguments
|
note: function defined here, with 0 generic parameters
--> $DIR/impl-trait-wrong-args-count.rs:6:12
|
LL | pub fn bar1(x: &super::XX) {}
| ^^^^
error[E0284]: type annotations needed
--> $DIR/impl-trait-wrong-args-count.rs:29:21
|
LL | reuse to_reuse::bar2;
| ^^^^ cannot infer the value of the const parameter `X` declared on the function `bar2`
|
note: required by a const generic parameter in `bar2`
--> $DIR/impl-trait-wrong-args-count.rs:7:35
|
LL | pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
| ^^^^^^^^^^^^^^ required by this const generic parameter in `bar2`
help: consider specifying the generic arguments
|
LL | reuse to_reuse::bar2::<A, B, C, D, E, F, X, Y>;
| ++++++++++++++++++++++++++
error[E0284]: type annotations needed
--> $DIR/impl-trait-wrong-args-count.rs:29:21
|
LL | reuse to_reuse::bar2;
| ^^^^ cannot infer the value of the const parameter `Y` declared on the function `bar2`
|
note: required by a const generic parameter in `bar2`
--> $DIR/impl-trait-wrong-args-count.rs:7:51
|
LL | pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
| ^^^^^^^^^^^^^ required by this const generic parameter in `bar2`
help: consider specifying the generic arguments
|
LL | reuse to_reuse::bar2::<A, B, C, D, E, F, X, Y>;
| ++++++++++++++++++++++++++
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0049, E0107, E0284.
For more information about an error, try `rustc --explain E0049`.

View File

@@ -0,0 +1,111 @@
#![feature(fn_delegation)]
#![allow(incomplete_features)]
// Testing delegation from trait impl to free functions.
mod test_1 {
mod to_reuse {
pub fn bar<'a: 'a, 'b: 'b, A, B>(x: &super::XX) {}
pub fn bar1(x: &super::XX) {}
pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
}
trait Trait<'a, 'b, 'c, A, B, const N: usize>: Sized {
fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
fn bar1<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
fn bar2(&self) {}
fn bar3(&self) {}
fn bar4<X, Y, Z>(&self) {}
}
struct X<'x1, 'x2, 'x3, 'x4, X1, X2, const X3: usize>(
&'x1 X1, &'x2 X2, &'x3 X1, &'x4 [usize; X3]);
type XX = X::<'static, 'static, 'static, 'static, i32, i32, 3>;
impl<'a, 'b, 'c, A, B, const N: usize> Trait<'a, 'b, 'c, A, B, N> for XX {
reuse to_reuse::bar;
//~^ ERROR: function takes at most 2 generic arguments but 3 generic arguments were supplied
reuse to_reuse::bar1;
//~^ ERROR: function takes 0 generic arguments but 3 generic arguments were supplied
reuse to_reuse::bar2;
//~^ ERROR: type annotations needed
//~| ERROR: type annotations needed
reuse to_reuse::bar2::<i32, i32, i32, i32, i32, i32, 123, true> as bar3;
reuse to_reuse::bar2::<i32, i32, i32, i32, i32, i32, 123, true> as bar4;
}
}
// Testing delegations of trait impl to other different trait
// with errors in Trait1 generics count.
mod test_2 {
trait Trait<A, const N: usize> {
fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>() {}
fn bar1<'x: 'x, 'y: 'y, AA, BB, const NN: usize>() {}
fn bar2() {}
fn bar3() {}
fn bar4<X, Y, Z>() {}
}
trait Trait1<A, B> {
fn bar<'x: 'x, AA, BB, const NN: usize>() {}
}
struct X;
impl<A, B> Trait1<A, B> for X {}
impl Trait<String, 1> for X {
reuse <X as Trait1>::bar;
//~^ ERROR: missing generics for trait
reuse <X as Trait1::<bool, bool>>::bar as bar1;
reuse <X as Trait1::<bool, bool>>::bar::<'static, u32, u32, 1> as bar2;
reuse <X as Trait1>::bar::<'static, u32, u32, 1> as bar3;
//~^ ERROR: missing generics for trait
reuse <X as Trait1>::bar as bar4;
//~^ ERROR: missing generics for trait
}
}
// Testing delegations of trait impl to other different trait
// with Trait1::bar and Trait1::foo wrong generics count.
mod test_3 {
trait Trait<A, const N: usize> {
fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>() {}
fn bar1<'x: 'x, 'y: 'y, AA, BB, const NN: usize>() {}
fn bar2() {}
fn bar3<X, Y, Z>() {}
}
trait Trait1<A, B> {
fn bar() {}
fn foo<X, Y>() {}
}
struct X;
impl<A, B> Trait1<A, B> for X {}
impl Trait<String, 1> for X {
reuse <X as Trait1::<(), ()>>::bar;
//~^ ERROR: associated function takes 0 generic arguments but 3 generic arguments were supplied
reuse <X as Trait1::<(), ()>>::bar as bar1;
//~^ ERROR: associated function takes 0 generic arguments but 3 generic arguments were supplied
reuse <X as Trait1::<(), ()>>::foo as bar2;
//~^ ERROR: type annotations needed
reuse <X as Trait1::<(), ()>>::foo as bar3;
//~^ ERROR: associated function takes at most 2 generic arguments but 3 generic arguments were supplied
}
}
fn main() {
}

View File

@@ -0,0 +1,156 @@
error[E0107]: function takes at most 2 generic arguments but 3 generic arguments were supplied
--> $DIR/trait-impl-wrong-args-count.rs:25:25
|
LL | fn bar<'x: 'x, 'y: 'y, AA, BB, const NN: usize>(&self) {}
| ----------------- help: remove the unnecessary generic argument
...
LL | reuse to_reuse::bar;
| ^^^ expected at most 2 generic arguments
|
note: function defined here, with at most 2 generic parameters: `A`, `B`
--> $DIR/trait-impl-wrong-args-count.rs:7:16
|
LL | pub fn bar<'a: 'a, 'b: 'b, A, B>(x: &super::XX) {}
| ^^^ - -
error[E0107]: function takes 0 generic arguments but 3 generic arguments were supplied
--> $DIR/trait-impl-wrong-args-count.rs:28:25
|
LL | reuse to_reuse::bar1;
| ^^^^ expected 0 generic arguments
|
note: function defined here, with 0 generic parameters
--> $DIR/trait-impl-wrong-args-count.rs:8:16
|
LL | pub fn bar1(x: &super::XX) {}
| ^^^^
error[E0284]: type annotations needed
--> $DIR/trait-impl-wrong-args-count.rs:31:25
|
LL | reuse to_reuse::bar2;
| ^^^^ cannot infer the value of the const parameter `X` declared on the function `bar2`
|
note: required by a const generic parameter in `bar2`
--> $DIR/trait-impl-wrong-args-count.rs:9:39
|
LL | pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
| ^^^^^^^^^^^^^^ required by this const generic parameter in `bar2`
help: consider specifying the generic arguments
|
LL | reuse to_reuse::bar2::<A, B, C, D, E, F, X, Y>;
| ++++++++++++++++++++++++++
error[E0284]: type annotations needed
--> $DIR/trait-impl-wrong-args-count.rs:31:25
|
LL | reuse to_reuse::bar2;
| ^^^^ cannot infer the value of the const parameter `Y` declared on the function `bar2`
|
note: required by a const generic parameter in `bar2`
--> $DIR/trait-impl-wrong-args-count.rs:9:55
|
LL | pub fn bar2<A, B, C, D, E, F, const X: usize, const Y: bool>(x: &super::XX) {}
| ^^^^^^^^^^^^^ required by this const generic parameter in `bar2`
help: consider specifying the generic arguments
|
LL | reuse to_reuse::bar2::<A, B, C, D, E, F, X, Y>;
| ++++++++++++++++++++++++++
error[E0107]: missing generics for trait `test_2::Trait1`
--> $DIR/trait-impl-wrong-args-count.rs:61:21
|
LL | reuse <X as Trait1>::bar;
| ^^^^^^ expected 2 generic arguments
|
note: trait defined here, with 2 generic parameters: `A`, `B`
--> $DIR/trait-impl-wrong-args-count.rs:52:11
|
LL | trait Trait1<A, B> {
| ^^^^^^ - -
help: add missing generic arguments
|
LL | reuse <X as Trait1<A, B>>::bar;
| ++++++
error[E0107]: missing generics for trait `test_2::Trait1`
--> $DIR/trait-impl-wrong-args-count.rs:68:21
|
LL | reuse <X as Trait1>::bar::<'static, u32, u32, 1> as bar3;
| ^^^^^^ expected 2 generic arguments
|
note: trait defined here, with 2 generic parameters: `A`, `B`
--> $DIR/trait-impl-wrong-args-count.rs:52:11
|
LL | trait Trait1<A, B> {
| ^^^^^^ - -
help: add missing generic arguments
|
LL | reuse <X as Trait1<A, B>>::bar::<'static, u32, u32, 1> as bar3;
| ++++++
error[E0107]: missing generics for trait `test_2::Trait1`
--> $DIR/trait-impl-wrong-args-count.rs:71:21
|
LL | reuse <X as Trait1>::bar as bar4;
| ^^^^^^ expected 2 generic arguments
|
note: trait defined here, with 2 generic parameters: `A`, `B`
--> $DIR/trait-impl-wrong-args-count.rs:52:11
|
LL | trait Trait1<A, B> {
| ^^^^^^ - -
help: add missing generic arguments
|
LL | reuse <X as Trait1<A, B>>::bar as bar4;
| ++++++
error[E0107]: associated function takes 0 generic arguments but 3 generic arguments were supplied
--> $DIR/trait-impl-wrong-args-count.rs:96:40
|
LL | reuse <X as Trait1::<(), ()>>::bar;
| ^^^ expected 0 generic arguments
|
note: associated function defined here, with 0 generic parameters
--> $DIR/trait-impl-wrong-args-count.rs:87:12
|
LL | fn bar() {}
| ^^^
error[E0107]: associated function takes 0 generic arguments but 3 generic arguments were supplied
--> $DIR/trait-impl-wrong-args-count.rs:99:40
|
LL | reuse <X as Trait1::<(), ()>>::bar as bar1;
| ^^^ expected 0 generic arguments
|
note: associated function defined here, with 0 generic parameters
--> $DIR/trait-impl-wrong-args-count.rs:87:12
|
LL | fn bar() {}
| ^^^
error[E0282]: type annotations needed
--> $DIR/trait-impl-wrong-args-count.rs:102:40
|
LL | reuse <X as Trait1::<(), ()>>::foo as bar2;
| ^^^ cannot infer type of the type parameter `X` declared on the associated function `foo`
error[E0107]: associated function takes at most 2 generic arguments but 3 generic arguments were supplied
--> $DIR/trait-impl-wrong-args-count.rs:105:40
|
LL | fn bar3<X, Y, Z>() {}
| --- help: remove the unnecessary generic argument
...
LL | reuse <X as Trait1::<(), ()>>::foo as bar3;
| ^^^ expected at most 2 generic arguments
|
note: associated function defined here, with at most 2 generic parameters: `X`, `Y`
--> $DIR/trait-impl-wrong-args-count.rs:88:12
|
LL | fn foo<X, Y>() {}
| ^^^ - -
error: aborting due to 11 previous errors
Some errors have detailed explanations: E0107, E0282, E0284.
For more information about an error, try `rustc --explain E0107`.