From d9eff3494e05a137039a33fee267561b1b9dfe15 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:20 +0300 Subject: [PATCH] checker: fix sumtype else branch of if !is should consider smartcast (fixes #13587) --- vlib/v/checker/if.v | 3 +++ .../sumtype_not_is_else_smartcast_test.v | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 vlib/v/tests/sumtypes/sumtype_not_is_else_smartcast_test.v diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 182915222..a86231779 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -751,6 +751,9 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co c.smartcast(mut first_cond.left, first_cond.left_type, first_cond.left_type.clear_flag(.option), mut scope, false, true, false) } + } else if first_cond.left in [ast.Ident, ast.SelectorExpr] && first_cond.op == .not_is { + c.smartcast(mut first_cond.left, first_cond.left_type, first_cond.right_type, mut + scope, false, false) } } } diff --git a/vlib/v/tests/sumtypes/sumtype_not_is_else_smartcast_test.v b/vlib/v/tests/sumtypes/sumtype_not_is_else_smartcast_test.v new file mode 100644 index 000000000..fa85fe07f --- /dev/null +++ b/vlib/v/tests/sumtypes/sumtype_not_is_else_smartcast_test.v @@ -0,0 +1,18 @@ +type NotIsElseSumtype = NotIsElseBb | NotIsElseCc + +struct NotIsElseBb { + b int +} + +struct NotIsElseCc { + c int +} + +fn test_sumtype_not_is_else_smartcast() { + aa := NotIsElseSumtype(NotIsElseBb{0}) + if aa !is NotIsElseBb { + assert false + } else { + assert aa.b == 0 + } +} -- 2.39.5