Skip to content

compiletime match fails for unaliased_typ #25200

Description

@Larsimusrex

Describe the bug

trying to use $match with unaliased_typ results in a compilation error.

Reproduction Steps

fn generic_stuff[T](t T) {
	$match T.unaliased_typ {
		int {
			println('hi')
		}
	}
}

fn main () {
	generic_stuff(1)
}

Expected Behavior

outputs 'hi'

Current Behavior

test.v:2:9: error: `ast.SelectorExpr{
    or_block: ast.OrExpr{
        kind: absent
        pos: token.Pos{
            len: 0
            line_nr: 0
            pos: 0
            col: 0
            last_line: 0
        }
        scope: &nil
        err_used: false
        stmts: []
    }
    from_embed_types: []
    generic_from_embed_types: []
    field_name: 'unaliased_typ'
    expr: T
    scope: &# 3 - 85
  * var: t - ast.Type(0x800006b = 134217835)
    # 55 - 80

    pos: token.Pos{
        len: 1
        line_nr: 1
        pos: 35
        col: 8
        last_line: 1
    }
    mut_pos: token.Pos{
        len: 0
        line_nr: 0
        pos: 0
        col: 0
        last_line: 0
    }
    expr_type: ast.Type(0x0 = 0)
    typ: ast.Type(0x0 = 0)
    name_type: ast.Type(0x800006b = 134217835)
    next_token: unknown
    gkind_field: unaliased_typ
    is_mut: false
    has_hidden_receiver: false
    is_field_typ: false
}` is not `$for` field.name.
    1 | fn generic_stuff[T](t T) {
    2 |     $match T.unaliased_typ {
      |            ^
    3 |         int {
    4 |             println('hi')

Possible Solution

No response

Additional Information/Context

I also got a different error when I tried using it in my json encoder:

fn (mut encoder Encoder) encode_value[T](val T) {
	$match T.unaliased_typ {
		string {
			encoder.encode_string(workaround_cast[string](&val))
		} 
		bool {
			encoder.encode_boolean(workaround_cast[bool](&val))
		} u8 {
			encoder.encode_number(workaround_cast[u8](&val))
		} u16 {
			encoder.encode_number(workaround_cast[u16](&val))
		} u32 {
			encoder.encode_number(workaround_cast[u32](&val))
		} u64 {
			encoder.encode_number(workaround_cast[u64](&val))
		} i8 {
			encoder.encode_number(workaround_cast[i8](&val))
		} i16 {
			encoder.encode_number(workaround_cast[i16](&val))
		} int {
			encoder.encode_number(workaround_cast[int](&val))
		} i64 {
			encoder.encode_number(workaround_cast[i64](&val))
		} usize {
			encoder.encode_number(workaround_cast[usize](&val))
		} isize {
			encoder.encode_number(workaround_cast[isize](&val))
		} f32 {
			encoder.encode_number(workaround_cast[f32](&val))
		} f64 {
			encoder.encode_number(workaround_cast[f64](&val))
		} $array {
			encoder.encode_array(val)
		} $map {
			encoder.encode_map(val)
		} $enum {
			encoder.encode_enum(val)
		} $sumtype {
			encoder.encode_sumtype(val)
		} $struct {
			encoder.encode_struct(val)
		}
	}
}

resulted in:

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: 7831fb0
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:19641: at _v_panic: Backtrace
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:37278: by v__ast__default_table_panic_handler
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:37284: by v__ast__Table_panic
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:37983: by v__ast__Table_sym
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:56314: by v__checker__Checker_smartcast
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:4830: by v__checker__Checker_match_exprs
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:4117: by v__checker__Checker_match_expr
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:54784: by v__checker__Checker_expr
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:53582: by v__checker__Checker_stmt
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:54240: by v__checker__Checker_stmts_ending_with_expression
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:54217: by v__checker__Checker_stmts
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:63344: by v__checker__Checker_fn_decl
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:299: by v__checker__Checker_post_process_generic_fns
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:51685: by v__checker__Checker_check_files
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:53227: by v__builder__Builder_middle_stages
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:53268: by v__builder__Builder_front_and_middle_stages
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:55798: by v__builder__cbuilder__gen_c
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:55770: by v__builder__cbuilder__build_c
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:55761: by v__builder__cbuilder__compile_c
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:55678: by v__builder__Builder_rebuild
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:54830: by v__builder__compile
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:56199: by main__rebuild
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:56112: by main__main
/tmp/v_1000/v.01K3VHCMXPCWGZD7T78ACTKBPD.tmp.c:57020: by main

V version

V 0.4.11

Environment details (OS name and version, etc.)

V full version V 0.4.11 7831fb0.2ac3478
OS linux, "Void Linux"
Processor 12 cpus, 64bit, little endian, AMD Ryzen 5 5600G with Radeon Graphics
Memory 6.64GB/15.52GB
V executable /home/lars/v/v
V last modified time 2025-08-30 16:44:56
V home dir OK, value: /home/lars/v
VMODULES OK, value: /home/lars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir contains spaces, value: /home/lars/Documents/V Programs/encoder2
Git version git version 2.50.1
V git status weekly.2025.06-1106-g2ac34782
.git/config present true
cc version cc (GCC) 14.2.1 20250405
gcc version gcc (GCC) 14.2.1 20250405
clang version clang version 17.0.6
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.3 (a9651ff57165f5710bb09a5fe52590fd6ddb72df)
glibc version ldd (GNU libc) 2.41

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.ComptimeFeatures processed during compile time, like $if, $for, $env etcUnit: CheckerBugs/feature requests, that are related to the type checker.

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