Skip to content

Commit c156165

Browse files
Fix late-bound lifetime warning
PR #35 introduced a warning in one of the examples. We were passing all generic parameters to the inner function, including lifetimes. But passing late-bound lifetimes won't be allowed in the future. This commit simply ignores lifetime parameters. AFAICT this should never be a problem. Passing lifetimes explicitly hardly ever makes sense. The only case I can think of where the changes of this commit could break anything is in this function: fn foo<'a>() -> &'a i32 { ... } But functions with an early-bound lifetime (return type) and no late-bound ones are extremely rare and special. So for now we can ignore them. In particular, determining which lifetime parameters are early- and which are late-bound is extremely difficult. Lastly, const parameters are not yet correctly passed to the inner function. This needs to be fixed in the future, when const parameters are a thing.
1 parent f4b553b commit c156165

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/gen.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,19 @@ fn gen_method_item(
430430
// Generate the list of argument used to call the method.
431431
let args = get_arg_list(sig.decl.inputs.iter())?;
432432

433-
// Builds turbofish with generic types
434-
let (_, generic_types, _) = sig.decl.generics.split_for_impl();
435-
let generic_types = generic_types.as_turbofish();
433+
// Builds turbofish with generic types (without lifetimes)
434+
let generic_types = sig.decl.generics
435+
.type_params()
436+
.map(|param| {
437+
let name = &param.ident;
438+
quote! { #name , }
439+
})
440+
.collect::<TokenStream2>();
441+
let generic_types = if generic_types.is_empty() {
442+
generic_types
443+
} else {
444+
quote ! { ::<#generic_types> }
445+
};
436446

437447
// Generate the body of the function. This mainly depends on the self type,
438448
// but also on the proxy type.
@@ -452,14 +462,14 @@ fn gen_method_item(
452462
// Receiver `self` (by value)
453463
SelfType::Value => {
454464
// The proxy type is a Box.
455-
quote! { (*self).#name#generic_types(#args) }
465+
quote! { (*self).#name #generic_types(#args) }
456466
}
457467

458468
// `&self` or `&mut self` receiver
459469
SelfType::Ref | SelfType::Mut => {
460470
// The proxy type could be anything in the `Ref` case, and `&mut`
461471
// or Box in the `Mut` case.
462-
quote! { (*self).#name#generic_types(#args) }
472+
quote! { (*self).#name #generic_types(#args) }
463473
}
464474
};
465475

0 commit comments

Comments
 (0)