Changeset - 7f9b23076d66
[Not reviewed]
0 6 0
mh - 4 years ago 2021-12-15 16:56:29
contact@maxhenger.nl
Add some tests for tuple member access
6 files changed with 118 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/protocol/eval/executor.rs
Show inline comments
 
@@ -467,54 +467,60 @@ impl Prompt {
 
                                self.store.heap_regions[new_heap_pos as usize].values = values;
 

	
 
                            } // else: empty range
 

	
 
                            cur_frame.expr_values.push_back(match value_kind {
 
                                ValueKind::Array => Value::Array(new_heap_pos),
 
                                ValueKind::String => Value::String(new_heap_pos),
 
                                ValueKind::Message => Value::Message(new_heap_pos),
 
                            });
 

	
 
                            // Dropping the original subject, because we don't
 
                            // want to drop something on the stack
 
                            self.store.drop_value(subject.get_heap_pos());
 
                        },
 
                        Expression::Select(expr) => {
 
                            let subject= cur_frame.expr_values.pop_back().unwrap();
 
                            let mono_data = types.get_procedure_monomorph(cur_frame.monomorph_idx);
 
                            let field_idx = mono_data.expr_data[expr.unique_id_in_definition as usize].field_or_monomorph_idx as u32;
 

	
 
                            // Note: same as above: clone if value lives on expr stack, simply
 
                            // refer to it if it already lives on the stack/heap.
 
                            let (deallocate_heap_pos, value_to_push) = match subject {
 
                                Value::Ref(value_ref) => {
 
                                    let subject = self.store.read_ref(value_ref);
 
                                    let subject_heap_pos = subject.as_struct();
 
                                    let subject_heap_pos = match expr.kind {
 
                                        SelectKind::StructField(_) => subject.as_struct(),
 
                                        SelectKind::TupleMember(_) => subject.as_tuple(),
 
                                    };
 

	
 
                                    (None, Value::Ref(ValueId::Heap(subject_heap_pos, field_idx)))
 
                                },
 
                                _ => {
 
                                    let subject_heap_pos = subject.as_struct();
 
                                    let subject_heap_pos = match expr.kind {
 
                                        SelectKind::StructField(_) => subject.as_struct(),
 
                                        SelectKind::TupleMember(_) => subject.as_tuple(),
 
                                    };
 
                                    let subject_indexed = Value::Ref(ValueId::Heap(subject_heap_pos, field_idx));
 
                                    (Some(subject_heap_pos), self.store.clone_value(subject_indexed))
 
                                },
 
                            };
 

	
 
                            cur_frame.expr_values.push_back(value_to_push);
 
                            self.store.drop_value(deallocate_heap_pos);
 
                        },
 
                        Expression::Literal(expr) => {
 
                            let value = match &expr.value {
 
                                Literal::Null => Value::Null,
 
                                Literal::True => Value::Bool(true),
 
                                Literal::False => Value::Bool(false),
 
                                Literal::Character(lit_value) => Value::Char(*lit_value),
 
                                Literal::String(lit_value) => {
 
                                    let heap_pos = self.store.alloc_heap();
 
                                    let values = &mut self.store.heap_regions[heap_pos as usize].values;
 
                                    let value = lit_value.as_str();
 
                                    debug_assert!(values.is_empty());
 
                                    values.reserve(value.len());
 
                                    for character in value.as_bytes() {
 
                                        debug_assert!(character.is_ascii());
 
                                        values.push(Value::Char(*character as char));
 
                                    }
src/protocol/eval/store.rs
Show inline comments
 
@@ -168,48 +168,49 @@ impl Store {
 
                Value::Ref(ValueId::Heap(heap_pos, val_idx)) => {
 
                    self.clone_value(self.heap_regions[heap_pos as usize].values[val_idx as usize].clone())
 
                },
 
                _ => value,
 
            };
 
        }
 

	
 
        // Value does live on heap, copy it
 
        let source_heap_pos = source_heap_pos.unwrap() as usize;
 
        let target_heap_pos = self.alloc_heap();
 
        let target_heap_pos_usize = target_heap_pos as usize;
 

	
 
        let num_values = self.heap_regions[source_heap_pos].values.len();
 
        for value_idx in 0..num_values {
 
            let cloned = self.clone_value(self.heap_regions[source_heap_pos].values[value_idx].clone());
 
            self.heap_regions[target_heap_pos_usize].values.push(cloned);
 
        }
 

	
 
        match value {
 
            Value::Message(_) => Value::Message(target_heap_pos),
 
            Value::String(_) => Value::String(target_heap_pos),
 
            Value::Array(_) => Value::Array(target_heap_pos),
 
            Value::Union(tag, _) => Value::Union(tag, target_heap_pos),
 
            Value::Struct(_) => Value::Struct(target_heap_pos),
 
            Value::Tuple(_) => Value::Tuple(target_heap_pos),
 
            _ => unreachable!("performed clone_value on heap, but {:?} is not a heap value", value),
 
        }
 
    }
 

	
 
    pub(crate) fn drop_value(&mut self, value: Option<HeapPos>) {
 
        if let Some(heap_pos) = value {
 
            self.drop_heap_pos(heap_pos);
 
        }
 
    }
 

	
 
    pub(crate) fn drop_heap_pos(&mut self, heap_pos: HeapPos) {
 
        let num_values = self.heap_regions[heap_pos as usize].values.len();
 
        for value_idx in 0..num_values {
 
            if let Some(other_heap_pos) = self.heap_regions[heap_pos as usize].values[value_idx].get_heap_pos() {
 
                self.drop_heap_pos(other_heap_pos);
 
            }
 
        }
 

	
 
        self.heap_regions[heap_pos as usize].values.clear();
 
        self.free_regions.push_back(heap_pos);
 
    }
 

	
 
    pub(crate) fn alloc_heap(&mut self) -> HeapPos {
 
        if self.free_regions.is_empty() {
src/protocol/parser/pass_typing.rs
Show inline comments
 
@@ -3280,49 +3280,52 @@ impl PassTyping {
 
            EP::Return(_) =>
 
                // Must match the return type of the function
 
                if let DefinitionType::Function(func_id) = self.definition_type {
 
                    debug_assert_eq!(ctx.heap[func_id].return_types.len(), 1);
 
                    let returned = &ctx.heap[func_id].return_types[0];
 
                    self.determine_inference_type_from_parser_type_elements(&returned.elements, true)
 
                } else {
 
                    // Cannot happen: definition always set upon body traversal
 
                    // and "return" calls in components are illegal.
 
                    unreachable!();
 
                },
 
            EP::New(_) =>
 
                // Must be a component call, which we assign a "Void" return
 
                // type
 
                InferenceType::new(false, true, vec![ITP::Void]),
 
        };
 

	
 
        let infer_expr = &mut self.expr_types[expr.get_unique_id_in_definition() as usize];
 
        let needs_extra_data = match expr {
 
            Expression::Call(_) => true,
 
            Expression::Literal(expr) => match expr.value {
 
                Literal::Enum(_) | Literal::Union(_) | Literal::Struct(_) => true,
 
                _ => false,
 
            },
 
            Expression::Select(_) => true,
 
            Expression::Select(expr) => match expr.kind {
 
                SelectKind::StructField(_) => true,
 
                SelectKind::TupleMember(_) => false,
 
            },
 
            _ => false,
 
        };
 

	
 
        if infer_expr.expr_id.is_invalid() {
 
            // Nothing is set yet
 
            infer_expr.expr_type = inference_type;
 
            infer_expr.expr_id = expr_id;
 
            if needs_extra_data {
 
                let extra_idx = self.extra_data.len() as i32;
 
                self.extra_data.push(ExtraData::default());
 
                infer_expr.extra_data_idx = extra_idx;
 
            }
 
        } else {
 
            // We already have an entry
 
            debug_assert!(false, "does this ever happen?");
 
            if let SingleInferenceResult::Incompatible = InferenceType::infer_subtree_for_single_type(
 
                &mut infer_expr.expr_type, 0, &inference_type.parts, 0, false
 
            ) {
 
                return Err(self.construct_expr_type_error(ctx, expr_id, expr_id));
 
            }
 

	
 
            debug_assert!((infer_expr.extra_data_idx != -1) == needs_extra_data);
 
        }
 

	
src/protocol/tests/eval_operators.rs
Show inline comments
 
@@ -126,79 +126,113 @@ fn test_binary_integer_operators() {
 
    );
 
    perform_test(
 
        "add", "u32",
 
        "auto a = 5; return a + 5;", Value::UInt32(10)
 
    );
 
    perform_test(
 
        "subtract", "u32",
 
        "auto a = 3; return a - 3;", Value::UInt32(0)
 
    );
 
    perform_test(
 
        "multiply", "u8",
 
        "auto a = 2 * 2; return a * 2 * 2;", Value::UInt8(16)
 
    );
 
    perform_test(
 
        "divide", "u8",
 
        "auto a = 32 / 2; return a / 2 / 2;", Value::UInt8(4)
 
    );
 
    perform_test(
 
        "remainder", "u16",
 
        "auto a = 29; return a % 3;", Value::UInt16(2)
 
    );
 
}
 

	
 
#[test]
 
fn test_tuple_operators() {
 
fn test_tuple_comparison_operators() {
 
    Tester::new_single_source_expect_ok("tuple equality", "
 
    func test_func() -> bool {
 
        auto a1 = (8, 16, 32);
 
        (u8, u16, u32) a2 = (8, 16, 32);
 
        auto b1 = ();
 
        () b2 = ();
 

	
 
        return a1 == a2 && a2 == (8, 16, 32) && b1 == b2 && b2 == ();
 
    }
 
    ").for_function("test_func", |f| { f
 
        .call_ok(Some(Value::Bool(true)));
 
    });
 

	
 
    Tester::new_single_source_expect_ok("tuple inequality", "
 
    func test_func() -> bool {
 
        auto a = (8, 16, 32);
 
        (u8, u16, u32) a_same = (8, 16, 32);
 
        auto a_diff = (0b111, 0b1111, 0b11111);
 
        auto b = ();
 
        return
 
            !(a != a_same) &&
 
            a != a_diff &&
 
            a != (8, 16, 320) &&
 
            !(b != ());
 
    }
 
    ").for_function("test_func", |f| { f
 
        .call_ok(Some(Value::Bool(true)));
 
    });
 
}
 

	
 
#[test]
 
fn test_tuple_select_operator() {
 
    Tester::new_single_source_expect_ok("tuple member assignment", "
 
    func test() -> bool {
 
        auto tuple = (0, 1, 0, 1);
 
        tuple.0 = cast<u8>(1);
 
        tuple.1 = cast<u16>(2);
 
        tuple.2 = cast<u32>(3);
 
        tuple.3 = cast<u64>(4);
 
        return cast(tuple.0) + cast(tuple.1) + tuple.2 + cast(tuple.3) == 10;
 
    }
 
    ").for_function("test", |f| { f
 
        .for_variable("tuple", |v| { v.assert_concrete_type("(u8,u16,u32,u64)"); })
 
        .call_ok(Some(Value::Bool(true)));
 
    });
 

	
 
    Tester::new_single_source_expect_ok("tuple polymorph member access", "
 
    func sum_variant_a<A,B,C,D>((A,B,C,D) v) -> C {
 
        return cast(v.0) + cast(v.1) + v.2 + cast(v.3);
 
    }
 
    func sum_variant_b<A,B,C,D>((A,B,C,D) i) -> B {
 
        (A,B,C,D) c = (0,0,0,0);
 
        c.0 = i.0; c.1 = i.1; c.2 = i.2; c.3 = i.3;
 
        return cast(c.0) + c.1 + cast(c.2) + cast(c.3);
 
    }
 
    func test() -> bool {
 
        (u8,u16,u32,u64) tuple = (1, 2, 3, 4);
 
        return sum_variant_a(tuple) == 10 && sum_variant_b(tuple) == 10;
 
    }
 
    ").for_function("test", |f| { f
 
        .call_ok(Some(Value::Bool(true)));
 
    });
 
}
 

	
 
