v icon indicating copy to clipboard operation
v copied to clipboard

Compiler errors is wrong when smartcasting references to sumtypes

Open edam opened this issue 2 years ago • 1 comments

Describe the bug

When

  • You have a reference to a sumtype value
  • You smartcast as a reference to the sumtype variant

You get a compiler error that the variant is not a variant of the sumtype, which is clearly wrong.

See example code below.

Expected Behavior

Not sure. Should casting &SumType as &VariantType be permitted?

At a minimum, the compiler error looks like it is inaccurate.

Current Behavior

Compiler error, which incorrectly states that the variant is not a variant of the sumtype.

Reproduction Steps

type SumType = VariantType | int
struct VariantType {}

fn main() {
    mut r := unsafe{ &SumType(VariantType{}) }
    if r is &VariantType { // ERROR: `SumType` has no variant `VariantType`
        println("hi")
    }
}

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 858ce4e

Environment details (OS name and version, etc.)

MacOS

edam avatar Feb 06 '23 13:02 edam

Note, this works:

if *r is VariantType {

However, it does not smartcast. And the following is not allowed:

if mut *r is VariantType {

Which begs the question: how do you smartcast a reference to a sumtype?

edam avatar Feb 06 '23 13:02 edam