From 9e88356282861e5812d95587cb8fab7f902bc85b Mon Sep 17 00:00:00 2001 From: Daniele Cocca Date: Wed, 28 Apr 2021 01:26:03 +0100 Subject: [PATCH] embedFile: change notation from [X:0] to [N:0] This is for consistency with the documentation on sentinel-terminated {arrays,slices,pointers} which already use `N` for a comptime-inferred size rather than `X`. Also adds a behavioral test to assert that a string literal is returned. --- doc/langref.html.in | 2 +- test/behavior/bugs/3779.zig | 11 +++++++++++ test/behavior/bugs/3779_file_to_embed.txt | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/behavior/bugs/3779_file_to_embed.txt diff --git a/doc/langref.html.in b/doc/langref.html.in index 4da857bbc..41cd77e2c 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -7447,7 +7447,7 @@ test "main" { {#see_also|@divFloor|@divExact#} {#header_close#} {#header_open|@embedFile#} -
{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}
+
{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}

This function returns a compile time constant pointer to null-terminated, fixed-size array with length equal to the byte count of the file given by diff --git a/test/behavior/bugs/3779.zig b/test/behavior/bugs/3779.zig index 2c92236e2..1bf5bfcc9 100644 --- a/test/behavior/bugs/3779.zig +++ b/test/behavior/bugs/3779.zig @@ -29,3 +29,14 @@ test "@typeName() returns a string literal" { try std.testing.expectEqualStrings("TestType", type_name); try std.testing.expectEqualStrings("TestType", ptr_type_name[0..type_name.len]); } + +const actual_contents = @embedFile("3779_file_to_embed.txt"); +const ptr_actual_contents: [*:0]const u8 = actual_contents; +const expected_contents = "hello zig\n"; + +test "@embedFile() returns a string literal" { + try std.testing.expectEqual(*const [expected_contents.len:0]u8, @TypeOf(actual_contents)); + try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents)); + try std.testing.expectEqualStrings(expected_contents, actual_contents); + try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]); +} diff --git a/test/behavior/bugs/3779_file_to_embed.txt b/test/behavior/bugs/3779_file_to_embed.txt new file mode 100644 index 000000000..0436280ed --- /dev/null +++ b/test/behavior/bugs/3779_file_to_embed.txt @@ -0,0 +1 @@ +hello zig