Merge pull request #15879 from r00ster91/more_stage1_test_coverage

More stage1 test coverage
This commit is contained in:
Andrew Kelley 2023-07-03 15:59:54 -07:00 committed by GitHub
commit 17890f6b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 198 additions and 13 deletions

View File

@ -280,28 +280,28 @@ pub fn Reader(
/// Reads a native-endian integer
pub fn readIntNative(self: Self, comptime T: type) (Error || error{EndOfStream})!T {
const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
return mem.readIntNative(T, &bytes);
}
/// Reads a foreign-endian integer
pub fn readIntForeign(self: Self, comptime T: type) (Error || error{EndOfStream})!T {
const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
return mem.readIntForeign(T, &bytes);
}
pub fn readIntLittle(self: Self, comptime T: type) !T {
const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
return mem.readIntLittle(T, &bytes);
}
pub fn readIntBig(self: Self, comptime T: type) !T {
const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
return mem.readIntBig(T, &bytes);
}
pub fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) !T {
const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
return mem.readInt(T, &bytes, endian);
}

View File

@ -47,32 +47,32 @@ pub fn Writer(
/// Write a native-endian integer.
pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void {
var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
return self.writeAll(&bytes);
}
/// Write a foreign-endian integer.
pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void {
var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
return self.writeAll(&bytes);
}
pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void {
var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
return self.writeAll(&bytes);
}
pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void {
var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
return self.writeAll(&bytes);
}
pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void {
var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);
return self.writeAll(&bytes);
}

View File

@ -1616,7 +1616,7 @@ test "readIntBig and readIntLittle" {
/// accepts any integer bit width.
/// This function stores in native endian, which means it is implemented as a simple
/// memory store.
pub fn writeIntNative(comptime T: type, buf: *[(@typeInfo(T).Int.bits + 7) / 8]u8, value: T) void {
pub fn writeIntNative(comptime T: type, buf: *[@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8, value: T) void {
@as(*align(1) T, @ptrCast(buf)).* = value;
}

View File

@ -616,7 +616,7 @@ pub const Value = struct {
.Int, .Enum => {
const int_info = ty.intInfo(mod);
const bits = int_info.bits;
const byte_count = (bits + 7) / 8;
const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
var bigint_buffer: BigIntSpace = undefined;
const bigint = val.toBigInt(&bigint_buffer, mod);
@ -859,7 +859,7 @@ pub const Value = struct {
};
const int_info = int_ty.intInfo(mod);
const bits = int_info.bits;
const byte_count = (bits + 7) / 8;
const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty);
if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64

View File

@ -37,3 +37,12 @@ test "comparison of @alignOf(T) against zero" {
try expect(@alignOf(T) >= 0);
}
}
test "correct alignment for elements and slices of aligned array" {
var buf: [1024]u8 align(64) = undefined;
var start: usize = 1;
var end: usize = undefined;
try expect(@alignOf(@TypeOf(buf[start..end])) == @alignOf(*u8));
try expect(@alignOf(@TypeOf(&buf[start..end])) == @alignOf(*u8));
try expect(@alignOf(@TypeOf(&buf[start])) == @alignOf(*u8));
}

View File

@ -151,3 +151,15 @@ test "result location with inferred type ends up being pointer to comptime_int"
} else @as(u32, 0);
try expect(c == 1);
}
test "if-@as-if chain" {
var fast = true;
var very_fast = false;
const num_frames = if (fast)
@as(u32, if (very_fast) 16 else 4)
else
1;
try expect(num_frames == 4);
}

View File

@ -1370,3 +1370,21 @@ test "vector pointer is indexable" {
try expectEqual(@as(u32, 100), (&y)[0]);
try expectEqual(@as(u32, 200), (&y)[1]);
}
test "boolean vector with 2 or more booleans" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
// TODO: try removing this after <https://github.com/ziglang/zig/issues/13782>:
if (!(builtin.os.tag == .linux and builtin.cpu.arch == .x86_64)) return;
const vec1 = @Vector(2, bool){ true, true };
_ = vec1;
const vec2 = @Vector(3, bool){ true, true, true };
_ = vec2;
}

View File

@ -0,0 +1,10 @@
var internal_variable: u32 = 42;
export const p_internal_variable = &internal_variable;
extern var external_variable: u32;
export const p_external_variable = &external_variable;
// compile
// output_mode=Obj
// backend=stage2,llvm
// target=x86_64-linux

View File

@ -0,0 +1,21 @@
const std = @import("std");
noinline fn outer() u32 {
var a: u32 = 42;
return inner(.{
.unused = a,
.value = [1]u32{0},
});
}
noinline fn inner(args: anytype) u32 {
return args.value[0];
}
pub fn main() !void {
try std.testing.expect(outer() == 0);
}
// run
// backend=llvm
// target=native

View File

@ -0,0 +1,13 @@
export fn entry() void {
const a = @Vector(4, u32){ 1, 1, 1, 1 };
_ = @as(u32, @intCast(a));
}
// TODO: change target in the manifest to "native" probably after this is fixed:
// https://github.com/ziglang/zig/issues/13782
// error
// backend=stage2
// target=x86_64-linux
//
// :3:27: error: expected type 'u32', found '@Vector(4, u32)'

View File

@ -0,0 +1,10 @@
export fn entry1() void {
const slice = @as([*]i32, undefined)[0];
_ = slice;
}
// error
// backend=stage2
// target=native
//
// :2:41: error: use of undefined value here causes undefined behavior

View File

@ -0,0 +1,10 @@
export fn entry() void {
var items = [_]u8{ 1, 2, 3 };
for (&items) |*i| {
i.?.* = 1;
}
}
// error
//
// :4:10: error: expected optional type, found '*u8'

View File

@ -0,0 +1,23 @@
extern var X: *volatile i32;
inline fn fiveXwithType(comptime T: type) void {
_ = T;
X.* = 5;
}
inline fn fiveXwithArg(v: i32) void {
_ = v;
X.* = 5;
}
export fn entry1() void {
@call(.never_inline, fiveXwithType, .{i32});
}
export fn entry2() void {
@call(.never_inline, fiveXwithArg, .{1});
}
// error
//
// :14:5: error: 'never_inline' call of inline function
// :17:5: error: 'never_inline' call of inline function

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
fn foo() [:0]const u8 {
return undefined;
}
pub fn main() void {
var guid: [*:0]const u8 = undefined;
guid = foo();
}
// run
// backend=llvm
// target=native

View File

@ -0,0 +1,26 @@
const std = @import("std");
const A = union(enum) { hello: usize, merp: void };
var global_a: A = .{ .hello = 12 };
var global_usize: usize = 0;
fn doSomethingWithUsize(ptr: *usize) usize {
ptr.* = ptr.* + 1;
return ptr.*;
}
pub fn main() !void {
try std.testing.expect(doSomethingWithUsize(&global_usize) == 1);
switch (global_a) {
.merp => return,
.hello => |*value| {
try std.testing.expect(doSomethingWithUsize(value) == 13);
},
}
}
// run
// backend=llvm
// target=native