Skip to content

Error using optional field with generic struct #16962

@zt64

Description

@zt64

Describe the bug

An internal error occurs during compilation when trying to use an optional field.

Expected Behavior

No error should occur when compiling.

Current Behavior

v/vlib/x/json2/encoder.v:168:12: cgen error: checker bug; node.right_types.len < node.left.len
  166 | 
  167 |         $if field.is_option {
  168 |             is_none := value.str() == 'Option(error: none)'
      |                     ~~
  169 | 
  170 |             if !is_none {

Reproduction Steps

module main

import x.json2

pub struct Struct1 {
	test Struct2
}

struct Struct2 {
	name ?string = none
}

struct Payload[T] {
	value T
}

fn main() {
	json2.encode(Payload[Struct1]{
		value: Struct1{
			test: Struct2{
				name: 'test'
			}
		}
	})
}

Possible Solution

The error results from the name field in Struct2. Changing it to just name string fixes the error.

Additional Information/Context

No response

V version

0.3.2 ba091a3

Environment details (OS name and version, etc.)

OS: linux, "Arch Linux"
Processor: 12 cpus, 64bit, little endian, Intel(R) Core(TM) i5-10600K CPU @ 4.10GHz
CC version: cc (GCC) 12.2.0

getwd: /home/nick
vmodules: /home/nick/.vmodules
vroot: /home/nick/.local/share/v
vexe: /home/nick/.local/share/v/v
vexe mtime: 2023-01-12 22:41:48
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.2 fd04c1a.ba091a3

Git version: git version 2.39.0
Git vroot status: weekly.2023.02-16-gba091a36
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions