zig/test/behavior/reflection.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

67 lines
1.8 KiB
Zig

const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
const mem = std.mem;
const reflection = @This();
test "reflection: function return type, var args, and param types" {
comptime {
const info = @typeInfo(@TypeOf(dummy)).Fn;
try expect(info.return_type.? == i32);
try expect(!info.is_var_args);
try expect(info.args.len == 3);
try expect(info.args[0].arg_type.? == bool);
try expect(info.args[1].arg_type.? == i32);
try expect(info.args[2].arg_type.? == f32);
}
}
fn dummy(a: bool, b: i32, c: f32) i32 {
if (false) {
a;
b;
c;
}
return 1234;
}
test "reflection: @field" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
var f = Foo{
.one = 42,
.two = true,
.three = void{},
};
try expect(f.one == f.one);
try expect(@field(f, "o" ++ "ne") == f.one);
try expect(@field(f, "t" ++ "wo") == f.two);
try expect(@field(f, "th" ++ "ree") == f.three);
try expect(@field(Foo, "const" ++ "ant") == Foo.constant);
try expect(@field(Bar, "O" ++ "ne") == Bar.One);
try expect(@field(Bar, "T" ++ "wo") == Bar.Two);
try expect(@field(Bar, "Th" ++ "ree") == Bar.Three);
try expect(@field(Bar, "F" ++ "our") == Bar.Four);
try expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2));
@field(f, "o" ++ "ne") = 4;
try expect(f.one == 4);
}
const Foo = struct {
const constant = 52;
one: i32,
two: bool,
three: void,
};
const Bar = union(enum) {
One: void,
Two: i32,
Three: bool,
Four: f64,
};