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

cgen: fix f as Fun, where f is type Expr = Fun | int, and struct Fun { f fn (int) int }. (fix #15730) (#15744)

This commit is contained in:
shove 2022-09-13 15:19:58 +08:00 committed by GitHub
parent adc3b25f52
commit 7cff7fb828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -1342,7 +1342,16 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
if obj := node.scope.find(node.name) {
match obj {
ast.Var {
if obj.smartcasts.len > 0 {
// Temp fix generate call fn error when the struct type of sumtype
// has the fn field and is same to the struct name.
mut is_need_cast := true
if node.left_type != 0 {
left_sym := g.table.sym(node.left_type)
if left_sym.kind == .struct_ && node.name == obj.name {
is_need_cast = false
}
}
if obj.smartcasts.len > 0 && is_need_cast {
for _ in obj.smartcasts {
g.write('(*')
}

View File

@ -0,0 +1,13 @@
type Expr = Fun | int
struct Fun {
f fn (int) int
}
fn test_sumtype_with_struct_fn_field_call() {
f := Expr(0)
if f is Fun {
println((f as Fun).f(0))
}
assert true
}