authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 16:22:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 16:22:09-07:00
loga6bf68ccf985787eeac33a97e362d043987905c4
tree463cb67f3551c397aae17dff5594cd2fd2037ca0
parentd979dd9b58f6d5462b009002e0a0eb79f94fb9a7

langref: fix test cases now that AST Lowering has priority


2 files changed, 50 insertions(+), 18 deletions(-)

doc/docgen.zig+34-14
......@@ -8,6 +8,7 @@ const Progress = std.Progress;
88const print = std.debug.print;
99const mem = std.mem;
1010const testing = std.testing;
11const Allocator = std.mem.Allocator;
1112
1213const max_doc_file_size = 10 * 1024 * 1024;
1314
......@@ -326,7 +327,7 @@ const Action = enum {
326327 Close,
327328};
328329
329fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
330fn genToc(allocator: *Allocator, tokenizer: *Tokenizer) !Toc {
330331 var urls = std.StringHashMap(Token).init(allocator);
331332 errdefer urls.deinit();
332333
......@@ -630,7 +631,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
630631 };
631632}
632633
633fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
634fn urlize(allocator: *Allocator, input: []const u8) ![]u8 {
634635 var buf = std.ArrayList(u8).init(allocator);
635636 defer buf.deinit();
636637
......@@ -649,7 +650,7 @@ fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
649650 return buf.toOwnedSlice();
650651}
651652
652fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 {
653fn escapeHtml(allocator: *Allocator, input: []const u8) ![]u8 {
653654 var buf = std.ArrayList(u8).init(allocator);
654655 defer buf.deinit();
655656
......@@ -695,7 +696,7 @@ test "term color" {
695696 testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);
696697}
697698
698fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
699fn termColor(allocator: *Allocator, input: []const u8) ![]u8 {
699700 var buf = std.ArrayList(u8).init(allocator);
700701 defer buf.deinit();
701702
......@@ -789,8 +790,15 @@ fn isType(name: []const u8) bool {
789790 return false;
790791}
791792
792fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token, raw_src: []const u8) !void {
793 const src = mem.trim(u8, raw_src, " \n");
793fn tokenizeAndPrintRaw(
794 allocator: *Allocator,
795 docgen_tokenizer: *Tokenizer,
796 out: anytype,
797 source_token: Token,
798 raw_src: []const u8,
799) !void {
800 const src_non_terminated = mem.trim(u8, raw_src, " \n");
801 const src = try allocator.dupeZ(u8, src_non_terminated);
794802 try out.writeAll("<code class=\"zig\">");
795803 var tokenizer = std.zig.Tokenizer.init(src);
796804 var index: usize = 0;
......@@ -1016,12 +1024,24 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token:
10161024 try out.writeAll("</code>");
10171025}
10181026
1019fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token) !void {
1027fn tokenizeAndPrint(
1028 allocator: *Allocator,
1029 docgen_tokenizer: *Tokenizer,
1030 out: anytype,
1031 source_token: Token,
1032) !void {
10201033 const raw_src = docgen_tokenizer.buffer[source_token.start..source_token.end];
1021 return tokenizeAndPrintRaw(docgen_tokenizer, out, source_token, raw_src);
1034 return tokenizeAndPrintRaw(allocator, docgen_tokenizer, out, source_token, raw_src);
10221035}
10231036
1024fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: anytype, zig_exe: []const u8, do_code_tests: bool) !void {
1037fn genHtml(
1038 allocator: *Allocator,
1039 tokenizer: *Tokenizer,
1040 toc: *Toc,
1041 out: anytype,
1042 zig_exe: []const u8,
1043 do_code_tests: bool,
1044) !void {
10251045 var progress = Progress{};
10261046 const root_node = try progress.start("Generating docgen examples", toc.nodes.len);
10271047 defer root_node.end();
......@@ -1048,7 +1068,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
10481068 },
10491069 .Builtin => |tok| {
10501070 try out.writeAll("<pre>");
1051 try tokenizeAndPrintRaw(tokenizer, out, tok, builtin_code);
1071 try tokenizeAndPrintRaw(allocator, tokenizer, out, tok, builtin_code);
10521072 try out.writeAll("</pre>");
10531073 },
10541074 .HeaderOpen => |info| {
......@@ -1069,7 +1089,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
10691089 try out.writeAll("</ul>\n");
10701090 },
10711091 .Syntax => |content_tok| {
1072 try tokenizeAndPrint(tokenizer, out, content_tok);
1092 try tokenizeAndPrint(allocator, tokenizer, out, content_tok);
10731093 },
10741094 .Code => |code| {
10751095 const raw_source = tokenizer.buffer[code.source_token.start..code.source_token.end];
......@@ -1078,7 +1098,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
10781098 try out.print("<p class=\"file\">{s}.zig</p>", .{code.name});
10791099 }
10801100 try out.writeAll("<pre>");
1081 try tokenizeAndPrint(tokenizer, out, code.source_token);
1101 try tokenizeAndPrint(allocator, tokenizer, out, code.source_token);
10821102 try out.writeAll("</pre>");
10831103
10841104 if (!do_code_tests) {
......@@ -1497,7 +1517,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
14971517 }
14981518}
14991519
1500fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult {
1520fn exec(allocator: *Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult {
15011521 const result = try ChildProcess.exec(.{
15021522 .allocator = allocator,
15031523 .argv = args,
......@@ -1521,7 +1541,7 @@ fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u
15211541 return result;
15221542}
15231543
1524fn getBuiltinCode(allocator: *mem.Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
1544fn getBuiltinCode(allocator: *Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
15251545 const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
15261546 return result.stdout;
15271547}
doc/langref.html.in+16-4
......@@ -3025,7 +3025,7 @@ test "@tagName" {
30253025 </p>
30263026 {#code_begin|obj_err|parameter of type 'Foo' not allowed in function with calling convention 'C'#}
30273027const Foo = enum { a, b, c };
3028export fn entry(foo: Foo) void { }
3028export fn entry(foo: Foo) void { _ = foo; }
30293029 {#code_end#}
30303030 <p>
30313031 For a C-ABI-compatible enum, provide an explicit tag type to
......@@ -3346,7 +3346,7 @@ test "call foo" {
33463346 <p>
33473347 Blocks are used to limit the scope of variable declarations:
33483348 </p>
3349 {#code_begin|test_err|undeclared identifier#}
3349 {#code_begin|test_err|unused local variable#}
33503350test "access variable after block scope" {
33513351 {
33523352 var x: i32 = 1;
......@@ -3377,7 +3377,7 @@ test "labeled break from labeled block expression" {
33773377
33783378 {#header_open|Shadowing#}
33793379 <p>It is never allowed for an identifier to "hide" another one by using the same name:</p>
3380 {#code_begin|test_err|redefinition#}
3380 {#code_begin|test_err|local shadows declaration#}
33813381const pi = 3.14;
33823382
33833383test "inside test block" {
......@@ -5257,6 +5257,7 @@ test "float widening" {
52575257// Compile time coercion of float to int
52585258test "implicit cast to comptime_int" {
52595259 var f: f32 = 54.0 / 5;
5260 _ = f;
52605261}
52615262 {#code_end#}
52625263 {#header_close#}
......@@ -5817,6 +5818,7 @@ fn foo(condition: bool) void {
58175818 if (condition) f32 else u64,
58185819 1234,
58195820 5678);
5821 _ = result;
58205822}
58215823 {#code_end#}
58225824 <p>
......@@ -6313,7 +6315,7 @@ pub fn printValue(self: *Writer, value: anytype) !void {
63136315 <p>
63146316 And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}?
63156317 </p>
6316 {#code_begin|test_err|Unused argument in "here is a string: '{s}' here is a number: {}#}
6318 {#code_begin|test_err|Unused argument in 'here is a string: '{s}' here is a number: {}#}
63176319const print = @import("std").debug.print;
63186320
63196321const a_number: i32 = 1234;
......@@ -8853,6 +8855,7 @@ pub fn main() void {
88538855comptime {
88548856 const array: [5]u8 = "hello".*;
88558857 const garbage = array[5];
8858 _ = garbage;
88568859}
88578860 {#code_end#}
88588861 <p>At runtime:</p>
......@@ -8873,6 +8876,7 @@ fn foo(x: []const u8) u8 {
88738876comptime {
88748877 const value: i32 = -1;
88758878 const unsigned = @intCast(u32, value);
8879 _ = unsigned;
88768880}
88778881 {#code_end#}
88788882 <p>At runtime:</p>
......@@ -8895,6 +8899,7 @@ pub fn main() void {
88958899comptime {
88968900 const spartan_count: u16 = 300;
88978901 const byte = @intCast(u8, spartan_count);
8902 _ = byte;
88988903}
88998904 {#code_end#}
89008905 <p>At runtime:</p>
......@@ -9028,6 +9033,7 @@ test "wraparound addition and subtraction" {
90289033 {#code_begin|test_err|operation caused overflow#}
90299034comptime {
90309035 const x = @shlExact(@as(u8, 0b01010101), 2);
9036 _ = x;
90319037}
90329038 {#code_end#}
90339039 <p>At runtime:</p>
......@@ -9046,6 +9052,7 @@ pub fn main() void {
90469052 {#code_begin|test_err|exact shift shifted out 1 bits#}
90479053comptime {
90489054 const x = @shrExact(@as(u8, 0b10101010), 2);
9055 _ = x;
90499056}
90509057 {#code_end#}
90519058 <p>At runtime:</p>
......@@ -9066,6 +9073,7 @@ comptime {
90669073 const a: i32 = 1;
90679074 const b: i32 = 0;
90689075 const c = a / b;
9076 _ = c;
90699077}
90709078 {#code_end#}
90719079 <p>At runtime:</p>
......@@ -9087,6 +9095,7 @@ comptime {
90879095 const a: i32 = 10;
90889096 const b: i32 = 0;
90899097 const c = a % b;
9098 _ = c;
90909099}
90919100 {#code_end#}
90929101 <p>At runtime:</p>
......@@ -9108,6 +9117,7 @@ comptime {
91089117 const a: u32 = 10;
91099118 const b: u32 = 3;
91109119 const c = @divExact(a, b);
9120 _ = c;
91119121}
91129122 {#code_end#}
91139123 <p>At runtime:</p>
......@@ -9300,6 +9310,7 @@ fn foo(set1: Set1) void {
93009310comptime {
93019311 const ptr = @intToPtr(*align(1) i32, 0x1);
93029312 const aligned = @alignCast(4, ptr);
9313 _ = aligned;
93039314}
93049315 {#code_end#}
93059316 <p>At runtime:</p>
......@@ -9414,6 +9425,7 @@ fn bar(f: *Foo) void {
94149425comptime {
94159426 const opt_ptr: ?*i32 = null;
94169427 const ptr = @ptrCast(*i32, opt_ptr);
9428 _ = ptr;
94179429}
94189430 {#code_end#}
94199431 <p>At runtime:</p>