vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.79k stars 2.16k forks source link

Assigning `nil` to `string` field should be forbidden due to TCC failure on Windows and Linux #18719

Closed i582 closed 1 year ago

i582 commented 1 year ago

Describe the bug

Code: https://vosca.dev/p/a0a13d5e07

struct Foo {
    name string
}

a := &Foo{
    name: unsafe { nil }
}
println(a)

Expected Behavior

Checker error

Current Behavior

Output:

&Foo{
    name: ''
}

Everything works well with GCC, however we ran into a problem compiling v-analyzer on Windows and Linux as TCC was unable to compile code with such assignment:

/tmp/v_1000/v-analyzer.tmp.c:84135: error: field expected
builder error: 

Thank you @Ekopalypse for help with debug!

Reproduction Steps

Run code above

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.4 fc4c431.45f16a2

Environment details (OS name and version, etc.)

V full version: V 0.3.5 fc4c431.015ccc2
OS: linux, Ubuntu 22.04.2 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz

getwd: /home/pmakhnev/playground
vexe: /home/pmakhnev/v/v
vexe mtime: 2023-06-30 00:00:09

vroot: OK, value: /home/pmakhnev/v
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.34.1
Git vroot status: 0.3.5
.git/config present: true

CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
JalonSolov commented 1 year ago

I don't think nil should be an assignable value, in unsafe or not.

Delta456 commented 1 year ago

Where exactly will be the usage of unsafe then? I think this still has to be made clear.

JalonSolov commented 1 year ago

If you're allowed to assign nil, then it should only be allowed in an unsafe block.

The problem here is that the code is assigning nil to a string field, and that's wrong regardless of unsafe or not. A string can never be nil in V. It is either empty, or has a valid string, but never nil since it should still have the string struct, just with empty fields.

Delta456 commented 1 year ago

I am working on this then.

JalonSolov commented 1 year ago

It should not be allowed for the same reason on any type of struct... arrays, strings, etc.