zig/test/behavior/if.zig
Andrew Kelley 6f303c01f3 LLVM: add extra padding to structs and tuples sometimes
* Sema: resolve type fully when emitting an alloc AIR instruction to
   avoid tripping assertion for checking struct field alignment.
 * LLVM backend: keep a reference to the LLVM target data alive during
   lowering so that we can ask LLVM what it thinks the ABI alignment
   and size of LLVM types are. We need this in order to lower tuples and
   structs so that we can put in extra padding bytes when Zig disagrees
   with LLVM about the size or alignment of something.
 * LLVM backend: make the LLVM struct type packed that contains the most
   aligned union field and the padding. This prevents the struct from
   being too big according to LLVM. In the future, we may want to
   consider instead emitting unions in a "flat" manner; putting the tag,
   most aligned union field, and padding all in the same struct field
   space.
 * LLVM backend: make structs with 2 or fewer fields return isByRef=false.
   This results in more efficient codegen. This required lowering of
   bitcast to sometimes store the struct into an alloca, ptrcast, and
   then load because LLVM does not allow bitcasting structs.
 * enable more passing behavior tests.
2022-03-01 18:24:00 -07:00

123 lines
3.0 KiB
Zig

const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "if statements" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
shouldBeEqual(1, 1);
firstEqlThird(2, 1, 2);
}
fn shouldBeEqual(a: i32, b: i32) void {
if (a != b) {
unreachable;
} else {
return;
}
}
fn firstEqlThird(a: i32, b: i32, c: i32) void {
if (a == b) {
unreachable;
} else if (b == c) {
unreachable;
} else if (a == c) {
return;
} else {
unreachable;
}
}
test "else if expression" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
try expect(elseIfExpressionF(1) == 1);
}
fn elseIfExpressionF(c: u8) u8 {
if (c == 0) {
return 0;
} else if (c == 1) {
return 1;
} else {
return @as(u8, 2);
}
}
// #2297
var global_with_val: anyerror!u32 = 0;
var global_with_err: anyerror!u32 = error.SomeError;
test "unwrap mutable global var" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (global_with_val) |v| {
try expect(v == 0);
} else |_| {
unreachable;
}
if (global_with_err) |_| {
unreachable;
} else |e| {
try expect(e == error.SomeError);
}
}
test "labeled break inside comptime if inside runtime if" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
var answer: i32 = 0;
var c = true;
if (c) {
answer = if (true) blk: {
break :blk @as(i32, 42);
};
}
try expect(answer == 42);
}
test "const result loc, runtime if cond, else unreachable" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
const Num = enum { One, Two };
var t = true;
const x = if (t) Num.Two else unreachable;
try expect(x == .Two);
}
test "if copies its payload" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
const S = struct {
fn doTheTest() !void {
var tmp: ?i32 = 10;
if (tmp) |value| {
// Modify the original variable
tmp = null;
try expect(value == 10);
} else unreachable;
}
};
try S.doTheTest();
comptime try S.doTheTest();
}
test "if prongs cast to expected type instead of peer type resolution" {
const S = struct {
fn doTheTest(f: bool) !void {
var x: i32 = 0;
x = if (f) 1 else 2;
try expect(x == 2);
var b = true;
const y: i32 = if (b) 1 else 2;
try expect(y == 1);
}
};
try S.doTheTest(false);
comptime try S.doTheTest(false);
}