v icon indicating copy to clipboard operation
v copied to clipboard

Error in setting default value for option types to `none` in nested struct

Open impopular-guy opened this issue 1 year ago • 0 comments

Describe the bug

  • Setting default value of option type to none causes error. Anything else runs successfully.
  • Also it prints incorrectly
    d := Data{} // see reproduction steps for definition
    println(d)
    println(d.a)
    
    gives the output
    Data{
        a: 0
        b: 1
        c: 0
    }
    Option(error: none)
    
    I think it should be
    Data{
        a: none
        b: 1
        c: none
    }
    Option(none)
    

Expected Behavior

Should run successfully with a warning.

Current Behavior

examples/check_encode.v:40:11: warning: unnecessary default value of `none`: struct fields are zeroed by default
   38 |     a ?int
   39 |     b ?int = 1
   40 |     c ?int = none
      |              ~~~~
   41 | }
   42 |
==================
C:/Users/dhrit/AppData/Local/Temp/v_0/check_encode.2632598036814096107.tmp.c:7630: warning: cast between pointer and integer of different size
C:/Users/dhrit/AppData/Local/Temp/v_0/check_encode.2632598036814096107.tmp.c:12490: error: field expected
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang 

Reproduction Steps

module main

struct Data {
	a ?int
	b ?int = 1
	c ?int = none // error
}

struct Data2 {
	d Data
}

fn main() {
	d := Data2{}
	println(d)
}

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 9c511e0

Environment details (OS name and version, etc.)

OS: windows, Microsoft Windows 11 Home Single Language v22621 64-bit
Processor: 8 cpus, 64bit, little endian, 
CC version: Error: exec failed (CreateProcess) with code 2: The system cannot find the file specified.
 cmd: cc --version

getwd: C:\Users\dhrit\.vmodules\vbson
vmodules: C:\Users\dhrit\.vmodules
vroot: C:\BIN\v
vexe: C:\BIN\v\v.exe
vexe mtime: 2023-02-26 08:44:50
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.3 9794a23.9c511e0

Git version: git version 2.35.1.windows.2
Git vroot status: weekly.2023.07-53-g9c511e03
.git/config present: true
thirdparty/tcc status: thirdparty-windows-amd64 1e6e7c6f

impopular-guy avatar Feb 26 '23 09:02 impopular-guy