diff --git a/starlark/src/macros.rs b/starlark/src/macros.rs index 43b0f44c2..b9431d3b2 100644 --- a/starlark/src/macros.rs +++ b/starlark/src/macros.rs @@ -49,6 +49,8 @@ macro_rules! starlark_complex_value { } impl<'v> $crate::values::type_repr::StarlarkTypeRepr for &'v $x<'v> { + type Canonical = $x<'v>; + #[inline] fn starlark_type_repr() -> $crate::typing::Ty { <$x as $crate::values::StarlarkValue>::get_type_starlark_repr() @@ -197,6 +199,8 @@ macro_rules! starlark_simple_value { } impl<'v> $crate::values::type_repr::StarlarkTypeRepr for &'v $x { + type Canonical = $x; + fn starlark_type_repr() -> $crate::typing::Ty { <$x as $crate::values::StarlarkValue>::get_type_starlark_repr() } diff --git a/starlark/src/stdlib/json.rs b/starlark/src/stdlib/json.rs index 75b412aaf..6b9c1b9cc 100644 --- a/starlark/src/stdlib/json.rs +++ b/starlark/src/stdlib/json.rs @@ -39,12 +39,16 @@ use crate::values::Heap; use crate::values::Value; impl StarlarkTypeRepr for serde_json::Number { + type Canonical = Either; + fn starlark_type_repr() -> Ty { Either::::starlark_type_repr() } } impl<'a> StarlarkTypeRepr for &'a serde_json::Number { + type Canonical = serde_json::Number; + fn starlark_type_repr() -> Ty { serde_json::Number::starlark_type_repr() } @@ -99,12 +103,16 @@ impl AllocFrozenValue for serde_json::Number { } impl<'a, K: StarlarkTypeRepr, V: StarlarkTypeRepr> StarlarkTypeRepr for &'a serde_json::Map { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { AllocDict::>::starlark_type_repr() } } impl StarlarkTypeRepr for serde_json::Map { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { AllocDict::>::starlark_type_repr() } @@ -141,6 +149,8 @@ impl AllocFrozenValue for serde_json::Map { } impl<'a> StarlarkTypeRepr for &'a serde_json::Value { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { // Any. Value::starlark_type_repr() @@ -148,6 +158,8 @@ impl<'a> StarlarkTypeRepr for &'a serde_json::Value { } impl StarlarkTypeRepr for serde_json::Value { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { // Any. Value::starlark_type_repr() diff --git a/starlark/src/values/layout/complex.rs b/starlark/src/values/layout/complex.rs index 5482d3a18..9d44e4c26 100644 --- a/starlark/src/values/layout/complex.rs +++ b/starlark/src/values/layout/complex.rs @@ -92,6 +92,8 @@ where T: ComplexValue<'v>, T::Frozen: StarlarkValue<'static>, { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { T::starlark_type_repr() } diff --git a/starlark/src/values/layout/typed.rs b/starlark/src/values/layout/typed.rs index 028edd7b3..0b4496166 100644 --- a/starlark/src/values/layout/typed.rs +++ b/starlark/src/values/layout/typed.rs @@ -343,6 +343,8 @@ impl<'v, T: StarlarkValue<'v>> Deref for ValueTyped<'v, T> { } impl<'v, T: StarlarkValue<'v>> StarlarkTypeRepr for ValueTyped<'v, T> { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { T::starlark_type_repr() } @@ -377,6 +379,8 @@ impl<'v> AllocStringValue<'v> for StringValue<'v> { } impl<'v, T: StarlarkValue<'v>> StarlarkTypeRepr for FrozenValueTyped<'v, T> { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { T::starlark_type_repr() } diff --git a/starlark/src/values/layout/value.rs b/starlark/src/values/layout/value.rs index 138293cb7..1f743223b 100644 --- a/starlark/src/values/layout/value.rs +++ b/starlark/src/values/layout/value.rs @@ -1137,12 +1137,16 @@ impl Serialize for FrozenValue { } impl<'v> StarlarkTypeRepr for Value<'v> { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { FrozenValue::starlark_type_repr() } } impl StarlarkTypeRepr for FrozenValue { + type Canonical = Self; + fn starlark_type_repr() -> Ty { Ty::any() } diff --git a/starlark/src/values/owned.rs b/starlark/src/values/owned.rs index 9bd9674b0..ac7746966 100644 --- a/starlark/src/values/owned.rs +++ b/starlark/src/values/owned.rs @@ -71,6 +71,8 @@ impl Display for OwnedFrozenValue { } impl StarlarkTypeRepr for OwnedFrozenValue { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { FrozenValue::starlark_type_repr() } diff --git a/starlark/src/values/type_repr.rs b/starlark/src/values/type_repr.rs index 60077b143..c82b2096a 100644 --- a/starlark/src/values/type_repr.rs +++ b/starlark/src/values/type_repr.rs @@ -50,6 +50,17 @@ use crate::values::Value; /// /// This derive is useful in combination with derive of [`UnpackValue`](crate::values::UnpackValue). pub trait StarlarkTypeRepr { + /// Different Rust type representing the same Starlark Type. + /// + /// For example, `bool` and `StarlarkBool` Rust types represent the same Starlark type `bool`. + /// + /// Formal requirement: `Self::starlark_type_repr() == Self::Canonical::starlark_type_repr()`. + /// + /// If unsure, it is safe to put `= Self` here. + /// When [`associated_type_defaults`](https://github.com/rust-lang/rust/issues/29661) + /// is stabilized, this will be the default. + type Canonical: StarlarkTypeRepr; + /// The representation of a type that a user would use verbatim in starlark type annotations fn starlark_type_repr() -> Ty; } @@ -64,42 +75,56 @@ pub struct DictType { } impl StarlarkTypeRepr for DictType { + type Canonical = DictType; + fn starlark_type_repr() -> Ty { Ty::dict(K::starlark_type_repr(), V::starlark_type_repr()) } } impl<'v, T: StarlarkValue<'v> + ?Sized> StarlarkTypeRepr for T { + type Canonical = Self; + fn starlark_type_repr() -> Ty { Self::get_type_starlark_repr() } } impl StarlarkTypeRepr for String { + type Canonical = StarlarkStr; + fn starlark_type_repr() -> Ty { StarlarkStr::starlark_type_repr() } } impl StarlarkTypeRepr for &str { + type Canonical = StarlarkStr; + fn starlark_type_repr() -> Ty { StarlarkStr::starlark_type_repr() } } impl StarlarkTypeRepr for Option { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Either::::starlark_type_repr() } } impl StarlarkTypeRepr for Vec { + type Canonical = Vec; + fn starlark_type_repr() -> Ty { Ty::list(T::starlark_type_repr()) } } impl StarlarkTypeRepr for Either { + type Canonical = Either; + fn starlark_type_repr() -> Ty { Ty::union2(TLeft::starlark_type_repr(), TRight::starlark_type_repr()) } diff --git a/starlark/src/values/types/bigint/convert.rs b/starlark/src/values/types/bigint/convert.rs index 025823764..667b95f71 100644 --- a/starlark/src/values/types/bigint/convert.rs +++ b/starlark/src/values/types/bigint/convert.rs @@ -30,6 +30,8 @@ use crate::values::UnpackValue; use crate::values::Value; impl StarlarkTypeRepr for u32 { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } @@ -50,6 +52,8 @@ impl AllocFrozenValue for u32 { } impl StarlarkTypeRepr for u64 { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } @@ -70,6 +74,8 @@ impl AllocFrozenValue for u64 { } impl StarlarkTypeRepr for i64 { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } @@ -90,6 +96,8 @@ impl AllocFrozenValue for i64 { } impl StarlarkTypeRepr for usize { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } @@ -110,6 +118,8 @@ impl AllocFrozenValue for usize { } impl StarlarkTypeRepr for isize { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } @@ -130,6 +140,8 @@ impl AllocFrozenValue for isize { } impl StarlarkTypeRepr for BigInt { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { i32::starlark_type_repr() } diff --git a/starlark/src/values/types/bool.rs b/starlark/src/values/types/bool.rs index 53f297c05..1befe72a0 100644 --- a/starlark/src/values/types/bool.rs +++ b/starlark/src/values/types/bool.rs @@ -54,11 +54,11 @@ use crate::values::ValueError; /// The result of calling `type()` on booleans. pub const BOOL_TYPE: &str = "bool"; -// We have to alias bool so we can have a Display that uses True/False. +/// `bool` value. #[derive(ProvidesStaticType, Debug, Serialize, StarlarkDocs, Allocative)] #[starlark_docs(builtin = "standard")] #[serde(transparent)] -pub(crate) struct StarlarkBool(pub(crate) bool); +pub struct StarlarkBool(pub(crate) bool); impl Display for StarlarkBool { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -88,6 +88,8 @@ impl AllocFrozenValue for bool { } impl StarlarkTypeRepr for bool { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { StarlarkBool::get_type_starlark_repr() } diff --git a/starlark/src/values/types/dict/alloc.rs b/starlark/src/values/types/dict/alloc.rs index 245c6c56b..6da5a0ca6 100644 --- a/starlark/src/values/types/dict/alloc.rs +++ b/starlark/src/values/types/dict/alloc.rs @@ -65,6 +65,8 @@ where K: StarlarkTypeRepr, V: StarlarkTypeRepr, { + type Canonical = DictType; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } diff --git a/starlark/src/values/types/dict/of.rs b/starlark/src/values/types/dict/of.rs index 1c14673ba..992935821 100644 --- a/starlark/src/values/types/dict/of.rs +++ b/starlark/src/values/types/dict/of.rs @@ -80,6 +80,8 @@ impl<'v, K: UnpackValue<'v> + Hash + Eq, V: UnpackValue<'v>> DictOf<'v, K, V> { } impl<'v, K: UnpackValue<'v>, V: UnpackValue<'v>> StarlarkTypeRepr for DictOf<'v, K, V> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } diff --git a/starlark/src/values/types/dict/refs.rs b/starlark/src/values/types/dict/refs.rs index abb93fb8e..27f925055 100644 --- a/starlark/src/values/types/dict/refs.rs +++ b/starlark/src/values/types/dict/refs.rs @@ -29,6 +29,7 @@ use crate::typing::Ty; use crate::values::dict::value::DictGen; use crate::values::dict::value::FrozenDictData; use crate::values::dict::Dict; +use crate::values::type_repr::DictType; use crate::values::type_repr::StarlarkTypeRepr; use crate::values::FrozenValue; use crate::values::UnpackValue; @@ -153,8 +154,10 @@ impl<'v> DerefMut for DictMut<'v> { } impl<'v> StarlarkTypeRepr for DictRef<'v> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { - Dict::<'v>::starlark_type_repr() + DictType::::starlark_type_repr() } } diff --git a/starlark/src/values/types/dict/traits.rs b/starlark/src/values/types/dict/traits.rs index 13783e503..7c2accf81 100644 --- a/starlark/src/values/types/dict/traits.rs +++ b/starlark/src/values/types/dict/traits.rs @@ -68,12 +68,16 @@ where } impl<'a, K: StarlarkTypeRepr, V: StarlarkTypeRepr> StarlarkTypeRepr for &'a SmallMap { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } } impl StarlarkTypeRepr for SmallMap { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } @@ -131,12 +135,16 @@ where } impl<'a, K: StarlarkTypeRepr, V: StarlarkTypeRepr> StarlarkTypeRepr for &'a BTreeMap { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } } impl StarlarkTypeRepr for BTreeMap { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictType::::starlark_type_repr() } diff --git a/starlark/src/values/types/dict/value.rs b/starlark/src/values/types/dict/value.rs index 89e0328f5..cc8d5b2b1 100644 --- a/starlark/src/values/types/dict/value.rs +++ b/starlark/src/values/types/dict/value.rs @@ -55,6 +55,7 @@ use crate::values::layout::avalue::AValueImpl; use crate::values::layout::avalue::AValueSimple; use crate::values::layout::heap::repr::AValueRepr; use crate::values::string::str_type::hash_string_value; +use crate::values::type_repr::DictType; use crate::values::type_repr::StarlarkTypeRepr; use crate::values::AllocFrozenValue; use crate::values::AllocValue; @@ -103,6 +104,8 @@ pub struct Dict<'v> { } impl<'v> StarlarkTypeRepr for Dict<'v> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { DictOf::, Value<'v>>::starlark_type_repr() } @@ -135,6 +138,8 @@ impl<'v> AllocValue<'v> for Dict<'v> { } impl StarlarkTypeRepr for FrozenDictData { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Ty::dict(Ty::any(), Ty::any()) } diff --git a/starlark/src/values/types/float.rs b/starlark/src/values/types/float.rs index 19cca36ac..34c28082c 100644 --- a/starlark/src/values/types/float.rs +++ b/starlark/src/values/types/float.rs @@ -213,6 +213,8 @@ impl StarlarkFloat { } impl StarlarkTypeRepr for f64 { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { StarlarkFloat::starlark_type_repr() } diff --git a/starlark/src/values/types/function.rs b/starlark/src/values/types/function.rs index db85b60a4..27a65fdfb 100644 --- a/starlark/src/values/types/function.rs +++ b/starlark/src/values/types/function.rs @@ -76,6 +76,8 @@ pub const FUNCTION_TYPE: &str = "function"; pub(crate) enum StarlarkFunction {} impl StarlarkTypeRepr for StarlarkFunction { + type Canonical = Self; + fn starlark_type_repr() -> Ty { Ty::any_callable() } diff --git a/starlark/src/values/types/inline_int.rs b/starlark/src/values/types/inline_int.rs index 16749b826..5bf52cb0c 100644 --- a/starlark/src/values/types/inline_int.rs +++ b/starlark/src/values/types/inline_int.rs @@ -56,7 +56,8 @@ use crate::values::Value; Serialize )] #[serde(transparent)] -pub(crate) struct InlineInt(i32); +#[doc(hidden)] +pub struct InlineInt(i32); impl Debug for InlineInt { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { @@ -203,7 +204,8 @@ impl InlineInt { } } -pub(crate) struct InlineIntOverflow; +#[doc(hidden)] +pub struct InlineIntOverflow; impl TryFrom for InlineInt { type Error = InlineIntOverflow; @@ -338,6 +340,8 @@ impl Rem for InlineInt { } impl StarlarkTypeRepr for InlineInt { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { PointerI32::starlark_type_repr() } diff --git a/starlark/src/values/types/int.rs b/starlark/src/values/types/int.rs index 844c58f59..058ecc260 100644 --- a/starlark/src/values/types/int.rs +++ b/starlark/src/values/types/int.rs @@ -86,6 +86,8 @@ impl AllocFrozenValue for i32 { } impl StarlarkTypeRepr for i32 { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { PointerI32::starlark_type_repr() } diff --git a/starlark/src/values/types/int_or_big.rs b/starlark/src/values/types/int_or_big.rs index 2fa687e36..888a28fb9 100644 --- a/starlark/src/values/types/int_or_big.rs +++ b/starlark/src/values/types/int_or_big.rs @@ -70,15 +70,23 @@ enum StarlarkIntError { PartialEq, derive_more::Display, Hash, - StarlarkTypeRepr, AllocValue, AllocFrozenValue )] -pub(crate) enum StarlarkInt { +#[doc(hidden)] +pub enum StarlarkInt { Small(InlineInt), Big(StarlarkBigInt), } +impl StarlarkTypeRepr for StarlarkInt { + type Canonical = Self; + + fn starlark_type_repr() -> Ty { + Ty::int() + } +} + #[derive(Eq, PartialEq, Copy, Clone, Dupe, Debug)] pub(crate) enum StarlarkIntRef<'v> { Small(InlineInt), @@ -385,6 +393,8 @@ impl<'v> StarlarkIntRef<'v> { } impl<'v> StarlarkTypeRepr for StarlarkIntRef<'v> { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { StarlarkInt::starlark_type_repr() } diff --git a/starlark/src/values/types/list/alloc.rs b/starlark/src/values/types/list/alloc.rs index 5fa32cb98..37a42a7f1 100644 --- a/starlark/src/values/types/list/alloc.rs +++ b/starlark/src/values/types/list/alloc.rs @@ -51,6 +51,8 @@ where L: IntoIterator, L::Item: StarlarkTypeRepr, { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::::starlark_type_repr() } diff --git a/starlark/src/values/types/list/of.rs b/starlark/src/values/types/list/of.rs index 206bb13f7..ceea4e031 100644 --- a/starlark/src/values/types/list/of.rs +++ b/starlark/src/values/types/list/of.rs @@ -50,6 +50,8 @@ impl<'v, V: UnpackValue<'v>> ListOf<'v, V> { } impl<'v, V: UnpackValue<'v>> StarlarkTypeRepr for ListOf<'v, V> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::::starlark_type_repr() } diff --git a/starlark/src/values/types/list/refs.rs b/starlark/src/values/types/list/refs.rs index 10f2f12ad..c1ba24c15 100644 --- a/starlark/src/values/types/list/refs.rs +++ b/starlark/src/values/types/list/refs.rs @@ -139,12 +139,16 @@ impl Display for FrozenListRef { } impl<'v> StarlarkTypeRepr for &'v ListRef<'v> { + type Canonical = > as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::>::starlark_type_repr() } } impl<'v> StarlarkTypeRepr for &'v FrozenListRef { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::::starlark_type_repr() } diff --git a/starlark/src/values/types/list/unpack.rs b/starlark/src/values/types/list/unpack.rs index 0f1edea18..a39a4b583 100644 --- a/starlark/src/values/types/list/unpack.rs +++ b/starlark/src/values/types/list/unpack.rs @@ -38,6 +38,8 @@ impl Default for UnpackList { } impl StarlarkTypeRepr for UnpackList { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::::starlark_type_repr() } diff --git a/starlark/src/values/types/list/value.rs b/starlark/src/values/types/list/value.rs index 3e1b4a1a6..048534724 100644 --- a/starlark/src/values/types/list/value.rs +++ b/starlark/src/values/types/list/value.rs @@ -252,6 +252,8 @@ impl<'a, V: 'a> StarlarkTypeRepr for &'a [V] where &'a V: StarlarkTypeRepr, { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Vec::<&'a V>::starlark_type_repr() } diff --git a/starlark/src/values/types/list_or_tuple.rs b/starlark/src/values/types/list_or_tuple.rs index e802c597a..fa2292d62 100644 --- a/starlark/src/values/types/list_or_tuple.rs +++ b/starlark/src/values/types/list_or_tuple.rs @@ -43,6 +43,8 @@ impl Default for UnpackListOrTuple { } impl StarlarkTypeRepr for UnpackListOrTuple { + type Canonical = , UnpackTuple> as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Either::, UnpackTuple>::starlark_type_repr() } diff --git a/starlark/src/values/types/none/none_or.rs b/starlark/src/values/types/none/none_or.rs index a22ed2d78..a61a1edce 100644 --- a/starlark/src/values/types/none/none_or.rs +++ b/starlark/src/values/types/none/none_or.rs @@ -57,6 +57,8 @@ impl NoneOr { } impl StarlarkTypeRepr for NoneOr { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Either::::starlark_type_repr() } diff --git a/starlark/src/values/types/starlark_value_as_type.rs b/starlark/src/values/types/starlark_value_as_type.rs index 66cdd79bf..5abb3fc28 100644 --- a/starlark/src/values/types/starlark_value_as_type.rs +++ b/starlark/src/values/types/starlark_value_as_type.rs @@ -127,6 +127,8 @@ impl Default for StarlarkValueAsType { } impl StarlarkTypeRepr for StarlarkValueAsType { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { AbstractType::starlark_type_repr() } diff --git a/starlark/src/values/types/string/alloc_unpack.rs b/starlark/src/values/types/string/alloc_unpack.rs index a769ed415..f0b9f01a8 100644 --- a/starlark/src/values/types/string/alloc_unpack.rs +++ b/starlark/src/values/types/string/alloc_unpack.rs @@ -68,6 +68,8 @@ impl<'v> AllocStringValue<'v> for String { } impl StarlarkTypeRepr for char { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { String::starlark_type_repr() } @@ -86,6 +88,8 @@ impl<'v> AllocStringValue<'v> for char { } impl StarlarkTypeRepr for &'_ String { + type Canonical = ::Canonical; + fn starlark_type_repr() -> Ty { String::starlark_type_repr() } diff --git a/starlark/src/values/types/structs/alloc.rs b/starlark/src/values/types/structs/alloc.rs index 7989ed350..eaeaf5598 100644 --- a/starlark/src/values/types/structs/alloc.rs +++ b/starlark/src/values/types/structs/alloc.rs @@ -24,6 +24,7 @@ use crate::values::alloc_value::AllocFrozenStringValue; use crate::values::alloc_value::AllocStringValue; use crate::values::structs::value::FrozenStruct; use crate::values::structs::value::Struct; +use crate::values::structs::StructRef; use crate::values::type_repr::StarlarkTypeRepr; use crate::values::AllocFrozenValue; use crate::values::AllocValue; @@ -62,6 +63,8 @@ where S: IntoIterator, V: StarlarkTypeRepr, { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Struct::starlark_type_repr() } diff --git a/starlark/src/values/types/structs/of.rs b/starlark/src/values/types/structs/of.rs index 0badb0766..6d99d188b 100644 --- a/starlark/src/values/types/structs/of.rs +++ b/starlark/src/values/types/structs/of.rs @@ -38,6 +38,8 @@ pub struct StructOf<'v, V: UnpackValue<'v>> { } impl<'v, V: UnpackValue<'v>> StarlarkTypeRepr for StructOf<'v, V> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Struct::starlark_type_repr() } diff --git a/starlark/src/values/types/structs/refs.rs b/starlark/src/values/types/structs/refs.rs index db80750d8..153da5b3b 100644 --- a/starlark/src/values/types/structs/refs.rs +++ b/starlark/src/values/types/structs/refs.rs @@ -54,6 +54,8 @@ impl<'v> StructRef<'v> { } impl<'v> StarlarkTypeRepr for StructRef<'v> { + type Canonical = Self; + fn starlark_type_repr() -> Ty { FrozenStruct::starlark_type_repr() } diff --git a/starlark/src/values/types/tuple/alloc.rs b/starlark/src/values/types/tuple/alloc.rs index d2ed23e11..1902c536e 100644 --- a/starlark/src/values/types/tuple/alloc.rs +++ b/starlark/src/values/types/tuple/alloc.rs @@ -18,6 +18,7 @@ use std::iter; use crate::typing::Ty; +use crate::values::tuple::UnpackTuple; use crate::values::type_repr::StarlarkTypeRepr; use crate::values::AllocFrozenValue; use crate::values::AllocValue; @@ -52,6 +53,8 @@ where T: IntoIterator, T::Item: StarlarkTypeRepr, { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Ty::tuple_of(T::Item::starlark_type_repr()) } diff --git a/starlark/src/values/types/tuple/refs.rs b/starlark/src/values/types/tuple/refs.rs index 7014d2ea0..6562de255 100644 --- a/starlark/src/values/types/tuple/refs.rs +++ b/starlark/src/values/types/tuple/refs.rs @@ -19,6 +19,7 @@ use ref_cast::ref_cast_custom; use ref_cast::RefCastCustom; use crate::typing::Ty; +use crate::values::tuple::UnpackTuple; use crate::values::type_repr::StarlarkTypeRepr; use crate::values::types::tuple::value::FrozenTuple; use crate::values::types::tuple::value::Tuple; @@ -103,12 +104,16 @@ impl FrozenTupleRef { } impl<'v> StarlarkTypeRepr for &'v TupleRef<'v> { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Ty::any_tuple() } } impl<'a> StarlarkTypeRepr for &'a FrozenTupleRef { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { Ty::any_tuple() } diff --git a/starlark/src/values/types/tuple/rust_tuple.rs b/starlark/src/values/types/tuple/rust_tuple.rs index ea4769c3a..dbbbc1881 100644 --- a/starlark/src/values/types/tuple/rust_tuple.rs +++ b/starlark/src/values/types/tuple/rust_tuple.rs @@ -80,12 +80,16 @@ impl AllocFroz } impl StarlarkTypeRepr for (T1, T2) { + type Canonical = (T1::Canonical, T2::Canonical); + fn starlark_type_repr() -> Ty { Ty::tuple2(T1::starlark_type_repr(), T2::starlark_type_repr()) } } impl StarlarkTypeRepr for (T1,) { + type Canonical = (T1::Canonical,); + fn starlark_type_repr() -> Ty { Ty::tuple(vec![T1::starlark_type_repr()]) } @@ -94,6 +98,8 @@ impl StarlarkTypeRepr for (T1,) { impl StarlarkTypeRepr for (T1, T2, T3) { + type Canonical = (T1::Canonical, T2::Canonical, T3::Canonical); + fn starlark_type_repr() -> Ty { Ty::tuple(vec![ T1::starlark_type_repr(), diff --git a/starlark/src/values/types/tuple/unpack.rs b/starlark/src/values/types/tuple/unpack.rs index 8fd3a211c..9c2ca6d98 100644 --- a/starlark/src/values/types/tuple/unpack.rs +++ b/starlark/src/values/types/tuple/unpack.rs @@ -38,6 +38,8 @@ impl Default for UnpackTuple { } impl StarlarkTypeRepr for UnpackTuple { + type Canonical = UnpackTuple; + fn starlark_type_repr() -> Ty { Ty::tuple_of(T::starlark_type_repr()) } diff --git a/starlark/src/values/typing/callable.rs b/starlark/src/values/typing/callable.rs index e05501fa9..055d66da4 100644 --- a/starlark/src/values/typing/callable.rs +++ b/starlark/src/values/typing/callable.rs @@ -180,6 +180,8 @@ impl<'v, P: StarlarkCallableParamSpec, R: StarlarkTypeRepr> StarlarkCallable<'v, impl<'v, P: StarlarkCallableParamSpec, R: StarlarkTypeRepr> StarlarkTypeRepr for StarlarkCallable<'v, P, R> { + type Canonical = Self; + fn starlark_type_repr() -> Ty { // TODO(nga): implement the same machinery for `typing.Callable`. Ty::callable(P::params(), R::starlark_type_repr()) @@ -263,6 +265,8 @@ impl FrozenStarlarkCallable

