v icon indicating copy to clipboard operation
v copied to clipboard

Error using optional field with generic struct

Open zt64 opened this issue 1 year ago • 1 comments

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

zt64 avatar Jan 12 '23 22:01 zt64

It seems just occurs when inside a method, I can't reproduce it inside a function.

felipensp avatar Mar 10 '23 18:03 felipensp