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

table: check sumtype of fntype assign error (#15685)

This commit is contained in:
yuyi 2022-09-07 21:05:38 +08:00 committed by GitHub
parent 2693935066
commit 8627af18dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -1308,7 +1308,14 @@ pub fn (t &Table) sumtype_has_variant(parent Type, variant Type, is_as bool) boo
fn (t &Table) sumtype_check_function_variant(parent_info SumType, variant Type, is_as bool) bool { fn (t &Table) sumtype_check_function_variant(parent_info SumType, variant Type, is_as bool) bool {
for v in parent_info.variants { for v in parent_info.variants {
if '$v.idx' == '$variant.idx' && (!is_as || v.nr_muls() == variant.nr_muls()) { v_sym := t.sym(v)
if v_sym.kind != .function {
continue
}
v_fn := (v_sym.info as FnType).func
variant_fn := (t.sym(variant).info as FnType).func
if t.fn_type_source_signature(v_fn) == t.fn_type_source_signature(variant_fn)
&& (!is_as || v.nr_muls() == variant.nr_muls()) {
return true return true
} }
} }

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/sumtype_of_fntype_assign_err.vv:14:11: error: cannot assign to `m['f']`: expected `Expr`, not `fn () Expr`
12 | mut m := map[string]Expr{}
13 |
14 | m['f'] = f
| ^
15 | }

View File

@ -0,0 +1,15 @@
type Expr = fn (int) int | int
fn id(n int) int {
return n
}
fn f() Expr {
return id
}
fn main() {
mut m := map[string]Expr{}
m['f'] = f
}