From 5edee84f678571f067cdf475b4c6fcbcb83c421f Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 29 Mar 2019 16:21:30 +0100 Subject: [PATCH] [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). --- cranelift/codegen/meta/src/cdsl/typevar.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cranelift/codegen/meta/src/cdsl/typevar.rs b/cranelift/codegen/meta/src/cdsl/typevar.rs index cb6f18ce56..983bb17a16 100644 --- a/cranelift/codegen/meta/src/cdsl/typevar.rs +++ b/cranelift/codegen/meta/src/cdsl/typevar.rs @@ -117,7 +117,7 @@ impl TypeVar { /// Create a type variable that is a function of another. 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. assert!(ts.specials.len() == 0, "can't derive from special types"); @@ -169,7 +169,7 @@ impl TypeVar { return TypeVar { content: Rc::new(TypeVarContent { - name: "".into(), // XXX Python passes None to these two fields + name: format!("{}({})", derived_func.name(), self.name), doc: "".into(), type_set: ts, 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] fn test_typevar_singleton() { use crate::cdsl::types::VectorType;