vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

Error casting aliases to generic types #13863

Open dcow0 opened 2 years ago

dcow0 commented 2 years ago

V version: V 0.2.4 875ad1f.42a6783 OS: linux, "Fedora release 35 (Thirty Five)"

What did you do? v report_test.v where report_test.v has the following content:

module report

type ParseRes = Result<[]Token, ParseErr>

struct ParseErr{

}

type Opt<T> = None<T> | Some<T>

struct None<T> {}

struct Some<T> {
    value T
}

type Result<T, U> = Err<U> | Ok<T>

struct Ok<T> {
    value T
}

struct Err<U> {
    value U
}

fn test_report() {
    r := Opt<ParseRes>(None<ParseRes>{})
    match r {
        Some<ParseRes> {
            rx := Result<[]Token, ParseErr>(r)
        }
        None<ParseRes> {}
    }
}

What did you expect to see? The code should compile

What did you see instead? The compiler fails with

report_test.v:31:10: error: cannot cast `Some<report.ParseRes>` to `Token, report.ParseErr>`
   29 |     match r {
   30 |         Some<ParseRes> {
   31 |             rx := Result<[]Token, ParseErr>(r)
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |         }
   33 |         None<ParseRes> {}
Corallus-Caninus commented 2 years ago

I am also experiencing this issue in my code and was able to reproduce.

JalonSolov commented 2 years ago

The error is different in latest V

report_test.v:3:17: error: unknown type `report.Token`.
Did you mean `Ok<[]report.Token>`?
    1 | module report
    2 | 
    3 | type ParseRes = Result<[]Token, ParseErr>
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~
    4 | 
    5 | struct ParseErr{
report_test.v:31:10: error: unknown type `report.Token`.
Did you mean `Ok<[]report.Token>`?
   29 |     match r {
   30 |         Some<ParseRes> {
   31 |             rx := Result<[]Token, ParseErr>(r)
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |         }
   33 |         None<ParseRes> {}