checker: ensure that the variant SubType exists (#14053)

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Vincenzo Palazzo 2022-04-16 18:29:10 +02:00 committed by Jef Roosens
parent 9528a89b17
commit 9f9d24d616
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 55 additions and 0 deletions

View File

@ -4197,6 +4197,12 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) ? {
c.ensure_type_exists(info.key_type, pos) ?
c.ensure_type_exists(info.value_type, pos) ?
}
.sum_type {
info := sym.info as ast.SumType
for concrete_typ in info.concrete_types {
c.ensure_type_exists(concrete_typ, pos) ?
}
}
else {}
}
}

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/undefined_type_on_sumtype.vv:1:17: error: unknown type `Token`.
Did you mean `Ok<[]Token>`?
1 | type ParseRes = Result<[]Token, ParseErr>
| ~~~~~~~~~~~~~~~~~~~~~~~~~
2 |
3 | // Token type is unknown
vlib/v/checker/tests/undefined_type_on_sumtype.vv:30:4: error: unused variable: `rx`
28 | match r {
29 | Some<ParseRes> {
30 | rx := r.value
| ~~
31 | }
32 | None<ParseRes> {}

View File

@ -0,0 +1,34 @@
type ParseRes = Result<[]Token, ParseErr>
// Token type is unknown
//struct Token {}
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 := r.value
}
None<ParseRes> {}
}
}

View File

@ -1,5 +1,7 @@
type ParseRes = Result<[]Token, ParseErr>
struct Token {}
struct ParseErr {}
type Opt<T> = None<T> | Some<T>