StarlarkTypeRepr for FrozenStarlarkCallable { + type Canonical = as StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> Ty { StarlarkCallable::::starlark_type_repr() } diff --git a/starlark/src/values/typing/iter.rs b/starlark/src/values/typing/iter.rs index f3c654d92..6b5be30b2 100644 --- a/starlark/src/values/typing/iter.rs +++ b/starlark/src/values/typing/iter.rs @@ -40,6 +40,8 @@ enum NonInstantiable {} pub struct StarlarkIter(PhantomData, NonInstantiable); impl StarlarkTypeRepr for StarlarkIter { + type Canonical = StarlarkIter; + fn starlark_type_repr() -> Ty { Ty::iter(T::starlark_type_repr()) } diff --git a/starlark/src/values/typing/never.rs b/starlark/src/values/typing/never.rs index a89b0281c..45a15d53c 100644 --- a/starlark/src/values/typing/never.rs +++ b/starlark/src/values/typing/never.rs @@ -80,6 +80,8 @@ impl AllocFrozenValue for TypingNever { pub enum StarlarkNever {} impl StarlarkTypeRepr for StarlarkNever { + type Canonical = Self; + fn starlark_type_repr() -> Ty { Ty::never() } diff --git a/starlark/src/values/typing/type_compiled/compiled.rs b/starlark/src/values/typing/type_compiled/compiled.rs index 7956ce772..daf98350d 100644 --- a/starlark/src/values/typing/type_compiled/compiled.rs +++ b/starlark/src/values/typing/type_compiled/compiled.rs @@ -258,6 +258,8 @@ impl<'v, V: ValueLike<'v>> Display for TypeCompiled { } impl StarlarkTypeRepr for TypeCompiled { + type Canonical = TypeCompiledImplAsStarlarkValue; + fn starlark_type_repr() -> Ty { TypeCompiledImplAsStarlarkValue::::starlark_type_repr() } diff --git a/starlark/src/values/unpack.rs b/starlark/src/values/unpack.rs index 57d804405..c577b49f8 100644 --- a/starlark/src/values/unpack.rs +++ b/starlark/src/values/unpack.rs @@ -58,6 +58,8 @@ use crate::values::ValueError; /// struct BoolOrInt(i32); /// /// impl StarlarkTypeRepr for BoolOrInt { +/// type Canonical = as StarlarkTypeRepr>::Canonical; +/// /// fn starlark_type_repr() -> Ty { /// Either::::starlark_type_repr() /// } diff --git a/starlark/src/values/value_of.rs b/starlark/src/values/value_of.rs index 57f825aac..9c1001842 100644 --- a/starlark/src/values/value_of.rs +++ b/starlark/src/values/value_of.rs @@ -53,6 +53,8 @@ impl<'v, T: UnpackValue<'v>> Deref for ValueOf<'v, T> { } impl<'v, T: UnpackValue<'v>> StarlarkTypeRepr for ValueOf<'v, T> { + type Canonical = T::Canonical; + fn starlark_type_repr() -> Ty { T::starlark_type_repr() } diff --git a/starlark/src/values/value_of_unchecked.rs b/starlark/src/values/value_of_unchecked.rs index 98422f312..2257786dc 100644 --- a/starlark/src/values/value_of_unchecked.rs +++ b/starlark/src/values/value_of_unchecked.rs @@ -75,6 +75,8 @@ impl<'v, T: StarlarkTypeRepr> ValueOfUnchecked<'v, T> { } impl<'v, T: StarlarkTypeRepr> StarlarkTypeRepr for ValueOfUnchecked<'v, T> { + type Canonical = T::Canonical; + fn starlark_type_repr() -> Ty { T::starlark_type_repr() } diff --git a/starlark_derive/src/starlark_type_repr.rs b/starlark_derive/src/starlark_type_repr.rs index 1b2921f3f..8e9991997 100644 --- a/starlark_derive/src/starlark_type_repr.rs +++ b/starlark_derive/src/starlark_type_repr.rs @@ -111,6 +111,8 @@ fn derive_starlark_type_repr_impl( let trait_impl: syn::ItemImpl = syn::parse_quote_spanned! { span => impl #impl_generics starlark::values::type_repr::StarlarkTypeRepr for #ident #type_generics #where_clause { + type Canonical = <#helper_type as starlark::values::type_repr::StarlarkTypeRepr>::Canonical; + fn starlark_type_repr() -> starlark::typing::Ty { <#helper_type as starlark::values::type_repr::StarlarkTypeRepr>::starlark_type_repr() } diff --git a/starlark_derive/src/starlark_value.rs b/starlark_derive/src/starlark_value.rs index 7eaf90ef7..481dd09f9 100644 --- a/starlark_derive/src/starlark_value.rs +++ b/starlark_derive/src/starlark_value.rs @@ -147,6 +147,8 @@ impl ImplStarlarkValue { impl<#params> starlark::values::type_repr::StarlarkTypeRepr for &#lt #self_ty #where_clause { + type Canonical = #self_ty; + fn starlark_type_repr() -> starlark::typing::Ty { <#self_ty as starlark::values::type_repr::StarlarkTypeRepr>::starlark_type_repr() }