[meta] Port TypeVar's test_functions to Rust;

Fixes two issues:

- name of derived type variables wasn't correct. (This is used during
code generation.)
- the TypeVar::derived() function wasn't creating the correct type set
(and would instead propagate the parent's one).
This commit is contained in:
Benjamin Bouvier
2019-03-29 16:21:30 +01:00
parent dd63ebfb68
commit 5edee84f67

View File

@@ -117,7 +117,7 @@ impl TypeVar {
/// Create a type variable that is a function of another. /// Create a type variable that is a function of another.
fn derived(&self, derived_func: DerivedFunc) -> TypeVar { fn derived(&self, derived_func: DerivedFunc) -> TypeVar {
let ts = self.content.type_set.clone(); let ts = self.get_typeset();
// Safety checks to avoid over/underflows. // Safety checks to avoid over/underflows.
assert!(ts.specials.len() == 0, "can't derive from special types"); assert!(ts.specials.len() == 0, "can't derive from special types");
@@ -169,7 +169,7 @@ impl TypeVar {
return TypeVar { return TypeVar {
content: Rc::new(TypeVarContent { content: Rc::new(TypeVarContent {
name: "".into(), // XXX Python passes None to these two fields name: format!("{}({})", derived_func.name(), self.name),
doc: "".into(), doc: "".into(),
type_set: ts, type_set: ts,
base: Some(TypeVarParent { base: Some(TypeVarParent {
@@ -842,6 +842,23 @@ fn test_typeset_singleton() {
); );
} }
#[test]
fn test_typevar_functions() {
let x = TypeVar::new(
"x",
"i16 and up",
TypeSetBuilder::new().ints(16..64).finish(),
);
assert_eq!(x.half_width().name, "half_width(x)");
assert_eq!(
x.half_width().double_width().name,
"double_width(half_width(x))"
);
let x = TypeVar::new("x", "up to i32", TypeSetBuilder::new().ints(8..32).finish());
assert_eq!(x.double_width().name, "double_width(x)");
}
#[test] #[test]
fn test_typevar_singleton() { fn test_typevar_singleton() {
use crate::cdsl::types::VectorType; use crate::cdsl::types::VectorType;