#[test]
 
fn test_string_operators() {
 
    Tester::new_single_source_expect_ok("string concatenation", "
 
func create_concatenated(string left, string right) -> string {
 
    return left @ \", but also \" @ right;
 
}
 
func perform_concatenate(string left, string right) -> string {
 
    left @= \", but also \";
 
    left @= right;
 
    return left;
 
}
 
func foo() -> bool {
 
    auto left = \"Darth Vader\";
 
    auto right = \"Anakin Skywalker\";
 
    auto res1 = create_concatenated(left, right);
 
    auto res2 = perform_concatenate(left, right);
 
    auto expected = \"Darth Vader, but also Anakin Skywalker\";
 

	
 
    return
 
        res1 == expected &&
 
        res2 == \"Darth Vader, but also Anakin Skywalker\" &&
 
        res1 != \"This kind of thing\" && res2 != \"Another likewise kind of thing\";
 
}
 
    ").for_function("foo", |f| { f
src/protocol/tests/parser_inference.rs
Show inline comments
 
@@ -101,49 +101,87 @@ fn test_binary_expr_inference() {
 
        )
 
        .for_expression_by_source(
 
            "l0 + l1", "+", 
 
            |e| { e.assert_concrete_type("s64"); }
 
        );
 
    });
 

	
 
    Tester::new_single_source_expect_err(
 
        "incompatible types", 
 
        "func call() -> s32 {
 
            s8 b = 0;
 
            s64 l = 1;
 
            auto r = b + l;
 
            return 0;
 
        }"
 
    ).error(|e| { e
 
        .assert_ctx_has(0, "b + l")
 
        .assert_msg_has(0, "cannot apply")
 
        .assert_occurs_at(0, "+")
 
        .assert_msg_has(1, "has type 's8'")
 
        .assert_msg_has(2, "has type 's64'");
 
    });
 
}
 

	
 
#[test]
 
fn test_tuple_inference() {
 
    Tester::new_single_source_expect_ok(
 
        "from tuple to variable",
 
        "
 
        func test() -> u32 {
 
            (u8,u16,u32,u64) tuple = (1, 2, 3, 4);
 
            auto a = tuple.0;
 
            auto b = tuple.1;
 
            auto c = tuple.2;
 
            auto d = tuple.3;
 
            return cast(a) + cast(b) + c + cast(d);
 
        }
 
        "
 
    ).for_function("test", |f| { f
 
        .for_variable("a", |v| { v.assert_concrete_type("u8"); })
 
        .for_variable("b", |v| { v.assert_concrete_type("u16"); })
 
        .for_variable("c", |v| { v.assert_concrete_type("u32"); })
 
        .for_variable("d", |v| { v.assert_concrete_type("u64"); })
 
        .call_ok(Some(Value::UInt32(10)));
 
    });
 

	
 
    Tester::new_single_source_expect_ok(
 
        "from variable to tuple",
 
        "
 
        func test() -> u32 {
 
            auto tuple = (1, 2, 3, 4);
 
            u8  a = tuple.0;
 
            u16 b = tuple.1;
 
            u32 c = tuple.2;
 
            u64 d = tuple.3;
 
            return cast(a) + cast(b) + c + cast(d);
 
        }
 
        "
 
    ).for_function("test", |f| { f
 
        .for_variable("tuple", |v| { v.assert_concrete_type("(u8,u16,u32,u64)"); })
 
        .call_ok(Some(Value::UInt32(10)));
 
    });
 
}
 

	
 
