1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright 2019 The Starlark in Rust Authors.
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Convert a value implementing [`StarlarkValue`] into a type usable in type expression.

use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::marker::PhantomData;

use allocative::Allocative;
use starlark_derive::starlark_value;
use starlark_derive::NoSerialize;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::typing::Ty;
use crate::values::layout::avalue::alloc_static;
use crate::values::layout::avalue::AValueImpl;
use crate::values::layout::avalue::Basic;
use crate::values::layout::heap::repr::AValueRepr;
use crate::values::type_repr::StarlarkTypeRepr;
use crate::values::typing::ty::AbstractType;
use crate::values::AllocFrozenValue;
use crate::values::FrozenHeap;
use crate::values::FrozenValue;
use crate::values::StarlarkValue;

#[derive(Debug, NoSerialize, Allocative, ProvidesStaticType)]
struct StarlarkValueAsTypeStarlarkValue(fn() -> Ty);

#[starlark_value(type = "type")]
impl<'v> StarlarkValue<'v> for StarlarkValueAsTypeStarlarkValue {
    type Canonical = AbstractType;

    fn eval_type(&self) -> Option<Ty> {
        Some((self.0)())
    }
}

impl Display for StarlarkValueAsTypeStarlarkValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&(self.0)(), f)
    }
}

/// Utility to declare a value usable in type expression.
///
/// # Example
///
/// ```
/// use allocative::Allocative;
/// use starlark::any::ProvidesStaticType;
/// use starlark::environment::GlobalsBuilder;
/// use starlark::values::starlark_value;
/// use starlark::values::starlark_value_as_type::StarlarkValueAsType;
/// use starlark::values::NoSerialize;
/// use starlark::values::StarlarkValue;
/// #[derive(
///     Debug,
///     derive_more::Display,
///     Allocative,
///     ProvidesStaticType,
///     NoSerialize
/// )]
/// struct Temperature;
///
/// #[starlark_value(type = "temperature")]
/// impl<'v> StarlarkValue<'v> for Temperature {}
///
/// fn my_type_globals(globals: &mut GlobalsBuilder) {
///     // This can now be used like:
///     // ```
///     // def f(x: Temperature): pass
///     // ```
///     const Temperature: StarlarkValueAsType<Temperature> = StarlarkValueAsType::new();
/// }
/// ```
pub struct StarlarkValueAsType<T: StarlarkTypeRepr>(PhantomData<fn(&T)>);

impl<T: StarlarkTypeRepr> Debug for StarlarkValueAsType<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_tuple("StarlarkValueAsType")
            .field(&T::starlark_type_repr())
            .finish()
    }
}

impl<T: StarlarkTypeRepr> Display for StarlarkValueAsType<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&T::starlark_type_repr(), f)
    }
}

impl<T: StarlarkTypeRepr> StarlarkValueAsType<T> {
    /// Constructor.
    pub const fn new() -> Self {
        Self(PhantomData)
    }

    const INSTANCE: AValueRepr<AValueImpl<Basic, StarlarkValueAsTypeStarlarkValue>> = alloc_static(
        Basic,
        StarlarkValueAsTypeStarlarkValue(T::starlark_type_repr),
    );
}

impl<T: StarlarkTypeRepr> Default for StarlarkValueAsType<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: StarlarkTypeRepr> StarlarkTypeRepr for StarlarkValueAsType<T> {
    fn starlark_type_repr() -> Ty {
        AbstractType::starlark_type_repr()
    }
}

impl<T: StarlarkTypeRepr> AllocFrozenValue for StarlarkValueAsType<T> {
    fn alloc_frozen_value(self, _heap: &FrozenHeap) -> FrozenValue {
        FrozenValue::new_repr(&Self::INSTANCE)
    }
}

#[cfg(test)]
mod tests {
    use allocative::Allocative;
    use starlark_derive::starlark_module;
    use starlark_derive::starlark_value;
    use starlark_derive::NoSerialize;
    use starlark_derive::ProvidesStaticType;

    use crate as starlark;
    use crate::assert::Assert;
    use crate::environment::GlobalsBuilder;
    use crate::values::types::starlark_value_as_type::tests;
    use crate::values::types::starlark_value_as_type::StarlarkValueAsType;
    use crate::values::AllocValue;
    use crate::values::Heap;
    use crate::values::StarlarkValue;
    use crate::values::Value;

    #[derive(
        derive_more::Display,
        Debug,
        NoSerialize,
        Allocative,
        ProvidesStaticType
    )]
    struct CompilerArgs(String);

    #[starlark_value(type = "compiler_args")]
    impl<'v> StarlarkValue<'v> for CompilerArgs {}

    impl<'v> AllocValue<'v> for CompilerArgs {
        fn alloc_value(self, heap: &'v Heap) -> Value<'v> {
            heap.alloc_simple(self)
        }
    }

    #[starlark_module]
    fn compiler_args_globals(globals: &mut GlobalsBuilder) {
        const CompilerArgs: StarlarkValueAsType<CompilerArgs> = StarlarkValueAsType::new();

        fn compiler_args(x: String) -> anyhow::Result<CompilerArgs> {
            Ok(tests::CompilerArgs(x))
        }
    }

    #[test]
    fn test_pass() {
        let mut a = Assert::new();
        a.globals_add(compiler_args_globals);
        a.pass(
            r#"
def f(x: CompilerArgs): pass

f(compiler_args("hello"))
        "#,
        );
    }

    #[test]
    fn test_fail_compile_time() {
        let mut a = Assert::new();
        a.globals_add(compiler_args_globals);
        a.fail(
            r#"
def g(x: CompilerArgs): pass

def h():
    g([])
"#,
            r#"Expected type `compiler_args` but got"#,
        );
    }

    #[test]
    fn test_fail_runtime() {
        let mut a = Assert::new();
        a.globals_add(compiler_args_globals);
        a.fail(
            r#"
def h(x: CompilerArgs): pass

noop(h)(1)
            "#,
            r#"Value `1` of type `int` does not match the type annotation"#,
        );
    }
}