cannot use none
as a ?&Type
#21213
Labels
Bug
This tag is applied to issues which reports bugs.
Option Type
Bugs/feature requests, that are related to `?Type`.
Unit: cgen
Bugs/feature requests, that are related to the default C generating backend.
Unit: Checker
Bugs/feature requests, that are related to the type checker.
Describe the bug
When a function takes an optional pointer,
?&T
, passingnone
to the function gives a type error.Reproduction Steps
Expected Behavior
The program should compile and run fine.
Current Behavior
It gives a type error.
Possible Solution
The way that type checking only requires
Type
rather than&Type
for function arguments might be tripping up the type inference, making it think that only?Type
is needed, and thus treatingnone
as a?Type
.In fact I think before #21081 was fixed, it used to actually accept
none
and complete type checking and cgen, but then the c compiler would complain about the resulting C code, its complaint was something along the lines of__t0
was an__option_MyTypeHere
when it should be an__option_MyTypeHere_ptr
.Additional Information/Context
Given that
nil
is considered unsafe, one would expect?&Type
to be a natural and valid way of passing optional pointers around. In Rust?&Type
takes up just as much memory as&Type
of course, but in V it should at least behave as expected, even if it takes more memory and requires manipulation of unaligned memory to work.Optional pointers are especially useful when making C bindings for functions that take multiple optional pointers.
Changing the V wrapper to take
?Type1
and?Type2
instead of?&Type1
and?&Type2
would add unnecessary stack allocation and copying, and potentially complicated default-initialization and cleanup, in order to simply call a C function that takes nullable pointers - nullable pointers are very common in C APIs!V version
V 0.4.5 ad17be5
Environment details (OS name and version, etc.)
V full version: V 0.4.5 386bd77.ad17be5
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 4 cpus, 64bit, little endian,
getwd: C:\Users\Owner\Code\v-experiments
vexe: C:\Program Files\v\v.exe
vexe mtime: 2024-04-07 05:22:56
vroot: contains spaces, value: C:\Program Files\v
VMODULES: OK, value: C:\Users\Owner.vmodules
VTMP: OK, value: C:\Users\Owner\AppData\Local\Temp\v_0
Git version: git version 2.37.2.windows.2
Git vroot status: weekly.2024.14-26-gad17be5d (5 commit(s) behind V master)
.git/config present: true
CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.
thirdparty/tcc: N/A
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.
The text was updated successfully, but these errors were encountered: