1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix recursive define check is missing when defining sumtype. (fix #15684) (#15718)

This commit is contained in:
shove 2022-09-11 20:17:38 +08:00 committed by GitHub
parent be0dc0e537
commit 550b27b014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -519,6 +519,16 @@ pub fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
}
}
}
} else if sym.info is ast.FnType {
func := (sym.info as ast.FnType).func
if c.table.sym(func.return_type).name.ends_with('.$node.name') {
c.error('sum type `$node.name` cannot be defined recursively', variant.pos)
}
for param in func.params {
if c.table.sym(param.typ).name.ends_with('.$node.name') {
c.error('sum type `$node.name` cannot be defined recursively', variant.pos)
}
}
}
if sym.name.trim_string_left(sym.mod + '.') == node.name {

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/sumtype_define_recursively.vv:1:13: error: sum type `Expr` cannot be defined recursively
1 | type Expr = fn ([]Expr) Expr | int
| ~~~~~~~~~~~~~~~~
2 |
3 | fn f(exprs []Expr) Expr {

View File

@ -0,0 +1,10 @@
type Expr = fn ([]Expr) Expr | int
fn f(exprs []Expr) Expr {
return 0
}
fn main() {
mut m := map[string]Expr{}
m['f'] = f
}