v icon indicating copy to clipboard operation
v copied to clipboard

Option type comparisons are permitted by V compiler.

Open edam opened this issue 2 years ago • 3 comments

Describe the bug

fn main() {
    a := ?string("hi")
    if a == "hi" { // C ERROR
        println("match")
    }
}

Expected Behavior

A V error, not a C error.

Current Behavior

Invalid C is generated:

VV_LOCAL_SYMBOL void main__main(void) {
	_option_string _t1;
	_option_ok(&(string[]) { _SLIT("hi") }, (_option*)(&_t1), sizeof(string));
	_option_string a = _t1;
	if (_option_string__eq(/*opt*/(*(string*)a.data), _SLIT("hi"))) {
		println(_SLIT("match"));
	}
}

C error:

error: implicit declaration of function '_option_string__eq' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        if (_option_string__eq(/*opt*/(*(string*)a.data), _SLIT("hi"))) {
            ^
1 warning and 1 error generated.

Reproduction Steps

See description

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 ee4150f

Environment details (OS name and version, etc.)

MacOS

edam avatar Mar 10 '23 10:03 edam

I am not sure if comparison operators should work on options.

Delta456 avatar Mar 10 '23 11:03 Delta456

I thought the plan was to eventually allow this:

if a {
    assert a == "hi" // smartcast: a is a string
}

So maybe, the correct code (when the above works) should be this?

if a {
    if a == "hi" {
        println("match")
    }
}

For the records, currently you can do this:

if a != none { }

edam avatar Mar 10 '23 11:03 edam

After discussion on discord, updated description:

  • This should not be allowed by the V compiler.
  • a needs upwrapping (to a string) before comparison.
  • Should be a V error

edam avatar Mar 10 '23 14:03 edam