detect and report top level decl dependency loop

This commit is contained in:
Andrew Kelley 2016-01-10 00:03:31 -07:00
parent 1fe1235e14
commit 49d0971cd4
4 changed files with 26 additions and 9 deletions

View File

@ -3033,7 +3033,9 @@ static void recursive_resolve_decl(CodeGen *g, ImportTableEntry *import, AstNode
AstNode *child_node = unresolved_entry->value; AstNode *child_node = unresolved_entry->value;
if (child_node->codegen_node->decl_node.in_current_deps) { 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 // set temporary flag

View File

@ -3,10 +3,8 @@ const ARRAY_SIZE : u16 = 624;
/// Use `rand_init` to initialize this state. /// Use `rand_init` to initialize this state.
pub struct Rand { pub struct Rand {
// TODO use ARRAY_SIZE here array: [ARRAY_SIZE]u32,
array: [624]u32, index: #typeof(ARRAY_SIZE),
// TODO use #typeof(ARRAY_SIZE) here
index: u16,
/// Initialize random state with the given seed. /// Initialize random state with the given seed.
pub fn init(r: &Rand, seed: u32) { pub fn init(r: &Rand, seed: u32) {

View File

@ -24,8 +24,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize {
// TODO handle buffering and flushing (mutex protected) // TODO handle buffering and flushing (mutex protected)
// TODO error handling // TODO error handling
pub fn print_u64(x: u64) -> isize { pub fn print_u64(x: u64) -> isize {
// TODO use max_u64_base10_digits instead of hardcoding 20 var buf: [max_u64_base10_digits]u8;
var buf: [20]u8;
const len = buf_print_u64(buf, x); const len = buf_print_u64(buf, x);
return write(stdout_fileno, buf.ptr, len); 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 handle buffering and flushing (mutex protected)
// TODO error handling // TODO error handling
pub fn print_i64(x: i64) -> isize { pub fn print_i64(x: i64) -> isize {
// TODO use max_u64_base10_digits instead of hardcoding 20 var buf: [max_u64_base10_digits]u8;
var buf: [20]u8;
const len = buf_print_i64(buf, x); const len = buf_print_i64(buf, x);
return write(stdout_fileno, buf.ptr, len); return write(stdout_fileno, buf.ptr, len);
} }

View File

@ -991,6 +991,20 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
return 0; return 0;
} }
)SOURCE", "OK\n"); )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) { fn f() -> #bogus(foo) {
} }
)SOURCE", 1, ".tmp_source.zig:2:11: error: invalid compiler function: 'bogus'"); )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) { static void print_compiler_invocation(TestCase *test_case) {