mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
tests for local variables
This commit is contained in:
parent
5af4ef88ac
commit
0c2cc9d2cf
2 changed files with 46 additions and 2 deletions
|
|
@ -9,6 +9,8 @@ export fn _start() -> unreachable {
|
||||||
let b = 2;
|
let b = 2;
|
||||||
// let c : i32; // not yet support for const variables
|
// let c : i32; // not yet support for const variables
|
||||||
// let d; // parse error
|
// let d; // parse error
|
||||||
puts("Hello, world!");
|
if (a + b == 3) {
|
||||||
exit(a + b);
|
puts("OK");
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -256,6 +256,23 @@ static void add_compiling_test_cases(void) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
)SOURCE", "loop\nloop\nloop\n");
|
)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) {
|
static void add_compile_failure_test_cases(void) {
|
||||||
|
|
@ -340,6 +357,31 @@ done:
|
||||||
fn b() {}
|
fn b() {}
|
||||||
)SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");
|
)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) {
|
static void print_compiler_invokation(TestCase *test_case, Buf *zig_stderr) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue