@@ -264,34 +264,34 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
264
264
}
265
265
} ;
266
266
267
- let impl_struct : syn:: ItemImpl = syn:: parse_quote! {
268
- impl #struct_name {
269
- // TODO(nga): copy lifetime parameter from declaration,
270
- // so the warning would be precise.
271
- # [ allow ( clippy :: extra_unused_lifetimes ) ]
272
- # ( #attrs ) *
273
- fn invoke_impl< ' v> (
274
- # ( #invoke_params , ) *
275
- ) -> #return_type {
276
- #body
277
- }
267
+ let item_invoke_impl : syn:: ItemFn = syn:: parse_quote! {
268
+ // TODO(nga): copy lifetime parameter from declaration,
269
+ // so the warning would be precise.
270
+ # [ allow ( clippy :: extra_unused_lifetimes ) ]
271
+ # ( #attrs ) *
272
+ fn __starlark_invoke_impl< ' v> (
273
+ # ( #invoke_params , ) *
274
+ ) -> #return_type {
275
+ #body
276
+ }
277
+ } ;
278
278
279
- // When function signature declares return type as `anyhow::Result<impl AllocValue>`,
280
- // we cannot call `T::starlark_type_repr` to render documentation, because there's no T.
281
- // Future Rust will provide syntax `type ReturnType = impl AllocValue`:
282
- // https://github.com/rust-lang/rfcs/pull/2515
283
- // Until then we use this hack as a workaround.
284
- #[ allow( dead_code) ] // Function is not used when return type is specified explicitly.
285
- fn return_type_starlark_type_repr( ) -> starlark:: typing:: Ty {
286
- fn get_impl<' v, T : starlark:: values:: AllocValue <' v>, E >(
287
- _f: fn (
288
- #( #param_types, ) *
289
- ) -> std:: result:: Result <T , E >,
290
- ) -> starlark:: typing:: Ty {
291
- <T as starlark:: values:: type_repr:: StarlarkTypeRepr >:: starlark_type_repr( )
292
- }
293
- get_impl( Self :: invoke_impl)
279
+ let item_return_type_repr: syn:: ItemFn = syn:: parse_quote! {
280
+ // When function signature declares return type as `anyhow::Result<impl AllocValue>`,
281
+ // we cannot call `T::starlark_type_repr` to render documentation, because there's no T.
282
+ // Future Rust will provide syntax `type ReturnType = impl AllocValue`:
283
+ // https://github.com/rust-lang/rfcs/pull/2515
284
+ // Until then we use this hack as a workaround.
285
+ #[ allow( dead_code) ] // Function is not used when return type is specified explicitly.
286
+ fn __starlark_return_type_starlark_type_repr( ) -> starlark:: typing:: Ty {
287
+ fn get_impl<' v, T : starlark:: values:: AllocValue <' v>, E >(
288
+ _f: fn (
289
+ #( #param_types, ) *
290
+ ) -> std:: result:: Result <T , E >,
291
+ ) -> starlark:: typing:: Ty {
292
+ <T as starlark:: values:: type_repr:: StarlarkTypeRepr >:: starlark_type_repr( )
294
293
}
294
+ get_impl( __starlark_invoke_impl)
295
295
}
296
296
} ;
297
297
@@ -306,7 +306,7 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
306
306
) -> starlark:: Result <starlark:: values:: Value <' v>> {
307
307
#this_prepare
308
308
#prepare
309
- match Self :: invoke_impl ( #( #invoke_args, ) * ) {
309
+ match __starlark_invoke_impl ( #( #invoke_args, ) * ) {
310
310
Ok ( v) => Ok ( eval. heap( ) . alloc( v) ) ,
311
311
Err ( e) => Err ( starlark:: __derive_refs:: invoke_macro_error:: InvokeMacroError :: into_starlark_error( e) ) ,
312
312
}
@@ -317,7 +317,8 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
317
317
Ok ( syn:: parse_quote! {
318
318
{
319
319
#struct_def
320
- #impl_struct
320
+ #item_invoke_impl
321
+ #item_return_type_repr
321
322
#impl_trait
322
323
#builder_set
323
324
}
@@ -541,13 +542,6 @@ fn render_starlark_type(typ: &syn::Type) -> syn::Expr {
541
542
}
542
543
}
543
544
544
- fn render_starlark_return_type ( fun : & StarFun ) -> syn:: Expr {
545
- let struct_name = fun. struct_name ( ) ;
546
- syn:: parse_quote! {
547
- #struct_name:: return_type_starlark_type_repr( )
548
- }
549
- }
550
-
551
545
fn render_regular_native_callable_param ( arg : & StarArg ) -> syn:: Result < syn:: Expr > {
552
546
let ty = render_starlark_type ( arg. without_option ( ) ) ;
553
547
let name_str = ident_string ( & arg. param . ident ) ;
@@ -656,7 +650,6 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
656
650
}
657
651
} ;
658
652
659
- let return_type_str = render_starlark_return_type ( x) ;
660
653
let speculative_exec_safe = x. speculative_exec_safe ;
661
654
Ok ( quote ! (
662
655
{
@@ -665,7 +658,7 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
665
658
speculative_exec_safe: #speculative_exec_safe,
666
659
rust_docstring: #docs,
667
660
param_spec,
668
- return_type: #return_type_str ,
661
+ return_type: __starlark_return_type_starlark_type_repr ( ) ,
669
662
}
670
663
}
671
664
) )
0 commit comments