From d505ea6cafe8bf6549eb2c56396b0a94d8109859 Mon Sep 17 00:00:00 2001 From: Vexu Date: Wed, 26 Feb 2020 01:03:58 +0200 Subject: fix `@tagName` on extern and non-exhaustive enums --- test/compile_errors.zig | 9 +++++++++ test/stage1/behavior/enum.zig | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 138f7a7633..0260160dca 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3,6 +3,15 @@ const builtin = @import("builtin"); const Target = @import("std").Target; pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.addTest("@tagName on invalid value of non-exhaustive enum", + \\test "enum" { + \\ const E = enum(u8) {A, B, _}; + \\ _ = @tagName(@intToEnum(E, 5)); + \\} + , &[_][]const u8{ + "tmp.zig:3:18: error: no tag by value 5", + }); + cases.addTest("@ptrToInt with pointer to zero-sized type", \\export fn entry() void { \\ var pointer: ?*u0 = null; diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 18489ba24b..b6cb86a363 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -198,7 +198,17 @@ test "@tagName" { comptime expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three")); } -fn testEnumTagNameBare(n: BareNumber) []const u8 { +test "@tagName extern enum with duplicates" { + expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A")); + comptime expect(mem.eql(u8, testEnumTagNameBare(ExternDuplicates.B), "A")); +} + +test "@tagName non-exhaustive enum" { + expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B")); + comptime expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B")); +} + +fn testEnumTagNameBare(n: var) []const u8 { return @tagName(n); } @@ -208,6 +218,17 @@ const BareNumber = enum { Three, }; +const ExternDuplicates = extern enum(u8) { + A = 1, + B = 1, +}; + +const NonExhaustive = enum(u8) { + A, + B, + _, +}; + test "enum alignment" { comptime { expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8)); -- cgit v1.2.3 From 22432b15e31d506d070171b2932d5df71e9a1b9e Mon Sep 17 00:00:00 2001 From: Vexu Date: Wed, 26 Feb 2020 11:07:47 +0200 Subject: add test for `@intToEnum` --- lib/std/fmt.zig | 6 ++---- src-self-hosted/ir.zig | 2 +- test/stage1/behavior/cast.zig | 11 +++++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index b9c6dd0033..d093101646 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -414,10 +414,9 @@ pub fn formatType( if (max_depth == 0) { return output(context, "{ ... }"); } - comptime var field_i = 0; try output(context, "{"); - inline for (StructT.fields) |f| { - if (field_i == 0) { + inline for (StructT.fields) |f, i| { + if (i == 0) { try output(context, " ."); } else { try output(context, ", ."); @@ -425,7 +424,6 @@ pub fn formatType( try output(context, f.name); try output(context, " = "); try formatType(@field(value, f.name), fmt, options, context, Errors, output, max_depth - 1); - field_i += 1; } try output(context, " }"); }, diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index 576294a7d7..2e65962d41 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -1803,7 +1803,7 @@ pub const Builder = struct { // Look at the params and ref() other instructions inline for (@typeInfo(I.Params).Struct.fields) |f| { - switch (f.fiedl_type) { + switch (f.field_type) { *Inst => @field(inst.params, f.name).ref(self), *BasicBlock => @field(inst.params, f.name).ref(self), ?*Inst => if (@field(inst.params, f.name)) |other| other.ref(self), diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index 76d3b784ec..d2267a3753 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -491,6 +491,17 @@ test "@intToEnum passed a comptime_int to an enum with one item" { expect(x == E.A); } +test "@intToEnum runtime to an extern enum with duplicate values" { + const E = extern enum(u8) { + A = 1, + B = 1, + }; + var a: u8 = 1; + var x = @intToEnum(E, a); + expect(x == E.A); + expect(x == E.B); +} + test "@intCast to u0 and use the result" { const S = struct { fn doTheTest(zero: u1, one: u1, bigzero: i32) void { -- cgit v1.2.3