| author | |
| committer | |
| log | 9be2f767417ef4eb69f35b67348bb3e2ec752d4a |
| tree | ec3eb55e45146939458cf801ea07b78080ef81a6 |
| parent | 171102ea7ca3a11113d2ccfb7206b0eaddab19ac |
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.2 files changed, 11 insertions(+), 1 deletions(-)
doc/langref.html.in+1-1| ... | @@ -8623,7 +8623,7 @@ test "integer truncation" { | ... | @@ -8623,7 +8623,7 @@ test "integer truncation" { |
| 8623 | {#header_close#} | 8623 | {#header_close#} |
| 8624 | 8624 | ||
| 8625 | {#header_open|@typeName#} | 8625 | {#header_open|@typeName#} |
| 8626 | <pre>{#syntax#}@typeName(T: type) [N]u8{#endsyntax#}</pre> | 8626 | <pre>{#syntax#}@typeName(T: type) *const [N:0]u8{#endsyntax#}</pre> |
| 8627 | <p> | 8627 | <p> |
| 8628 | This function returns the string representation of a type, as | 8628 | This function returns the string representation of a type, as |
| 8629 | an array. It is equivalent to a string literal of the type name. | 8629 | an array. It is equivalent to a string literal of the type name. |
test/behavior/bugs/3779.zig+10| ... | @@ -19,3 +19,13 @@ test "@errorName() returns a string literal" { | ... | @@ -19,3 +19,13 @@ test "@errorName() returns a string literal" { |
| 19 | try std.testing.expectEqualStrings("TestErrorCode", error_name); | 19 | try std.testing.expectEqualStrings("TestErrorCode", error_name); |
| 20 | try std.testing.expectEqualStrings("TestErrorCode", ptr_error_name[0..error_name.len]); | 20 | try std.testing.expectEqualStrings("TestErrorCode", ptr_error_name[0..error_name.len]); |
| 21 | } | 21 | } |
| 22 | |||
| 23 | const TestType = struct {}; | ||
| 24 | const type_name = @typeName(TestType); | ||
| 25 | const ptr_type_name: [*:0]const u8 = type_name; | ||
| 26 | |||
| 27 | test "@typeName() returns a string literal" { | ||
| 28 | try std.testing.expectEqual(*const [type_name.len:0]u8, @TypeOf(type_name)); | ||
| 29 | try std.testing.expectEqualStrings("TestType", type_name); | ||
| 30 | try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]); | ||
| 31 | } |