Merge pull request #14682 from ziglang/stage1-coverage

add test coverage for fixed stage1 bugs
This commit is contained in:
Andrew Kelley
2023-02-20 14:23:37 -05:00
committed by GitHub
5 changed files with 95 additions and 0 deletions

View File

@@ -1143,3 +1143,17 @@ test "orelse coercion as function argument" {
var foo = Container.init(optional orelse .{});
try expect(foo.a.?.start == -1);
}
test "runtime-known globals initialized with undefined" {
const S = struct {
var array: [10]u32 = [_]u32{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var vp: [*]u32 = undefined;
var s: []u32 = undefined;
};
S.vp = &S.array;
S.s = S.vp[0..5];
try expect(S.s[0] == 1);
try expect(S.s[4] == 5);
}

View File

@@ -1568,3 +1568,12 @@ test "@volatileCast without a result location" {
try expect(@TypeOf(z) == *i32);
try expect(z.* == 1234);
}
test "coercion from single-item pointer to @as to slice" {
var x: u32 = 1;
// Why the following line gets a compile error?
const t: []u32 = @as(*[1]u32, &x);
try expect(t[0] == 1);
}

View File

@@ -1578,3 +1578,38 @@ test "directly initiating tuple like struct" {
const a = struct { u8 }{8};
try expect(a[0] == 8);
}
test "instantiate struct with comptime field" {
{
var things = struct {
comptime foo: i8 = 1,
}{};
comptime std.debug.assert(things.foo == 1);
}
{
const T = struct {
comptime foo: i8 = 1,
};
var things = T{};
comptime std.debug.assert(things.foo == 1);
}
{
var things: struct {
comptime foo: i8 = 1,
} = .{};
comptime std.debug.assert(things.foo == 1);
}
{
var things: struct {
comptime foo: i8 = 1,
} = undefined; // Segmentation fault at address 0x0
comptime std.debug.assert(things.foo == 1);
}
}

View File

@@ -603,3 +603,9 @@ test "@typeInfo decls ignore dependency loops" {
};
_ = S.foo;
}
test "type info of tuple of string literal default value" {
const struct_field = @typeInfo(@TypeOf(.{"hi"})).Struct.fields[0];
const value = @ptrCast(*align(1) const *const [2:0]u8, struct_field.default_value.?).*;
comptime std.debug.assert(value[0] == 'h');
}

View File

@@ -0,0 +1,31 @@
const std = @import("std");
const Error = error{InvalidCharacter};
const Direction = enum { upside_down };
const Barrrr = union(enum) {
float: f64,
direction: Direction,
};
fn fooey(bar: std.meta.Tag(Barrrr), args: []const []const u8) !Barrrr {
return switch (bar) {
.float => .{ .float = try std.fmt.parseFloat(f64, args[0]) },
.direction => if (std.mem.eql(u8, args[0], "upside_down"))
Barrrr{ .direction = .upside_down }
else
error.InvalidDirection,
};
}
pub fn main() Error!void {
std.debug.print("{}", .{try fooey(.direction, &[_][]const u8{ "one", "two", "three" })});
}
// error
// backend=llvm
// target=native
//
// :23:29: error: expected type 'error{InvalidCharacter}', found '@typeInfo(@typeInfo(@TypeOf(tmp.fooey)).Fn.return_type.?).ErrorUnion.error_set'
// :23:29: note: 'error.InvalidDirection' not a member of destination error set