Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Serialise Value, PrimValue, and TypeArg with internal tags #496

Merged
merged 9 commits into from
Sep 6, 2023
2 changes: 1 addition & 1 deletion src/types/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Type {
.try_for_each(|(elem, ty)| ty.check_type(elem))
.map_err(|_| ConstTypeError::ValueCheckFail(self.clone(), val.clone()))
}
(TypeEnum::Sum(sum), Value::Sum(tag, value)) => sum
(TypeEnum::Sum(sum), Value::Sum { tag, value }) => sum
.get_variant(*tag)
.ok_or(ConstTypeError::InvalidSumTag)?
.check_type(value),
Expand Down
1 change: 1 addition & 0 deletions src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl TypeParam {
/// A statically-known argument value to an operation.
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
#[serde(tag = "aty")]
mark-koch marked this conversation as resolved.
Show resolved Hide resolved
pub enum TypeArg {
/// Where the (Type/Op)Def declares that an argument is a [TypeParam::Type]
Type(Type),
Expand Down
18 changes: 15 additions & 3 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::types::{CustomCheckFailure, CustomType};

/// A constant value of a primitive (or leaf) type.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "pty")]
mark-koch marked this conversation as resolved.
Show resolved Hide resolved
pub enum PrimValue {
/// An extension constant value, that can check it is of a given [CustomType].
///
Expand Down Expand Up @@ -42,14 +43,20 @@ impl PrimValue {
/// A value that can be stored as a static constant. Representing core types and
/// extension types.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "vty")]
mark-koch marked this conversation as resolved.
Show resolved Hide resolved
pub enum Value {
/// A primitive (non-container) value.
Prim(PrimValue),
/// A tuple
Tuple(Vec<Value>),
/// A Sum variant -- for any Sum type where this value meets
/// the type of the variant indicated by the tag
Sum(usize, Box<Value>), // Tag and value
Sum {
/// The tag index of the variant
tag: usize,
/// The value of the variant
value: Box<Value>,
},
}

impl Value {
Expand All @@ -61,7 +68,9 @@ impl Value {
let names: Vec<_> = vals.iter().map(Value::name).collect();
format!("const:seq:{{{}}}", names.join(", "))
}
Value::Sum(tag, val) => format!("const:sum:{{tag:{tag}, val:{}}}", val.name()),
Value::Sum { tag, value: val } => {
format!("const:sum:{{tag:{tag}, val:{}}}", val.name())
}
}
}

Expand Down Expand Up @@ -92,7 +101,10 @@ impl Value {

/// Sum value (could be of any compatible type, e.g. a predicate)
pub fn sum(tag: usize, value: Value) -> Self {
Self::Sum(tag, Box::new(value))
Self::Sum {
tag,
value: Box::new(value),
}
}

/// New custom value (of type that implements [`CustomConst`]).
Expand Down