diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig index b82bfab99e..669cacfc40 100644 --- a/test/behavior/basic.zig +++ b/test/behavior/basic.zig @@ -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); +} diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 16f3c6e2dd..927caa965b 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -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); +} diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index ed3e1ce88f..8a01d68587 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -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); + } +} diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index 419a2f231c..6f64c92006 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -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'); +} diff --git a/test/cases/compile_errors/error_set_membership.zig b/test/cases/compile_errors/error_set_membership.zig new file mode 100644 index 0000000000..5683e9594b --- /dev/null +++ b/test/cases/compile_errors/error_set_membership.zig @@ -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