checker: constant cycle & test fix
parent
c9e290b36f
commit
97d8633557
|
@ -26,6 +26,7 @@ mut:
|
||||||
error_lines []int // to avoid printing multiple errors for the same line
|
error_lines []int // to avoid printing multiple errors for the same line
|
||||||
expected_type table.Type
|
expected_type table.Type
|
||||||
fn_return_type table.Type // current function's return type
|
fn_return_type table.Type // current function's return type
|
||||||
|
const_decl string
|
||||||
const_deps []string
|
const_deps []string
|
||||||
// fn_decl ast.FnDecl
|
// fn_decl ast.FnDecl
|
||||||
pref &pref.Preferences // Preferences shared from V struct
|
pref &pref.Preferences // Preferences shared from V struct
|
||||||
|
@ -581,28 +582,23 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
mut needs_order := false
|
mut needs_order := false
|
||||||
mut done_fields := []int
|
mut done_fields := []int
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
mut set_const := false
|
c.const_decl = field.name
|
||||||
if c.const_deps.len == 0 {
|
|
||||||
set_const = true
|
|
||||||
}
|
|
||||||
c.const_deps << field.name
|
c.const_deps << field.name
|
||||||
typ := c.expr(field.expr)
|
typ := c.expr(field.expr)
|
||||||
it.fields[i].typ = typ
|
it.fields[i].typ = typ
|
||||||
if set_const {
|
for cd in c.const_deps {
|
||||||
for cd in c.const_deps {
|
for j, f in it.fields {
|
||||||
for j, f in it.fields {
|
if j != i && cd in field_names && cd == f.name && !(j in done_fields) {
|
||||||
if j != i && cd in field_names && cd == f.name && !(j in done_fields) {
|
needs_order = true
|
||||||
needs_order = true
|
x := field_order[j]
|
||||||
x := field_order[j]
|
field_order[j] = field_order[i]
|
||||||
field_order[j] = field_order[i]
|
field_order[i] = x
|
||||||
field_order[i] = x
|
break
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done_fields << i
|
|
||||||
c.const_deps = []
|
|
||||||
}
|
}
|
||||||
|
done_fields << i
|
||||||
|
c.const_deps = []
|
||||||
}
|
}
|
||||||
if needs_order {
|
if needs_order {
|
||||||
mut ordered_fields := []ast.ConstField
|
mut ordered_fields := []ast.ConstField
|
||||||
|
@ -844,8 +840,8 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
if !name.contains('.') && !(ident.mod in ['builtin', 'main']) {
|
if !name.contains('.') && !(ident.mod in ['builtin', 'main']) {
|
||||||
name = '${ident.mod}.$ident.name'
|
name = '${ident.mod}.$ident.name'
|
||||||
}
|
}
|
||||||
if name in c.const_deps {
|
if name == c.const_decl {
|
||||||
c.error('cycle in constants', ident.pos)
|
c.error('cycle in constant `$c.const_decl`', ident.pos)
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
c.const_deps << name
|
c.const_deps << name
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub const (
|
pub const (
|
||||||
c = a
|
|
||||||
a = b
|
a = b
|
||||||
c = a + b
|
c = a + b
|
||||||
b = 1
|
b = 1
|
||||||
|
|
Loading…
Reference in New Issue