Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coercing a pointer into an option uses the address as if it were a value #21081

Closed
spiveeworks opened this issue Mar 23, 2024 · 0 comments · Fixed by #21087
Closed

Coercing a pointer into an option uses the address as if it were a value #21081

spiveeworks opened this issue Mar 23, 2024 · 0 comments · Fixed by #21087
Assignees
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.

Comments

@spiveeworks
Copy link
Contributor

spiveeworks commented Mar 23, 2024

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.

option_ptr_bug.v:23: FAIL: fn main.main: assert data.x == result
left value: data.x = 0
right value: result = 645921536

This is the C code it generates:

VV_LOCAL_SYMBOL int main__wrap_unwrap_ptr(main__MyStruct* event_data) {
	_option_main__MyStruct _t2;
	_option_ok(&(main__MyStruct[]) { event_data }, (_option*)(&_t2), sizeof(main__MyStruct));
	int _t1 = main__unwrap_option( _t2);
	return _t1;
}

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.

@spiveeworks spiveeworks added the Bug This tag is applied to issues which reports bugs. label Mar 23, 2024
@spytheman spytheman added 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. labels Mar 23, 2024
@felipensp felipensp assigned felipensp and unassigned felipensp Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants