mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
comptime: fix $match with fn type (#25271)
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
This commit is contained in:
parent
4ea05636fb
commit
919c68e6f9
2 changed files with 43 additions and 4 deletions
|
@ -145,10 +145,18 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
|
||||||
node.cond)
|
node.cond)
|
||||||
c_str = '${expr} == ${c.table.type_to_str(branch_type)}'
|
c_str = '${expr} == ${c.table.type_to_str(branch_type)}'
|
||||||
} else {
|
} else {
|
||||||
|
is_function := c.table.final_sym(node.cond_type).kind == .function
|
||||||
|
if !is_function {
|
||||||
// $match a { $int {}
|
// $match a { $int {}
|
||||||
comptime_match_branch_result = c.check_compatible_types(node.cond_type,
|
comptime_match_branch_result = c.check_compatible_types(node.cond_type,
|
||||||
'${node.cond}', expr)
|
'${node.cond}', expr)
|
||||||
c_str = '${c.table.type_to_str(node.cond_type)} == ${expr}'
|
c_str = '${c.table.type_to_str(node.cond_type)} == ${expr}'
|
||||||
|
} else {
|
||||||
|
// $match T { FnType {} }
|
||||||
|
branch_type := c.get_expr_type(expr)
|
||||||
|
comptime_match_branch_result = c.table.type_to_str(node.cond_type) == c.table.type_to_str(branch_type)
|
||||||
|
c_str = '${comptime_match_branch_result} == true'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if comptime_match_branch_result {
|
if comptime_match_branch_result {
|
||||||
break
|
break
|
||||||
|
|
31
vlib/v/tests/comptime/comptime_match_fntype_test.v
Normal file
31
vlib/v/tests/comptime/comptime_match_fntype_test.v
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
type FN1 = fn (a int) int
|
||||||
|
|
||||||
|
type FN2 = fn (a int, b int) int
|
||||||
|
|
||||||
|
type FN3 = FN1 | FN2
|
||||||
|
|
||||||
|
fn invoke[T](cb T) int {
|
||||||
|
$match T {
|
||||||
|
FN1 {
|
||||||
|
return cb(1)
|
||||||
|
}
|
||||||
|
FN2 {
|
||||||
|
return cb(1, 2)
|
||||||
|
}
|
||||||
|
$else {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
ret1 := invoke(fn (a int) int {
|
||||||
|
return a
|
||||||
|
})
|
||||||
|
assert ret1 == 1
|
||||||
|
|
||||||
|
ret2 := invoke(fn (a int, b int) int {
|
||||||
|
return a + b
|
||||||
|
})
|
||||||
|
assert ret2 == 3
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue