1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker, cgen: fix complex generic sumtype (fix #11392) (#11400)

This commit is contained in:
yuyi 2021-09-06 08:15:53 +08:00 committed by GitHub
parent 87f38bf6e7
commit 6084c0fc54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 160 additions and 6 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -0,0 +1,145 @@
struct Empty {}
struct Node<T> {
value T
left Tree<T>
right Tree<T>
}
type Tree<T> = Empty | Node<T>
// return size(number of nodes) of BST
fn size<T>(tree Tree<T>) int {
return match tree {
Empty { 0 }
Node<T> { 1 + size<T>(tree.left) + size<T>(tree.right) }
}
}
// insert a value to BST
fn insert<T>(tree Tree<T>, x T) Tree<T> {
return match tree {
Empty {
Node<T>{x, tree, tree}
}
Node<T> {
if x == tree.value {
tree
} else if x < tree.value {
Node<T>{
...tree
left: insert<T>(tree.left, x)
}
} else {
Node<T>{
...tree
right: insert<T>(tree.right, x)
}
}
}
}
}
// whether able to find a value in BST
fn search<T>(tree Tree<T>, x T) bool {
return match tree {
Empty {
false
}
Node<T> {
if x == tree.value {
true
} else if x < tree.value {
search<T>(tree.left, x)
} else {
search<T>(tree.right, x)
}
}
}
}
// find the minimal value of a BST
fn min<T>(tree Tree<T>) T {
return match tree {
Empty {
1e100
}
Node<T> {
if tree.value < (min<T>(tree.left)) {
tree.value
} else {
min<T>(tree.left)
}
}
}
}
// delete a value in BST (if nonexistant do nothing)
fn delete<T>(tree Tree<T>, x T) Tree<T> {
return match tree {
Empty {
tree
}
Node<T> {
if tree.left is Node<T> && tree.right is Node<T> {
if x < tree.value {
Node<T>{
...tree
left: delete<T>(tree.left, x)
}
} else if x > tree.value {
Node<T>{
...tree
right: delete<T>(tree.right, x)
}
} else {
Node<T>{
...tree
value: min<T>(tree.right)
right: delete<T>(tree.right, min<T>(tree.right))
}
}
} else if tree.left is Node<T> {
if x == tree.value {
tree.left
} else {
Node<T>{
...tree
left: delete<T>(tree.left, x)
}
}
} else {
if x == tree.value {
tree.right
} else {
Node<T>{
...tree
right: delete<T>(tree.right, x)
}
}
}
}
}
}
fn test_generics_complex_sumtype() {
mut tree := Tree<f64>(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
}