checker: constant cycle & test fix

pull/4219/head^2
joe-conigliaro 2020-04-04 17:05:26 +11:00
parent c9e290b36f
commit 97d8633557
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 14 additions and 19 deletions

View File

@ -26,6 +26,7 @@ mut:
error_lines []int // to avoid printing multiple errors for the same line
expected_type table.Type
fn_return_type table.Type // current function's return type
const_decl string
const_deps []string
// fn_decl ast.FnDecl
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 done_fields := []int
for i, field in it.fields {
mut set_const := false
if c.const_deps.len == 0 {
set_const = true
}
c.const_decl = field.name
c.const_deps << field.name
typ := c.expr(field.expr)
it.fields[i].typ = typ
if set_const {
for cd in c.const_deps {
for j, f in it.fields {
if j != i && cd in field_names && cd == f.name && !(j in done_fields) {
needs_order = true
x := field_order[j]
field_order[j] = field_order[i]
field_order[i] = x
break
}
for cd in c.const_deps {
for j, f in it.fields {
if j != i && cd in field_names && cd == f.name && !(j in done_fields) {
needs_order = true
x := field_order[j]
field_order[j] = field_order[i]
field_order[i] = x
break
}
}
done_fields << i
c.const_deps = []
}
done_fields << i
c.const_deps = []
}
if needs_order {
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']) {
name = '${ident.mod}.$ident.name'
}
if name in c.const_deps {
c.error('cycle in constants', ident.pos)
if name == c.const_decl {
c.error('cycle in constant `$c.const_decl`', ident.pos)
return table.void_type
}
c.const_deps << name

View File

@ -1,5 +1,4 @@
pub const (
c = a
a = b
c = a + b
b = 1