Skip to content

Commit d5ffb22

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
module: Free two generated functions
Summary: They're currently on an `impl` block, move them off Reviewed By: IanChilds Differential Revision: D73726011 fbshipit-source-id: 35d5dea6abbc5772e6f1d0798a1ce547aa5e78bf
1 parent 9c36de6 commit d5ffb22

File tree

1 file changed

+30
-37
lines changed
  • starlark_derive/src/module/render

1 file changed

+30
-37
lines changed

starlark_derive/src/module/render/fun.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -264,34 +264,34 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
264264
}
265265
};
266266

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+
};
278278

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()
294293
}
294+
get_impl(__starlark_invoke_impl)
295295
}
296296
};
297297

@@ -306,7 +306,7 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
306306
) -> starlark::Result<starlark::values::Value<'v>> {
307307
#this_prepare
308308
#prepare
309-
match Self::invoke_impl(#( #invoke_args, )*) {
309+
match __starlark_invoke_impl(#( #invoke_args, )*) {
310310
Ok(v) => Ok(eval.heap().alloc(v)),
311311
Err(e) => Err(starlark::__derive_refs::invoke_macro_error::InvokeMacroError::into_starlark_error(e)),
312312
}
@@ -317,7 +317,8 @@ pub(crate) fn render_fun(x: StarFun) -> syn::Result<syn::Stmt> {
317317
Ok(syn::parse_quote! {
318318
{
319319
#struct_def
320-
#impl_struct
320+
#item_invoke_impl
321+
#item_return_type_repr
321322
#impl_trait
322323
#builder_set
323324
}
@@ -541,13 +542,6 @@ fn render_starlark_type(typ: &syn::Type) -> syn::Expr {
541542
}
542543
}
543544

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-
551545
fn render_regular_native_callable_param(arg: &StarArg) -> syn::Result<syn::Expr> {
552546
let ty = render_starlark_type(arg.without_option());
553547
let name_str = ident_string(&arg.param.ident);
@@ -656,7 +650,6 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
656650
}
657651
};
658652

659-
let return_type_str = render_starlark_return_type(x);
660653
let speculative_exec_safe = x.speculative_exec_safe;
661654
Ok(quote!(
662655
{
@@ -665,7 +658,7 @@ fn render_native_callable_components(x: &StarFun) -> syn::Result<TokenStream> {
665658
speculative_exec_safe: #speculative_exec_safe,
666659
rust_docstring: #docs,
667660
param_spec,
668-
return_type: #return_type_str,
661+
return_type: __starlark_return_type_starlark_type_repr(),
669662
}
670663
}
671664
))

0 commit comments

Comments
 (0)