diff --git a/vlib/v/checker/tests/comptime_if_optional_string_test.out b/vlib/v/checker/tests/comptime_if_optional_string_test.out new file mode 100644 index 0000000000..73aae9a35c --- /dev/null +++ b/vlib/v/checker/tests/comptime_if_optional_string_test.out @@ -0,0 +1 @@ +202201 \ No newline at end of file diff --git a/vlib/v/checker/tests/comptime_if_optional_string_test.vv b/vlib/v/checker/tests/comptime_if_optional_string_test.vv new file mode 100644 index 0000000000..df4aeb6a5b --- /dev/null +++ b/vlib/v/checker/tests/comptime_if_optional_string_test.vv @@ -0,0 +1,17 @@ +module main + +struct Foo { + a string + b ?string +} + +fn test_main() { + $for field in Foo.fields { + print(field.typ) + $if field.typ is ?string { + print(1) + } $else $if field.typ is string { + print(2) + } + } +} diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index ae770a91fe..30e413a3d0 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -434,7 +434,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) bool { } if cond.op == .key_is { - g.write('${exp_type.idx()} == ${got_type.idx()}') + g.write('${exp_type.idx()} == ${got_type.idx()} && ${exp_type.has_flag(.optional)} == ${got_type.has_flag(.optional)}') return exp_type == got_type } else { g.write('${exp_type.idx()} != ${got_type.idx()}')