#[test]
 
fn test_struct_inference() {
 
    Tester::new_single_source_expect_ok(
 
        "by function calls",
 
        "
 
        struct Pair<T1, T2>{ T1 first, T2 second }
 
        func construct<T1, T2>(T1 first, T2 second) -> Pair<T1, T2> {
 
            return Pair{ first: first, second: second };
 
        }
 
        func fix_t1<T2>(Pair<s8, T2> arg) -> s32 { return 0; }
 
        func fix_t2<T1>(Pair<T1, s32> arg) -> s32 { return 0; }
 
        func test() -> s32 {
 
            auto first = 0;
 
            auto second = 1;
 
            auto pair = construct(first, second);
 
            fix_t1(pair);
 
            fix_t2(pair);
 
            return 0;
 
        }
 
        "
 
    ).for_function("test", |f| { f
 
        .for_variable("first", |v| { v
 
            .assert_parser_type("auto")
src/protocol/tests/parser_validation.rs
Show inline comments
 
@@ -455,45 +455,77 @@ fn test_incorrect_tuple_polymorph_args() {
 
            return 0;
 
        }"
 
    ).error(|e| { e
 
        .assert_num(2)
 
        .assert_msg_has(0, "closing '>'").assert_occurs_at(0, ">)::None")
 
        .assert_msg_has(1, "match this '('").assert_occurs_at(1, "(u32>");
 
    });
 

	
 
    Tester::new_single_source_expect_err(
 
        "wrongly placed angle",
 
        "
 
        union O<T>{ S(T), N }
 
        func f() -> u32 {
 
            auto a = O<(<u32>)>::None;
 
            return 0;
 
        }
 
        "
 
    ).error(|e| { e
 
        .assert_num(1)
 
        .assert_msg_has(0, "expected typename")
 
        .assert_occurs_at(0, "<u32");
 
    });
 
}
 

	
 
#[test]
 
fn test_incorrect_tuple_member_access() {
 
    Tester::new_single_source_expect_err(
 
        "zero-tuple",
 
        "func foo() -> () { () a = (); auto b = a.0; return a; }"
 
    ).error(|e| { e
 
        .assert_num(1)
 
        .assert_msg_has(0, "out of bounds")
 
        .assert_occurs_at(0, "a.0");
 
    });
 

	
 
    // Make the type checker do some shenanigans before we can decide the tuple
 
    // type.
 
    Tester::new_single_source_expect_err(
 
        "sized tuple",
 
        "
 
        func determinator<A,B>((A,B,A) v) -> B { return v.1; }
 
        func tester() -> u64 {
 
            auto v = (0,1,2);
 
            u32 a_u32 = 5;
 
            v.2 = a_u32;
 
            v.8 = 5;
 
            return determinator(v);
 
        }
 
        "
 
    ).error(|e| { e
 
        .assert_num(1)
 
        .assert_msg_has(0, "out of bounds")
 
        .assert_occurs_at(0, "v.8");
 
    });
 
}
 

	
 
#[test]
 
fn test_polymorph_array_types() {
 
    Tester::new_single_source_expect_ok(
 
        "array of polymorph in struct",
 
        "
 
        struct Foo<T> { T[] hello }
 
        struct Bar { Foo<u32>[] world }
 
        "
 
    ).for_struct("Bar", |s| { s
 
        .for_field("world", |f| { f.assert_parser_type("Foo<u32>[]"); });
 
    });
 

	
 
    Tester::new_single_source_expect_ok(
 
        "array of port in struct",
 
        "
 
        struct Bar { in<u32>[] inputs }
 
        "
 
    ).for_struct("Bar", |s| { s
 
        .for_field("inputs", |f| { f.assert_parser_type("in<u32>[]"); });
 
    });
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)