From 7dec8b9da193f181db125569017b1a5ae4511f8d 2021-12-11 00:51:32 From: mh Date: 2021-12-11 00:51:32 Subject: [PATCH] Support tuples in protocol tests, remove duplicate ConcreteType serialization --- diff --git a/src/protocol/ast.rs b/src/protocol/ast.rs index 3510e80f484af804fb41047d1ff0c38d14abd87c..2e5bdc8d168c3eba253195ba4b8075f3313e7a88 100644 --- a/src/protocol/ast.rs +++ b/src/protocol/ast.rs @@ -642,8 +642,8 @@ impl ConcreteType { for poly_arg_idx in 0..num_poly_args { if poly_arg_idx != 0 { target.push(','); - idx = Self::render_type_part_at(parts, heap, idx, target); } + idx = Self::render_type_part_at(parts, heap, idx, target); } target.push('>'); } diff --git a/src/protocol/tests/utils.rs b/src/protocol/tests/utils.rs index e21d72b5708c0631c39008fffe5427d0880a83da..45faf494e2d692b358c0d91f5692c431d103d1b8 100644 --- a/src/protocol/tests/utils.rs +++ b/src/protocol/tests/utils.rs @@ -732,8 +732,7 @@ impl<'a> VariableTester<'a> { let concrete_type = &mono_data.expr_data[self.var_expr.unique_id_in_definition as usize].expr_type; // Serialize and check - let mut serialized = String::new(); - serialize_concrete_type(&mut serialized, self.ctx.heap, self.definition_id, concrete_type); + let mut serialized = concrete_type.display_name(self.ctx.heap); assert_eq!( expected, &serialized, @@ -768,8 +767,7 @@ impl<'a> ExpressionTester<'a> { let concrete_type = &mono_data.expr_data[expr_index as usize].expr_type; // Serialize and check type - let mut serialized = String::new(); - serialize_concrete_type(&mut serialized, self.ctx.heap, self.definition_id, concrete_type); + let mut serialized = concrete_type.display_name(self.ctx.heap); assert_eq!( expected, &serialized, @@ -919,7 +917,7 @@ fn has_monomorph(ctx: TestCtx, definition_id: DefinitionId, serialized_monomorph full_buffer.push('"'); let first_idx = full_buffer.len(); - serialize_concrete_type(&mut full_buffer, ctx.heap, definition_id, concrete_type); + full_buffer.push_str(concrete_type.display_name(ctx.heap).as_str()); if &full_buffer[first_idx..] == serialized_monomorph { has_match = Some(mono_idx); } @@ -1002,6 +1000,16 @@ fn serialize_parser_type(buffer: &mut String, heap: &Heap, parser_type: &ParserT idx = serialize_variant(buffer, heap, parser_type, idx + 1); buffer.push('>'); }, + PTV::Tuple(num_embedded) => { + buffer.push('('); + for embedded_idx in 0..*num_embedded { + if embedded_idx != 0 { + buffer.push(','); + } + idx = serialize_variant(buffer, heap, parser_type, idx + 1); + } + buffer.push(')'); + }, PTV::PolymorphicArgument(definition_id, poly_idx) => { let definition = &heap[*definition_id]; let poly_arg = &definition.poly_vars()[*poly_idx as usize]; @@ -1031,77 +1039,6 @@ fn serialize_parser_type(buffer: &mut String, heap: &Heap, parser_type: &ParserT serialize_variant(buffer, heap, parser_type, 0); } -fn serialize_concrete_type(buffer: &mut String, heap: &Heap, def: DefinitionId, concrete: &ConcreteType) { - // Retrieve polymorphic variables - let poly_vars = heap[def].poly_vars(); - - fn write_bytes(buffer: &mut String, bytes: &[u8]) { - let utf8 = String::from_utf8_lossy(bytes); - buffer.push_str(&utf8); - } - - fn serialize_recursive( - buffer: &mut String, heap: &Heap, poly_vars: &Vec, concrete: &ConcreteType, mut idx: usize - ) -> usize { - use ConcreteTypePart as CTP; - - let part = &concrete.parts[idx]; - match part { - CTP::Void => buffer.push_str("void"), - CTP::Message => write_bytes(buffer, KW_TYPE_MESSAGE), - CTP::Bool => write_bytes(buffer, KW_TYPE_BOOL), - CTP::UInt8 => write_bytes(buffer, KW_TYPE_UINT8), - CTP::UInt16 => write_bytes(buffer, KW_TYPE_UINT16), - CTP::UInt32 => write_bytes(buffer, KW_TYPE_UINT32), - CTP::UInt64 => write_bytes(buffer, KW_TYPE_UINT64), - CTP::SInt8 => write_bytes(buffer, KW_TYPE_SINT8), - CTP::SInt16 => write_bytes(buffer, KW_TYPE_SINT16), - CTP::SInt32 => write_bytes(buffer, KW_TYPE_SINT32), - CTP::SInt64 => write_bytes(buffer, KW_TYPE_SINT64), - CTP::Character => write_bytes(buffer, KW_TYPE_CHAR), - CTP::String => write_bytes(buffer, KW_TYPE_STRING), - CTP::Array => { - idx = serialize_recursive(buffer, heap, poly_vars, concrete, idx + 1); - buffer.push_str("[]"); - }, - CTP::Slice => { - idx = serialize_recursive(buffer, heap, poly_vars, concrete, idx + 1); - buffer.push_str("[..]"); - }, - CTP::Input => { - write_bytes(buffer, KW_TYPE_IN_PORT); - buffer.push('<'); - idx = serialize_recursive(buffer, heap, poly_vars, concrete, idx + 1); - buffer.push('>'); - }, - CTP::Output => { - write_bytes(buffer, KW_TYPE_OUT_PORT); - buffer.push('<'); - idx = serialize_recursive(buffer, heap, poly_vars, concrete, idx + 1); - buffer.push('>'); - }, - CTP::Instance(definition_id, num_sub) | - CTP::Function(definition_id, num_sub) | - CTP::Component(definition_id, num_sub) => { - let definition_name = heap[*definition_id].identifier(); - buffer.push_str(definition_name.value.as_str()); - if *num_sub != 0 { - buffer.push('<'); - for sub_idx in 0..*num_sub { - if sub_idx != 0 { buffer.push(','); } - idx = serialize_recursive(buffer, heap, poly_vars, concrete, idx + 1); - } - buffer.push('>'); - } - }, - } - - idx - } - - serialize_recursive(buffer, heap, poly_vars, concrete, 0); -} - fn seek_def_in_modules<'a>(heap: &Heap, modules: &'a [Module], def_id: DefinitionId) -> Option<&'a Module> { for module in modules { let root = &heap.protocol_descriptions[module.root_id];