From 17b54cde977886740c574ac3115de73fa6076cc5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 12 Sep 2022 18:55:34 +0800 Subject: [PATCH] checker: fix match with complex sumtype exprs that adding print (#15728) --- vlib/v/checker/if.v | 5 + ...ith_complex_sumtype_exprs_add_print_test.v | 155 ++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 vlib/v/tests/match_with_complex_sumtype_exprs_add_print_test.v diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 2af8b14868..6b914bd14e 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -188,6 +188,11 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } } c.expected_type = former_expected_type + if c.table.type_kind(c.expected_type) == .sum_type + && c.table.is_sumtype_or_in_variant(c.expected_type, node.typ) { + node.is_expr = true + node.typ = c.expected_type + } if c.expected_type.has_flag(.optional) || c.expected_type.has_flag(.result) { if node.typ == ast.void_type { node.is_expr = true diff --git a/vlib/v/tests/match_with_complex_sumtype_exprs_add_print_test.v b/vlib/v/tests/match_with_complex_sumtype_exprs_add_print_test.v new file mode 100644 index 0000000000..7f25b1c86e --- /dev/null +++ b/vlib/v/tests/match_with_complex_sumtype_exprs_add_print_test.v @@ -0,0 +1,155 @@ +struct Empty {} + +struct Node { + value f64 + 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 f64) 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 f64) 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) f64 { + 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 f64) Tree { + return match tree { + Empty { + tree + } + Node { + if tree.left is Node && tree.right is Node { + if x < tree.value { + println(x) + Node{ + ...tree + left: delete(tree.left, x) + } + } else if x > tree.value { + println(x) + 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_match_with_complex_sumtype_exprs() { + 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) + } + print('[1] after insertion tree size is ') // 11 + println(size(tree)) + assert 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 ') // 7 + print(size(tree)) + assert size(tree) == 7 + print(', and these elements were deleted: ') // 0.0 0.3 0.6 1.0 + mut deleted := []f64{} + for i in input { + if !search(tree, i) { + print(i) + print(' ') + deleted << i + } + } + deleted.sort() + assert deleted == [0.0, 0.3, 0.6, 1.0] + println('') +}