Skip to content

orm: cgen error when a option on a struct is type narrowed #26496

@Le0Developer

Description

@Le0Developer
V version: V 0.5.0 75fc8be, press to see full `v doctor` output
V full version V 0.5.0 ba1cfcc.75fc8be
OS macos, macOS, 26.2, 25C56
Processor 10 cpus, 64bit, little endian, Apple M4
Memory 0.25GB/16GB
V executable /Users/leodev/p/v/v/v
V last modified time 2026-02-01 13:34:00
V home dir OK, value: /Users/leodev/p/v/v
VMODULES OK, value: /Users/leodev/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/leodev/p/v/test
Git version git version 2.52.0
V git status weekly.2026.05-80-g75fc8bed
.git/config present true
cc version Apple clang version 17.0.0 (clang-1700.0.13.3)
gcc version Apple clang version 17.0.0 (clang-1700.0.13.3)
clang version Apple clang version 17.0.0 (clang-1700.0.13.3)
tcc version tcc version 0.9.28rc 2026-01-10 HEAD@5ec0e6f8 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 f995efa3
emcc version N/A
glibc version N/A

What did you do?
./v -g -o vdbg cmd/v && ./vdbg repro.v && repro

import db.sqlite

@[table: 'foos']
struct Foo {
	value int
}

struct State {
	val ?int
}

fn 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)
	}
}

What did you see?

cgen error: repro.v:20: ORM: unknown type t == 0
expr: state.val
last SQL stmt:
VV_LOC void main__main(void) {
	_option_int val = (_option_int){ .state=2, .err=_const_none__, .data={E_STRUCT} };
	main__State state = ((main__State){.val = val,});
	_result_db__sqlite__DB _t1 = db__sqlite__connect(_S(":memory:"));
	if (_t1.is_error) {
		builtin__panic_result_not_set(IError_name_table[_t1.err._typ]._method_msg(_t1.err._object));
	}
	
 	db__sqlite__DB *db = HEAP(db__sqlite__DB, ((*(db__sqlite__DB*)_t1.data)));
	if ((state.val).state != 2) {

		// ORM
		orm__Connection _t2 = (orm__Connection){._db__sqlite__DB = &(*(db)), ._typ = _orm__Connection_db__sqlite__DB_index};
		_result_Array_main__Foo _t3;
		// sql { select from `foos` }
		_result_Array_Array_orm__Primitive _t4 = orm__Connection_name_table[_t2._typ]._method_select(
			_t2._object, // Connection object
			(orm__SelectConfig){
				.table = 
				((orm__Table){
					.name = _S("foos"),
					.attrs = builtin__new_array_from_c_array(1, 1, sizeof(VAttribute),
						_MOV((VAttribute[1]){(VAttribute){ .name = _S("table"), .has_arg = true, .arg = _S("foos"), .kind = 1,},})
					)
				}),
				.is_count = false,
				.has_where = true,
				.has_order = false,
				.has_limit = false,
				.has_offset = false,
				.has_distinct = false,
				.fields = builtin__new_array_from_c_array(1, 1, sizeof(string),
					_MOV((string[1]){
						_S("value"),
					})
				),
				.types = builtin__new_array_from_c_array(1, 1, sizeof(int),
					_MOV((int[1]){ 8, })
				),
				.joins = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__JoinConfig), 0),
			},
			(orm__QueryData) {
				.types = builtin____new_array_with_default_noscan(0, 0, sizeof(int), 0),
				.kinds = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__OperationKind), 0),
				.is_and = builtin____new_array_with_default_noscan(0, 0, sizeof(bool), 0),
				.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_int), 0),
				.data = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__Primitive), 0
			)},
			// ORM where
			(orm__QueryData){
				.types = builtin____new_array_with_default_noscan(0, 0, sizeof(int), 0),
				.fields = builtin__new_array_from_c_array(1, 1, sizeof(string),
					_MOV((string[1]){
						_S("value"),
					})
				),
				.data = builtin__new_array_from_c_array(1, 1, sizeof(orm__Primitive),
					_MOV((orm__Primitive[1]){

What did you expect to see?

Should compile

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions