Skip to content

More stage1 test coverage #15879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/std/io/reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/std/io/writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/behavior/alignof.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
12 changes: 12 additions & 0 deletions test/behavior/if.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions test/behavior/vector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions test/cases/address_of_extern_var_as_const.zig
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/cases/array_in_anon_struct.zig
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/cases/compile_errors/@intCast_on_vec.zig
Original file line number Diff line number Diff line change
@@ -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)'
10 changes: 10 additions & 0 deletions test/cases/compile_errors/as_many_ptr_undefined.zig
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/cases/compile_errors/expected_optional_type_in_for.zig
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions test/cases/maximum_sized_integer_literal.zig

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
26 changes: 26 additions & 0 deletions test/cases/taking_pointer_of_global_tagged_union.zig
Original file line number Diff line number Diff line change
@@ -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