Skip to content

Commit f9bdf4c

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
module: Make generic work for consts
Summary: The easiest one of consts/attrs/funcs, just need to pass the generics down to the inner builder function This breaks the one place that was putting `<'v>` on the `fn globals_builder()`, but that's completely wrong anyway Reviewed By: ndmitchell Differential Revision: D73726004 fbshipit-source-id: 7567478c297f9d811e328af73fff1d98e618a24f
1 parent 7a71418 commit f9bdf4c

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

starlark/src/tests/derive/module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
mod basic;
1919
mod default_value;
20+
mod generic;
2021
mod kwargs;
2122
mod methods;
2223
mod named_positional;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 The Starlark in Rust Authors.
3+
* Copyright (c) Facebook, Inc. and its affiliates.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use starlark_derive::starlark_module;
19+
20+
use crate as starlark;
21+
use crate::assert::Assert;
22+
use crate::environment::GlobalsBuilder;
23+
24+
#[starlark_module]
25+
fn generic_builder<T: Default, U>(globals: &mut GlobalsBuilder)
26+
where
27+
U: std::fmt::Display + Default,
28+
{
29+
const MY_STR: &str = &U::default().to_string();
30+
}
31+
32+
#[test]
33+
fn test_generic_builder() {
34+
let mut a = Assert::new();
35+
a.globals_add(generic_builder::<u8, u8>);
36+
a.eq("\"0\"", "MY_STR");
37+
}

starlark/src/tests/uncategorized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ fn test_label_assign() {
743743
}
744744

745745
#[starlark_module]
746-
fn module<'v>(builder: &mut GlobalsBuilder) {
746+
fn module(builder: &mut GlobalsBuilder) {
747747
fn wrapper<'v>(heap: &'v Heap) -> anyhow::Result<Value<'v>> {
748748
Ok(heap.alloc_complex(Wrapper(RefCell::new(SmallMap::new()))))
749749
}

starlark_derive/src/module/render.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ fn render_impl(x: StarModule) -> syn::Result<syn::ItemFn> {
6868
}
6969
},
7070
};
71+
let turbo = input.sig.generics.split_for_impl().1;
72+
let turbo = turbo.as_turbofish();
7173
input.block = syn::parse_quote! {
7274
{
7375
#inner_fn
7476
static RES: starlark::environment::#statics = starlark::environment::#statics::new();
75-
RES.populate(build, globals_builder);
77+
RES.populate(build #turbo, globals_builder);
7678
}
7779
};
7880
Ok(input)

0 commit comments

Comments
 (0)