aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJosh Wolfe <thejoshwolfe@gmail.com>2015-12-03 11:06:05 -0700
committerJosh Wolfe <thejoshwolfe@gmail.com>2015-12-03 11:06:05 -0700
commit0c2cc9d2cffd783a4db159cb84027e4016f618ff (patch)
tree1b1d2a5fd01cdc0f768a08017b6e9ca0d38a36e5 /test
parent5af4ef88acb68b66d38846b398e1e34845cccfbf (diff)
downloadzig-0c2cc9d2cffd783a4db159cb84027e4016f618ff.tar.gz
zig-0c2cc9d2cffd783a4db159cb84027e4016f618ff.zip
tests for local variables
Diffstat (limited to 'test')
-rw-r--r--test/run_tests.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/run_tests.cpp b/test/run_tests.cpp
index a8bc81bf11..bf5cf6e2f6 100644
--- a/test/run_tests.cpp
+++ b/test/run_tests.cpp
@@ -256,6 +256,23 @@ static void add_compiling_test_cases(void) {
exit(0);
}
)SOURCE", "loop\nloop\nloop\n");
+
+ add_simple_case("local variables", R"SOURCE(
+#link("c")
+extern {
+ fn puts(s: *const u8) -> i32;
+ fn exit(code: i32) -> unreachable;
+}
+
+export fn _start() -> unreachable {
+ let a : i32 = 1;
+ let b = 2;
+ if (a + b == 3) {
+ puts("OK");
+ }
+ exit(0);
+}
+ )SOURCE", "OK\n");
}
static void add_compile_failure_test_cases(void) {
@@ -340,6 +357,31 @@ done:
fn b() {}
)SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");
+ add_compile_fail_case("parameter redeclaration", R"SOURCE(
+fn f(a : i32, a : i32) {
+}
+ )SOURCE", 1, ".tmp_source.zig:2:1: error: redeclaration of parameter 'a'.");
+
+ add_compile_fail_case("local variable redeclaration", R"SOURCE(
+fn f() {
+ let a : i32 = 0;
+ let a = 0;
+}
+ )SOURCE", 1, ".tmp_source.zig:4:5: error: redeclaration of variable 'a'.");
+
+ add_compile_fail_case("local variable redeclares parameter", R"SOURCE(
+fn f(a : i32) {
+ let a = 0;
+}
+ )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'.");
+
+ add_compile_fail_case("variable has wrong type", R"SOURCE(
+fn f() -> i32 {
+ let a = "a";
+ a
+}
+ )SOURCE", 1, ".tmp_source.zig:2:15: error: type mismatch. expected i32. got *const u8");
+
}
static void print_compiler_invokation(TestCase *test_case, Buf *zig_stderr) {