zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 49d0971cd4f6aa3f897a0f36272c05c641e25f79 (tree)
parent 1fe1235e14cd599c1b3a6f079670b7cb7ea270d2
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun, 10 Jan 2016 00:03:31 -0700

detect and report top level decl dependency loop

Diffstat:
Msrc/analyze.cpp | 4+++-
Mstd/rand.zig | 6++----
Mstd/std.zig | 6++----
Mtest/run_tests.cpp | 19+++++++++++++++++++
4 files changed, 26 insertions(+), 9 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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) {