@@ -33,6 +33,7 @@ use crate::module::typ::StarArg;
33
33
use crate :: module:: typ:: StarArgSource ;
34
34
use crate :: module:: typ:: StarFun ;
35
35
use crate :: module:: typ:: StarFunSource ;
36
+ use crate :: module:: typ:: StarGenerics ;
36
37
use crate :: module:: util:: ident_string;
37
38
38
39
impl StarFun {
@@ -143,11 +144,13 @@ impl StarFun {
143
144
}
144
145
145
146
/// Globals builder call to register the function.
146
- fn builder_set ( & self ) -> syn:: Result < syn:: Stmt > {
147
+ fn builder_set ( & self , generics : & StarGenerics ) -> syn:: Result < syn:: Stmt > {
147
148
let name_str = self . name_str ( ) ;
148
- let components = render_native_callable_components ( self ) ?;
149
+ let components = render_native_callable_components ( self , generics ) ?;
149
150
let param_spec = render_signature ( self ) ?;
150
151
152
+ let turbofish = generics. turbofish ( ) ;
153
+
151
154
let special_builtin_function = self . special_builtin_function_expr ( ) ;
152
155
153
156
if self . is_method ( ) {
@@ -169,7 +172,7 @@ impl StarFun {
169
172
#name_str,
170
173
#components,
171
174
#param_spec,
172
- __starlark_invoke_outer,
175
+ __starlark_invoke_outer #turbofish ,
173
176
) ;
174
177
} )
175
178
} else {
@@ -184,20 +187,24 @@ impl StarFun {
184
187
#as_type,
185
188
#ty_custom,
186
189
#special_builtin_function,
187
- __starlark_invoke_outer,
190
+ __starlark_invoke_outer #turbofish ,
188
191
) ;
189
192
} )
190
193
}
191
194
}
192
195
}
193
196
194
- pub ( crate ) fn render_fun ( x : StarFun ) -> syn:: Result < syn:: Stmt > {
197
+ pub ( crate ) fn render_fun ( x : StarFun , generics : & StarGenerics ) -> syn:: Result < syn:: Stmt > {
198
+ let generic_decls = generics. decls ( ) ;
199
+ let where_clause = generics. where_clause ( ) ;
200
+ let turbofish = generics. turbofish ( ) ;
201
+
195
202
let ( this_outer_param, this_inner_param, this_prepare, this_arg) = x. this_param_arg ( ) ;
196
203
let ( eval_param, eval_arg) = x. eval_param_arg ( ) ;
197
204
let ( heap_param, heap_arg) = x. heap_param_arg ( ) ;
198
205
let ( binding_params, prepare, binding_args) = x. binding_params_arg ( ) ?;
199
206
200
- let builder_set = x. builder_set ( ) ?;
207
+ let builder_set = x. builder_set ( generics ) ?;
201
208
202
209
let StarFun {
203
210
attrs,
@@ -228,9 +235,9 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
228
235
// so the warning would be precise.
229
236
#[ allow( clippy:: extra_unused_lifetimes) ]
230
237
#( #attrs ) *
231
- fn __starlark_invoke_impl< ' v> (
238
+ fn __starlark_invoke_impl #generic_decls (
232
239
#( #invoke_params, ) *
233
- ) -> #return_type {
240
+ ) -> #return_type #where_clause {
234
241
#body
235
242
}
236
243
} ;
@@ -242,29 +249,29 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
242
249
// https://github.com/rust-lang/rfcs/pull/2515
243
250
// Until then we use this hack as a workaround.
244
251
#[ allow( dead_code) ] // Function is not used when return type is specified explicitly.
245
- fn __starlark_return_type_starlark_type_repr( ) -> starlark:: typing:: Ty {
252
+ fn __starlark_return_type_starlark_type_repr #generic_decls ( ) -> starlark:: typing:: Ty #where_clause {
246
253
fn get_impl<' v, T : starlark:: values:: AllocValue <' v>, E >(
247
254
_f: fn (
248
255
#( #param_types, ) *
249
256
) -> std:: result:: Result <T , E >,
250
257
) -> starlark:: typing:: Ty {
251
258
<T as starlark:: values:: type_repr:: StarlarkTypeRepr >:: starlark_type_repr( )
252
259
}
253
- get_impl( __starlark_invoke_impl)
260
+ get_impl( __starlark_invoke_impl #turbofish )
254
261
}
255
262
} ;
256
263
257
264
let impl_invoke_outer: syn:: ItemFn = syn:: parse_quote! {
258
265
#[ allow( non_snake_case) ] // Starlark doesn't have this convention
259
- fn __starlark_invoke_outer< ' v> (
266
+ fn __starlark_invoke_outer #generic_decls (
260
267
eval: & mut starlark:: eval:: Evaluator <' v, ' _, ' _>,
261
268
#( #this_outer_param, ) *
262
269
signature: & starlark:: eval:: ParametersSpec <starlark:: values:: FrozenValue >,
263
270
parameters: & starlark:: eval:: Arguments <' v, ' _>,
264
- ) -> starlark:: Result <starlark:: values:: Value <' v>> {
271
+ ) -> starlark:: Result <starlark:: values:: Value <' v>> #where_clause {
265
272
#this_prepare
266
273
#prepare
267
- match __starlark_invoke_impl( #( #invoke_args, ) * ) {
274
+ match __starlark_invoke_impl #turbofish ( #( #invoke_args, ) * ) {
268
275
Ok ( v) => Ok ( eval. heap( ) . alloc( v) ) ,
269
276
Err ( e) => Err ( starlark:: __derive_refs:: invoke_macro_error:: InvokeMacroError :: into_starlark_error( e) ) ,
270
277
}
@@ -486,20 +493,26 @@ fn render_option(expr: Option<syn::Expr>) -> syn::Expr {
486
493
}
487
494
}
488
495
489
- fn render_starlark_type ( typ : & syn:: Type ) -> syn:: Expr {
496
+ fn render_starlark_type ( typ : & syn:: Type , generics : & StarGenerics ) -> syn:: Expr {
497
+ let decls = generics. decls ( ) ;
498
+ let turbofish = generics. turbofish ( ) ;
499
+ let where_clause = generics. where_clause ( ) ;
490
500
syn:: parse_quote! {
491
501
{
492
502
#[ allow( clippy:: extra_unused_lifetimes) ]
493
- fn get_type_string< ' v> ( ) -> starlark:: typing:: Ty {
503
+ fn get_type_string #decls ( ) -> starlark:: typing:: Ty #where_clause {
494
504
<#typ as starlark:: values:: type_repr:: StarlarkTypeRepr >:: starlark_type_repr( )
495
505
}
496
- get_type_string( )
506
+ get_type_string #turbofish ( )
497
507
}
498
508
}
499
509
}
500
510
501
- fn render_regular_native_callable_param ( arg : & StarArg ) -> syn:: Result < syn:: Expr > {
502
- let ty = render_starlark_type ( arg. without_option ( ) ) ;
511
+ fn render_regular_native_callable_param (
512
+ arg : & StarArg ,
513
+ generics : & StarGenerics ,
514
+ ) -> syn:: Result < syn:: Expr > {
515
+ let ty = render_starlark_type ( arg. without_option ( ) , generics) ;
503
516
let name_str = ident_string ( & arg. param . ident ) ;
504
517
let required: syn:: Expr = match ( & arg. default , arg. is_option ( ) ) {
505
518
( Some ( _) , true ) => {
@@ -541,7 +554,10 @@ fn render_regular_native_callable_param(arg: &StarArg) -> syn::Result<syn::Expr>
541
554
} )
542
555
}
543
556
544
- fn render_native_callable_components ( x : & StarFun ) -> syn:: Result < TokenStream > {
557
+ fn render_native_callable_components (
558
+ x : & StarFun ,
559
+ generics : & StarGenerics ,
560
+ ) -> syn:: Result < TokenStream > {
545
561
let docs = match x. docstring . as_ref ( ) {
546
562
Some ( d) => quote ! ( Some ( #d) ) ,
547
563
None => quote ! ( None ) ,
@@ -565,28 +581,28 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
565
581
let pos_only: Vec < syn:: Expr > = pos_only
566
582
. iter ( )
567
583
. copied ( )
568
- . map ( render_regular_native_callable_param)
584
+ . map ( |a| render_regular_native_callable_param ( a , generics ) )
569
585
. collect :: < syn:: Result < Vec < _ > > > ( ) ?;
570
586
let pos_or_named: Vec < syn:: Expr > = pos_or_named
571
587
. iter ( )
572
588
. copied ( )
573
- . map ( render_regular_native_callable_param)
589
+ . map ( |a| render_regular_native_callable_param ( a , generics ) )
574
590
. collect :: < syn:: Result < Vec < _ > > > ( ) ?;
575
591
let args: Option < syn:: Expr > = args. map ( |arg| {
576
592
let name_str = ident_string ( & arg. param . ident ) ;
577
- let ty = render_starlark_type ( & arg. param . ty ) ;
593
+ let ty = render_starlark_type ( & arg. param . ty , generics ) ;
578
594
syn:: parse_quote! {
579
595
starlark:: __derive_refs:: param_spec:: NativeCallableParam :: args( #name_str, #ty)
580
596
}
581
597
} ) ;
582
598
let named_only: Vec < syn:: Expr > = named_only
583
599
. iter ( )
584
600
. copied ( )
585
- . map ( render_regular_native_callable_param)
601
+ . map ( |a| render_regular_native_callable_param ( a , generics ) )
586
602
. collect :: < syn:: Result < Vec < _ > > > ( ) ?;
587
603
let kwargs: Option < syn:: Expr > = kwargs. map ( |arg| {
588
604
let name_str = ident_string ( & arg. param . ident ) ;
589
- let ty = render_starlark_type ( & arg. param . ty ) ;
605
+ let ty = render_starlark_type ( & arg. param . ty , generics ) ;
590
606
syn:: parse_quote! {
591
607
starlark:: __derive_refs:: param_spec:: NativeCallableParam :: kwargs( #name_str, #ty)
592
608
}
@@ -606,6 +622,8 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
606
622
}
607
623
} ;
608
624
625
+ let turbofish = generics. turbofish ( ) ;
626
+
609
627
let speculative_exec_safe = x. speculative_exec_safe ;
610
628
Ok ( quote ! (
611
629
{
@@ -614,7 +632,7 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
614
632
speculative_exec_safe: #speculative_exec_safe,
615
633
rust_docstring: #docs,
616
634
param_spec,
617
- return_type: __starlark_return_type_starlark_type_repr( ) ,
635
+ return_type: __starlark_return_type_starlark_type_repr #turbofish ( ) ,
618
636
}
619
637
}
620
638
) )
0 commit comments