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

[bugfix] Check #TypeArgs == #TypeParams for TypeDefs as well as OpDefs #342

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ trait TypeParametrised {
fn resource(&self) -> Option<&ResourceId>;
/// Check provided type arguments are valid against parameters.
fn check_args_impl(&self, args: &[TypeArg]) -> Result<(), SignatureError> {
if args.len() != self.params().len() {
return Err(SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(
args.len(),
self.params().len(),
)));
}
for (a, p) in args.iter().zip(self.params().iter()) {
check_type_arg(a, p).map_err(SignatureError::TypeArgMismatch)?;
}
Expand Down Expand Up @@ -358,12 +364,6 @@ impl OpDef {
/// Computes the signature of a node, i.e. an instantiation of this
/// OpDef with statically-provided [TypeArg]s.
pub fn compute_signature(&self, args: &[TypeArg]) -> Result<AbstractSignature, SignatureError> {
if args.len() != self.params.len() {
return Err(SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(
args.len(),
self.params.len(),
)));
}
self.check_args(args)?;
let (ins, outs, res) = match &self.signature_func {
SignatureFunc::FromYAML { .. } => {
Expand Down Expand Up @@ -688,3 +688,58 @@ impl FromIterator<ResourceId> for ResourceSet {
Self(HashSet::from_iter(iter))
}
}

#[cfg(test)]
mod test {
use crate::resource::SignatureError;
use crate::types::type_param::{TypeArg, TypeArgError, TypeParam};
use crate::types::{ClassicType, HashableType, PrimType, SimpleType, TypeTag};

use super::{TypeDef, TypeDefTag};

#[test]
fn test_instantiate_typedef() {
let def = TypeDef {
name: "MyType".into(),
params: vec![TypeParam::ClassicType],
resource: Some("MyRsrc".into()),
description: "Some parameterised type".into(),
tag: TypeDefTag::FromParams(vec![0]),
};
let typ: SimpleType = def
.instantiate_concrete(vec![TypeArg::ClassicType(ClassicType::F64)])
.unwrap()
.into();
assert_eq!(typ.tag(), TypeTag::Classic);
let typ2: SimpleType = def
.instantiate_concrete([TypeArg::ClassicType(ClassicType::Hashable(
HashableType::String,
))])
.unwrap()
.into();
assert_eq!(typ2.tag(), TypeTag::Hashable);

// And some bad arguments...firstly, wrong kind of TypeArg:
assert_eq!(
def.instantiate_concrete([TypeArg::HashableType(HashableType::String)]),
Err(SignatureError::TypeArgMismatch(TypeArgError::TypeMismatch(
TypeArg::HashableType(HashableType::String),
TypeParam::ClassicType
)))
);
// Too few arguments:
assert_eq!(
def.instantiate_concrete([]).unwrap_err(),
SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(0, 1))
);
// Too many arguments:
assert_eq!(
def.instantiate_concrete([
TypeArg::ClassicType(ClassicType::F64),
TypeArg::ClassicType(ClassicType::F64),
])
.unwrap_err(),
SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(2, 1))
);
}
}
4 changes: 2 additions & 2 deletions src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub enum TypeArgError {
// TODO It may become possible to combine this with ConstTypeError.
#[error("Type argument {0:?} does not fit declared parameter {1:?}")]
TypeMismatch(TypeArg, TypeParam),
/// Wrong number of type arguments.
// For now this only happens at the top level (TypeArgs of Op vs TypeParams of OpDef).
/// Wrong number of type arguments (actual vs expected).
// For now this only happens at the top level (TypeArgs of op/type vs TypeParams of Op/TypeDef).
// However in the future it may be applicable to e.g. contents of Tuples too.
#[error("Wrong number of type arguments: {0} vs expected {1} declared type parameters")]
WrongNumber(usize, usize),
Expand Down