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 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,14 +582,10 @@ 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) {
@ -603,7 +600,6 @@ fn (c mut Checker) stmt(node ast.Stmt) {
done_fields << i done_fields << i
c.const_deps = [] c.const_deps = []
} }
}
if needs_order { if needs_order {
mut ordered_fields := []ast.ConstField mut ordered_fields := []ast.ConstField
for order in field_order { for order in field_order {
@ -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

View File

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