Files
zig/test/behavior/bugs/3779.zig
Daniele Cocca 9be2f76741 typeName: amend return type to string literal
This was already the case, but the documentation failed to point out
that the returned value is of type `*const [N:0]u8`, i.e. that of a
string literal.

Also adds a behavioral test to assert that this is the case.
2021-06-16 22:03:02 +01:00

32 lines
1.2 KiB
Zig

const std = @import("std");
const TestEnum = enum { TestEnumValue };
const tag_name = @tagName(TestEnum.TestEnumValue);
const ptr_tag_name: [*:0]const u8 = tag_name;
test "@tagName() returns a string literal" {
try std.testing.expectEqual([:0]const u8, @TypeOf(tag_name));
try std.testing.expectEqualStrings("TestEnumValue", tag_name);
try std.testing.expectEqualStrings("TestEnumValue", ptr_tag_name[0..tag_name.len]);
}
const TestError = error{TestErrorCode};
const error_name = @errorName(TestError.TestErrorCode);
const ptr_error_name: [*:0]const u8 = error_name;
test "@errorName() returns a string literal" {
try std.testing.expectEqual([:0]const u8, @TypeOf(error_name));
try std.testing.expectEqualStrings("TestErrorCode", error_name);
try std.testing.expectEqualStrings("TestErrorCode", ptr_error_name[0..error_name.len]);
}
const TestType = struct {};
const type_name = @typeName(TestType);
const ptr_type_name: [*:0]const u8 = type_name;
test "@typeName() returns a string literal" {
try std.testing.expectEqual(*const [type_name.len:0]u8, @TypeOf(type_name));
try std.testing.expectEqualStrings("TestType", type_name);
try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]);
}