From e8ad4adb65135a1aa61320619ad599fddf3c0d45 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 2 Feb 2026 22:01:49 +0200 Subject: [PATCH] checker: fix sql statement using an unwrapped option value from an if guard (fix #26496) (using codex) (#26505) --- vlib/db/sqlite/sqlite_orm_option_field_test.v | 26 +++++++++++++++++++ vlib/v/checker/checker.v | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 vlib/db/sqlite/sqlite_orm_option_field_test.v diff --git a/vlib/db/sqlite/sqlite_orm_option_field_test.v b/vlib/db/sqlite/sqlite_orm_option_field_test.v new file mode 100644 index 000000000..7a0b857d3 --- /dev/null +++ b/vlib/db/sqlite/sqlite_orm_option_field_test.v @@ -0,0 +1,26 @@ +// vtest build: present_sqlite3? && !windows && !sanitize-memory-clang && !docker-ubuntu-musl +import db.sqlite + +@[table: 'foos'] +struct Foo { + value int +} + +struct State { + val ?int +} + +fn test_main() { + val := ?int(none) + state := State{val} + db := sqlite.connect(':memory:')! + if state.val != none { + v := sql db { + select from Foo where value < state.val + }! + println(v) + assert false + } else { + assert true + } +} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 32b034573..92d841011 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1983,7 +1983,11 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { if !prevent_sum_type_unwrapping_once { scope_field := node.scope.find_struct_field(node.expr.str(), typ, field_name) if scope_field != unsafe { nil } { - return scope_field.smartcasts.last() + sf_smartcast_type := scope_field.smartcasts.last() + if c.inside_sql { + node.typ = sf_smartcast_type + } + return sf_smartcast_type } } } -- 2.39.5