Coercing a pointer into an option uses the address as if it were a value #21081
Labels
Bug
This tag is applied to issues which reports bugs.
Option Type
Bugs/feature requests, that are related to `?Type`.
Status: Confirmed
This bug has been confirmed to be valid by a contributor.
Unit: Checker
Bugs/feature requests, that are related to the type checker.
Unit: Compiler
Bugs/feature requests, that are related to the V compiler in general.
Describe the bug
If a function expects an optional struct, but you pass it a pointer instead, it wraps the pointer itself instead of giving a type error or dereferencing the pointer.
Reproduction Steps
struct MyStruct {
x int
}
fn unwrap_option(event_data ?MyStruct) int {
if val := event_data {
return val.x
} else {
return 0
}
}
fn wrap_unwrap_ptr(
event_data &MyStruct
) int {
return unwrap_option(event_data)
}
fn main() {
data := MyStruct { x: 0 }
result := wrap_unwrap_ptr(&data)
assert data.x == result
}
Expected Behavior
Either a type error or for the assertion to pass.
Current Behavior
The code compiles but then the assertion fails. E.g.
This is the C code it generates:
Notice event_data is a pointer, but it's used as the content of the option.
Possible Solution
Detecting this case and dereference the pointer, or giving a type error if this isn't meant to be allowed at all.
Additional Information/Context
No response
V version
V full version: V 0.4.5 386bd77.29e5124
Environment details (OS name and version, etc.)
V full version: V 0.4.5 386bd77.29e5124
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-03-23 10:18:34
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.11-67-g29e5124c
.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: