diff --git a/src/analyze.cpp b/src/analyze.cpp index 2d7c5e4331..0d73c4c3ee 100644 --- a/src/analyze.cpp +++ b/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 diff --git a/std/rand.zig b/std/rand.zig index 46b3674929..637f34443d 100644 --- a/std/rand.zig +++ b/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) { diff --git a/std/std.zig b/std/std.zig index d42074a340..e1b66d9cb1 100644 --- a/std/std.zig +++ b/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); } diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 6711346a66..153887e4c3 100644 --- a/test/run_tests.cpp +++ b/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) {