Commit 49d0971cd4

Andrew Kelley <superjoe30@gmail.com>
2016-01-10 08:03:31
detect and report top level decl dependency loop
1 parent 1fe1235
Changed files (4)
src/analyze.cpp
@@ -3033,7 +3033,9 @@ static void recursive_resolve_decl(CodeGen *g, ImportTableEntry *import, AstNode
         AstNode *child_node = unresolved_entry->value;
 
         if (child_node->codegen_node->decl_node.in_current_deps) {
-            zig_panic("TODO infinite top level decl loop");
+            // dependency loop. we'll let the fact that it's not in the respective
+            // table cause an error in resolve_top_level_decl.
+            continue;
         }
 
         // set temporary flag
std/rand.zig
@@ -3,10 +3,8 @@ const ARRAY_SIZE : u16 = 624;
 
 /// Use `rand_init` to initialize this state.
 pub struct Rand {
-    // TODO use ARRAY_SIZE here
-    array: [624]u32,
-    // TODO use #typeof(ARRAY_SIZE) here
-    index: u16,
+    array: [ARRAY_SIZE]u32,
+    index: #typeof(ARRAY_SIZE),
 
     /// Initialize random state with the given seed.
     pub fn init(r: &Rand, seed: u32) {
std/std.zig
@@ -24,8 +24,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize {
 // TODO handle buffering and flushing (mutex protected)
 // TODO error handling
 pub fn print_u64(x: u64) -> isize {
-    // TODO use max_u64_base10_digits instead of hardcoding 20
-    var buf: [20]u8;
+    var buf: [max_u64_base10_digits]u8;
     const len = buf_print_u64(buf, x);
     return write(stdout_fileno, buf.ptr, len);
 }
@@ -33,8 +32,7 @@ pub fn print_u64(x: u64) -> isize {
 // TODO handle buffering and flushing (mutex protected)
 // TODO error handling
 pub fn print_i64(x: i64) -> isize {
-    // TODO use max_u64_base10_digits instead of hardcoding 20
-    var buf: [20]u8;
+    var buf: [max_u64_base10_digits]u8;
     const len = buf_print_i64(buf, x);
     return write(stdout_fileno, buf.ptr, len);
 }
test/run_tests.cpp
@@ -991,6 +991,20 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
     return 0;
 }
     )SOURCE", "OK\n");
+
+    add_simple_case("order-independent declarations", R"SOURCE(
+use "std.zig";
+const x : #typeof(y) = 1234;
+const y : u16 = 5678;
+pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
+    print_ok(x)
+}
+fn print_ok(val: #typeof(x)) -> #typeof(foo) {
+    print_str("OK\n");
+    return 0;
+}
+const foo : i32 = 0;
+    )SOURCE", "OK\n");
 }
 
 
@@ -1299,6 +1313,11 @@ fn f() -> i32 {
 fn f() -> #bogus(foo) {
 }
     )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid compiler function: 'bogus'");
+
+    add_compile_fail_case("top level decl dependency loop", R"SOURCE(
+const a : #typeof(b) = 0;
+const b : #typeof(a) = 0;
+    )SOURCE", 1, ".tmp_source.zig:3:19: error: use of undeclared identifier 'a'");
 }
 
 static void print_compiler_invocation(TestCase *test_case) {