Skip to content

Compiler panic when instantiating generic struct with interface/sumtype reference #25889

Description

@wenxuanjun

Describe the bug

The compiler panic when instantiating a generic struct. The panic occurs under a specific combination of conditions:

  1. A generic struct (struct Queue[T]) is defined after the function (some_func) that instantiates it
  2. The struct contains a field that is a reference/pointer to an interface, a sumtype, or the generic type itself (e.g., &Speaker, &Event, or &T)

If I change the field type to a concrete struct reference (e.g., &Cat) or move the struct definition before the usage, the code compiles successfully.

Reproduction Steps

interface Speaker {
	speak()
}

struct Cat {}

fn (c &Cat) speak() {}

struct EventA {
	a u32
}

struct EventB {
	b u32
}

type Event = EventA | EventB

fn some_func() {
	queue := Queue.new[Speaker]()
}

struct Queue[T] {
mut:
	data &T = unsafe { nil }
}

fn Queue.new[T]() Queue[T] {
	return Queue[T]{}
}

fn main() {
    some_func()
}

Expected Behavior

The code should compile successfully regardless of the declaration order.

Current Behavior

The compiler crashes with the following panic:

V panic: table.sym: invalid type (typ=ast.Type(0x0 = 0) idx=0). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.

 v hash: d9f1e33
    pid: 0xc2127
    tid: 0xc2132
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:19978: at builtin___v_panic: Backtrace
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:37647: by v__ast__default_table_panic_handler
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:37653: by v__ast__Table_panic
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:38355: by v__ast__Table_sym
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:33408: by v__gen__c__Gen_expr_with_cast
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:55586: by v__gen__c__Gen_zero_struct_field
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:55369: by v__gen__c__Gen_struct_init
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:34569: by v__gen__c__Gen_expr
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:34565: by v__gen__c__Gen_expr
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:33564: by v__gen__c__Gen_expr_with_cast
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:37413: by v__gen__c__Gen_return_stmt
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:33000: by v__gen__c__Gen_stmt
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:32664: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:32503: by v__gen__c__Gen_stmts
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:43235: by v__gen__c__Gen_gen_fn_decl
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:42877: by v__gen__c__Gen_gen_fn_decl
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:42786: by v__gen__c__Gen_fn_decl
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:32888: by v__gen__c__Gen_stmt
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:32664: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:32503: by v__gen__c__Gen_stmts
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:31309: by v__gen__c__Gen_gen_file
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:31248: by v__gen__c__cgen_process_one_file_cb
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:33656: by sync__pool__process_in_thread
/tmp/v_1000/v2.01KBHPZTRSRMD1X332H4ZCPK7B.tmp.c:16430: by sync__pool__process_in_thread_thread_wrapper
0x7f2945c9698b: by ???

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.12 d9f1e33

Environment details (OS name and version, etc.)

V 0.4.12 d9f1e33

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

Labels

BugThis tag is applied to issues which reports bugs.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions