Skip to content

cgen: ?SumType as SumType accesses the option wrapper without unwrapping (no member named '_main__...') #27403

@medvednikov

Description

@medvednikov

Description

Casting an option-of-sumtype field with as SumType (i.e. ?Type as Type, where Type is a sumtype) generates C that accesses the sumtype-variant members (._main__Type, ._typ) directly on the _option_main__Type option wrapper, instead of first unwrapping the option's .data. The C compiler then rejects it because the option wrapper struct has no such members.

A plain sumtype as cast (no option) compiles correctly, isolating the fault to the ?SumType as SumType case.

V version: V 0.5.1, current master (3690f882f)
OS/Backend: reproduced on macOS arm64; originally reported on Linux/cc.

Reproducer

module main

type Type = TypeBasic | TypeFunc

struct TypeBasic {
	name string
}

struct TypeFunc {
	variadic_type ?Type
}

fn f(tf TypeFunc) Type {
	return tf.variadic_type as Type
}

fn main() {
	tf := TypeFunc{
		variadic_type: Type(TypeBasic{name: 'v'})
	}
	println(f(tf))
}

Compile with:

v file.v

Current behavior

error: no member named '_main__Type' in 'struct _option_main__Type'
  return *(main__Type*)builtin____as_cast((tf.variadic_type)._main__Type, (tf.variadic_type)._typ, 116);
error: no member named '_typ' in 'struct _option_main__Type'

The as Type cast emits (tf.variadic_type)._main__Type / (tf.variadic_type)._typ, but tf.variadic_type has type _option_main__Type (the option wrapper) — the option must be unwrapped (.data) before the sumtype-variant access.

Expected behavior

The option should be unwrapped before the sumtype as cast, so the program compiles.

Notes

  • Reported via the bugs.vlang.io crash reporter. Original instance: iris/checker.v, sym.type.variadic_type as Type where variadic_type is ?Type and Type is a sumtype (also surfaced as expected expression before '}' token in some report variants).

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

    bugs.vlang.ioReported via the bugs.vlang.io crash reporter

    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