From 6084c0fc54253cf1b5b9c90db11e4157bae7c556 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 6 Sep 2021 08:15:53 +0800 Subject: [PATCH] checker, cgen: fix complex generic sumtype (fix #11392) (#11400) --- vlib/v/checker/checker.v | 2 +- vlib/v/gen/c/cgen.v | 19 ++- vlib/v/tests/generic_complex_sumtype_test.v | 145 ++++++++++++++++++++ 3 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 vlib/v/tests/generic_complex_sumtype_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2765375fa1..b26494084d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1152,7 +1152,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { } } } else if expr_type != ast.void_type && expr_type_sym.kind != .placeholder { - c.check_expected(expr_type, field_info.typ) or { + c.check_expected(c.unwrap_generic(expr_type), c.unwrap_generic(field_info.typ)) or { c.error('cannot assign to field `$field_info.name`: $err.msg', field.pos) } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 6b47b4108b..3c7804d504 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2029,8 +2029,17 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ exp_styp := g.typ(expected_type) got_styp := g.typ(got_type) if expected_type != ast.void_type { - expected_deref_type := if expected_is_ptr { expected_type.deref() } else { expected_type } - got_deref_type := if got_is_ptr { got_type.deref() } else { got_type } + unwrapped_expected_type := g.unwrap_generic(expected_type) + unwrapped_got_type := g.unwrap_generic(got_type) + unwrapped_exp_sym := g.table.get_type_symbol(unwrapped_expected_type) + unwrapped_got_sym := g.table.get_type_symbol(unwrapped_got_type) + + expected_deref_type := if expected_is_ptr { + unwrapped_expected_type.deref() + } else { + unwrapped_expected_type + } + got_deref_type := if got_is_ptr { unwrapped_got_type.deref() } else { unwrapped_got_type } if g.table.sumtype_has_variant(expected_deref_type, got_deref_type) { mut is_already_sum_type := false scope := g.file.scope.innermost(expr.position().pos) @@ -2050,9 +2059,9 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ g.prevent_sum_type_unwrapping_once = true g.expr(expr) } else { - g.write_sumtype_casting_fn(got_type, expected_type) - fname := '${got_sym.cname}_to_sumtype_$exp_sym.cname' - g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, exp_sym.cname, + g.write_sumtype_casting_fn(unwrapped_got_type, unwrapped_expected_type) + fname := '${unwrapped_got_sym.cname}_to_sumtype_$unwrapped_exp_sym.cname' + g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname, got_is_ptr, got_styp) } return diff --git a/vlib/v/tests/generic_complex_sumtype_test.v b/vlib/v/tests/generic_complex_sumtype_test.v new file mode 100644 index 0000000000..06f2789701 --- /dev/null +++ b/vlib/v/tests/generic_complex_sumtype_test.v @@ -0,0 +1,145 @@ +struct Empty {} + +struct Node { + value T + left Tree + right Tree +} + +type Tree = Empty | Node + +// return size(number of nodes) of BST +fn size(tree Tree) int { + return match tree { + Empty { 0 } + Node { 1 + size(tree.left) + size(tree.right) } + } +} + +// insert a value to BST +fn insert(tree Tree, x T) Tree { + return match tree { + Empty { + Node{x, tree, tree} + } + Node { + if x == tree.value { + tree + } else if x < tree.value { + Node{ + ...tree + left: insert(tree.left, x) + } + } else { + Node{ + ...tree + right: insert(tree.right, x) + } + } + } + } +} + +// whether able to find a value in BST +fn search(tree Tree, x T) bool { + return match tree { + Empty { + false + } + Node { + if x == tree.value { + true + } else if x < tree.value { + search(tree.left, x) + } else { + search(tree.right, x) + } + } + } +} + +// find the minimal value of a BST +fn min(tree Tree) T { + return match tree { + Empty { + 1e100 + } + Node { + if tree.value < (min(tree.left)) { + tree.value + } else { + min(tree.left) + } + } + } +} + +// delete a value in BST (if nonexistant do nothing) +fn delete(tree Tree, x T) Tree { + return match tree { + Empty { + tree + } + Node { + if tree.left is Node && tree.right is Node { + if x < tree.value { + Node{ + ...tree + left: delete(tree.left, x) + } + } else if x > tree.value { + Node{ + ...tree + right: delete(tree.right, x) + } + } else { + Node{ + ...tree + value: min(tree.right) + right: delete(tree.right, min(tree.right)) + } + } + } else if tree.left is Node { + if x == tree.value { + tree.left + } else { + Node{ + ...tree + left: delete(tree.left, x) + } + } + } else { + if x == tree.value { + tree.right + } else { + Node{ + ...tree + right: delete(tree.right, x) + } + } + } + } + } +} + +fn test_generics_complex_sumtype() { + mut tree := Tree(Empty{}) + input := [0.3, 0.2, 0.5, 0.0, 0.6, 0.8, 0.9, 1.0, 0.1, 0.4, 0.7] + for i in input { + tree = insert(tree, i) + } + println('[1] after insertion tree size is ${size(tree)}') // 11 + del := [-0.3, 0.0, 0.3, 0.6, 1.0, 1.5] + for i in del { + tree = delete(tree, i) + } + print('[2] after deletion tree size is ${size(tree)}, ') // 7 + print('and these elements were deleted: ') // 0.0 0.3 0.6 1.0 + for i in input { + if !search(tree, i) { + print('$i ') + } + } + println('') + assert size(tree) == 7 +}