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;...@@ -8,6 +8,7 @@ const Progress = std.Progress;
8const print = std.debug.print;8const print = std.debug.print;
9const mem = std.mem;9const mem = std.mem;
10const testing = std.testing;10const testing = std.testing;
11const Allocator = std.mem.Allocator;
1112
12const max_doc_file_size = 10 * 1024 * 1024;13const max_doc_file_size = 10 * 1024 * 1024;
1314
...@@ -326,7 +327,7 @@ const Action = enum {...@@ -326,7 +327,7 @@ const Action = enum {
326 Close,327 Close,
327};328};
328329
329fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {330fn genToc(allocator: *Allocator, tokenizer: *Tokenizer) !Toc {
330 var urls = std.StringHashMap(Token).init(allocator);331 var urls = std.StringHashMap(Token).init(allocator);
331 errdefer urls.deinit();332 errdefer urls.deinit();
332333
...@@ -630,7 +631,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {...@@ -630,7 +631,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
630 };631 };
631}632}
632633
633fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {634fn urlize(allocator: *Allocator, input: []const u8) ![]u8 {
634 var buf = std.ArrayList(u8).init(allocator);635 var buf = std.ArrayList(u8).init(allocator);
635 defer buf.deinit();636 defer buf.deinit();
636637
...@@ -649,7 +650,7 @@ fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {...@@ -649,7 +650,7 @@ fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
649 return buf.toOwnedSlice();650 return buf.toOwnedSlice();
650}651}
651652
652fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 {653fn escapeHtml(allocator: *Allocator, input: []const u8) ![]u8 {
653 var buf = std.ArrayList(u8).init(allocator);654 var buf = std.ArrayList(u8).init(allocator);
654 defer buf.deinit();655 defer buf.deinit();
655656
...@@ -695,7 +696,7 @@ test "term color" {...@@ -695,7 +696,7 @@ test "term color" {
695 testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);696 testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);
696}697}
697698
698fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {699fn termColor(allocator: *Allocator, input: []const u8) ![]u8 {
699 var buf = std.ArrayList(u8).init(allocator);700 var buf = std.ArrayList(u8).init(allocator);
700 defer buf.deinit();701 defer buf.deinit();
701702
...@@ -789,8 +790,15 @@ fn isType(name: []const u8) bool {...@@ -789,8 +790,15 @@ fn isType(name: []const u8) bool {
789 return false;790 return false;
790}791}
791792
792fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token, raw_src: []const u8) !void {793fn tokenizeAndPrintRaw(
793 const src = mem.trim(u8, raw_src, " \n");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);
794 try out.writeAll("<code class=\"zig\">");802 try out.writeAll("<code class=\"zig\">");
795 var tokenizer = std.zig.Tokenizer.init(src);803 var tokenizer = std.zig.Tokenizer.init(src);
796 var index: usize = 0;804 var index: usize = 0;
...@@ -1016,12 +1024,24 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token:...@@ -1016,12 +1024,24 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token:
1016 try out.writeAll("</code>");1024 try out.writeAll("</code>");
1017}1025}
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 {
1020 const raw_src = docgen_tokenizer.buffer[source_token.start..source_token.end];1033 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);
1022}1035}
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 {
1025 var progress = Progress{};1045 var progress = Progress{};
1026 const root_node = try progress.start("Generating docgen examples", toc.nodes.len);1046 const root_node = try progress.start("Generating docgen examples", toc.nodes.len);
1027 defer root_node.end();1047 defer root_node.end();
...@@ -1048,7 +1068,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any...@@ -1048,7 +1068,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
1048 },1068 },
1049 .Builtin => |tok| {1069 .Builtin => |tok| {
1050 try out.writeAll("<pre>");1070 try out.writeAll("<pre>");
1051 try tokenizeAndPrintRaw(tokenizer, out, tok, builtin_code);1071 try tokenizeAndPrintRaw(allocator, tokenizer, out, tok, builtin_code);
1052 try out.writeAll("</pre>");1072 try out.writeAll("</pre>");
1053 },1073 },
1054 .HeaderOpen => |info| {1074 .HeaderOpen => |info| {
...@@ -1069,7 +1089,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any...@@ -1069,7 +1089,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
1069 try out.writeAll("</ul>\n");1089 try out.writeAll("</ul>\n");
1070 },1090 },
1071 .Syntax => |content_tok| {1091 .Syntax => |content_tok| {
1072 try tokenizeAndPrint(tokenizer, out, content_tok);1092 try tokenizeAndPrint(allocator, tokenizer, out, content_tok);
1073 },1093 },
1074 .Code => |code| {1094 .Code => |code| {
1075 const raw_source = tokenizer.buffer[code.source_token.start..code.source_token.end];1095 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...@@ -1078,7 +1098,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
1078 try out.print("<p class=\"file\">{s}.zig</p>", .{code.name});1098 try out.print("<p class=\"file\">{s}.zig</p>", .{code.name});
1079 }1099 }
1080 try out.writeAll("<pre>");1100 try out.writeAll("<pre>");
1081 try tokenizeAndPrint(tokenizer, out, code.source_token);1101 try tokenizeAndPrint(allocator, tokenizer, out, code.source_token);
1082 try out.writeAll("</pre>");1102 try out.writeAll("</pre>");
10831103
1084 if (!do_code_tests) {1104 if (!do_code_tests) {
...@@ -1497,7 +1517,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any...@@ -1497,7 +1517,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
1497 }1517 }
1498}1518}
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 {
1501 const result = try ChildProcess.exec(.{1521 const result = try ChildProcess.exec(.{
1502 .allocator = allocator,1522 .allocator = allocator,
1503 .argv = args,1523 .argv = args,
...@@ -1521,7 +1541,7 @@ fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u...@@ -1521,7 +1541,7 @@ fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u
1521 return result;1541 return result;
1522}1542}
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 {
1525 const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });1545 const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
1526 return result.stdout;1546 return result.stdout;
1527}1547}
doc/langref.html.in+16-4
...@@ -3025,7 +3025,7 @@ test "@tagName" {...@@ -3025,7 +3025,7 @@ test "@tagName" {
3025 </p>3025 </p>
3026 {#code_begin|obj_err|parameter of type 'Foo' not allowed in function with calling convention 'C'#}3026 {#code_begin|obj_err|parameter of type 'Foo' not allowed in function with calling convention 'C'#}
3027const Foo = enum { a, b, c };3027const Foo = enum { a, b, c };
3028export fn entry(foo: Foo) void { }3028export fn entry(foo: Foo) void { _ = foo; }
3029 {#code_end#}3029 {#code_end#}
3030 <p>3030 <p>
3031 For a C-ABI-compatible enum, provide an explicit tag type to3031 For a C-ABI-compatible enum, provide an explicit tag type to
...@@ -3346,7 +3346,7 @@ test "call foo" {...@@ -3346,7 +3346,7 @@ test "call foo" {
3346 <p>3346 <p>
3347 Blocks are used to limit the scope of variable declarations:3347 Blocks are used to limit the scope of variable declarations:
3348 </p>3348 </p>
3349 {#code_begin|test_err|undeclared identifier#}3349 {#code_begin|test_err|unused local variable#}
3350test "access variable after block scope" {3350test "access variable after block scope" {
3351 {3351 {
3352 var x: i32 = 1;3352 var x: i32 = 1;
...@@ -3377,7 +3377,7 @@ test "labeled break from labeled block expression" {...@@ -3377,7 +3377,7 @@ test "labeled break from labeled block expression" {
33773377
3378 {#header_open|Shadowing#}3378 {#header_open|Shadowing#}
3379 <p>It is never allowed for an identifier to "hide" another one by using the same name:</p>3379 <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#}
3381const pi = 3.14;3381const pi = 3.14;
33823382
3383test "inside test block" {3383test "inside test block" {
...@@ -5257,6 +5257,7 @@ test "float widening" {...@@ -5257,6 +5257,7 @@ test "float widening" {
5257// Compile time coercion of float to int5257// Compile time coercion of float to int
5258test "implicit cast to comptime_int" {5258test "implicit cast to comptime_int" {
5259 var f: f32 = 54.0 / 5;5259 var f: f32 = 54.0 / 5;
5260 _ = f;
5260}5261}
5261 {#code_end#}5262 {#code_end#}
5262 {#header_close#}5263 {#header_close#}
...@@ -5817,6 +5818,7 @@ fn foo(condition: bool) void {...@@ -5817,6 +5818,7 @@ fn foo(condition: bool) void {
5817 if (condition) f32 else u64,5818 if (condition) f32 else u64,
5818 1234,5819 1234,
5819 5678);5820 5678);
5821 _ = result;
5820}5822}
5821 {#code_end#}5823 {#code_end#}
5822 <p>5824 <p>
...@@ -6313,7 +6315,7 @@ pub fn printValue(self: *Writer, value: anytype) !void {...@@ -6313,7 +6315,7 @@ pub fn printValue(self: *Writer, value: anytype) !void {
6313 <p>6315 <p>
6314 And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}?6316 And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}?
6315 </p>6317 </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: {}#}
6317const print = @import("std").debug.print;6319const print = @import("std").debug.print;
63186320
6319const a_number: i32 = 1234;6321const a_number: i32 = 1234;
...@@ -8853,6 +8855,7 @@ pub fn main() void {...@@ -8853,6 +8855,7 @@ pub fn main() void {
8853comptime {8855comptime {
8854 const array: [5]u8 = "hello".*;8856 const array: [5]u8 = "hello".*;
8855 const garbage = array[5];8857 const garbage = array[5];
8858 _ = garbage;
8856}8859}
8857 {#code_end#}8860 {#code_end#}
8858 <p>At runtime:</p>8861 <p>At runtime:</p>
...@@ -8873,6 +8876,7 @@ fn foo(x: []const u8) u8 {...@@ -8873,6 +8876,7 @@ fn foo(x: []const u8) u8 {
8873comptime {8876comptime {
8874 const value: i32 = -1;8877 const value: i32 = -1;
8875 const unsigned = @intCast(u32, value);8878 const unsigned = @intCast(u32, value);
8879 _ = unsigned;
8876}8880}
8877 {#code_end#}8881 {#code_end#}
8878 <p>At runtime:</p>8882 <p>At runtime:</p>
...@@ -8895,6 +8899,7 @@ pub fn main() void {...@@ -8895,6 +8899,7 @@ pub fn main() void {
8895comptime {8899comptime {
8896 const spartan_count: u16 = 300;8900 const spartan_count: u16 = 300;
8897 const byte = @intCast(u8, spartan_count);8901 const byte = @intCast(u8, spartan_count);
8902 _ = byte;
8898}8903}
8899 {#code_end#}8904 {#code_end#}
8900 <p>At runtime:</p>8905 <p>At runtime:</p>
...@@ -9028,6 +9033,7 @@ test "wraparound addition and subtraction" {...@@ -9028,6 +9033,7 @@ test "wraparound addition and subtraction" {
9028 {#code_begin|test_err|operation caused overflow#}9033 {#code_begin|test_err|operation caused overflow#}
9029comptime {9034comptime {
9030 const x = @shlExact(@as(u8, 0b01010101), 2);9035 const x = @shlExact(@as(u8, 0b01010101), 2);
9036 _ = x;
9031}9037}
9032 {#code_end#}9038 {#code_end#}
9033 <p>At runtime:</p>9039 <p>At runtime:</p>
...@@ -9046,6 +9052,7 @@ pub fn main() void {...@@ -9046,6 +9052,7 @@ pub fn main() void {
9046 {#code_begin|test_err|exact shift shifted out 1 bits#}9052 {#code_begin|test_err|exact shift shifted out 1 bits#}
9047comptime {9053comptime {
9048 const x = @shrExact(@as(u8, 0b10101010), 2);9054 const x = @shrExact(@as(u8, 0b10101010), 2);
9055 _ = x;
9049}9056}
9050 {#code_end#}9057 {#code_end#}
9051 <p>At runtime:</p>9058 <p>At runtime:</p>
...@@ -9066,6 +9073,7 @@ comptime {...@@ -9066,6 +9073,7 @@ comptime {
9066 const a: i32 = 1;9073 const a: i32 = 1;
9067 const b: i32 = 0;9074 const b: i32 = 0;
9068 const c = a / b;9075 const c = a / b;
9076 _ = c;
9069}9077}
9070 {#code_end#}9078 {#code_end#}
9071 <p>At runtime:</p>9079 <p>At runtime:</p>
...@@ -9087,6 +9095,7 @@ comptime {...@@ -9087,6 +9095,7 @@ comptime {
9087 const a: i32 = 10;9095 const a: i32 = 10;
9088 const b: i32 = 0;9096 const b: i32 = 0;
9089 const c = a % b;9097 const c = a % b;
9098 _ = c;
9090}9099}
9091 {#code_end#}9100 {#code_end#}
9092 <p>At runtime:</p>9101 <p>At runtime:</p>
...@@ -9108,6 +9117,7 @@ comptime {...@@ -9108,6 +9117,7 @@ comptime {
9108 const a: u32 = 10;9117 const a: u32 = 10;
9109 const b: u32 = 3;9118 const b: u32 = 3;
9110 const c = @divExact(a, b);9119 const c = @divExact(a, b);
9120 _ = c;
9111}9121}
9112 {#code_end#}9122 {#code_end#}
9113 <p>At runtime:</p>9123 <p>At runtime:</p>
...@@ -9300,6 +9310,7 @@ fn foo(set1: Set1) void {...@@ -9300,6 +9310,7 @@ fn foo(set1: Set1) void {
9300comptime {9310comptime {
9301 const ptr = @intToPtr(*align(1) i32, 0x1);9311 const ptr = @intToPtr(*align(1) i32, 0x1);
9302 const aligned = @alignCast(4, ptr);9312 const aligned = @alignCast(4, ptr);
9313 _ = aligned;
9303}9314}
9304 {#code_end#}9315 {#code_end#}
9305 <p>At runtime:</p>9316 <p>At runtime:</p>
...@@ -9414,6 +9425,7 @@ fn bar(f: *Foo) void {...@@ -9414,6 +9425,7 @@ fn bar(f: *Foo) void {
9414comptime {9425comptime {
9415 const opt_ptr: ?*i32 = null;9426 const opt_ptr: ?*i32 = null;
9416 const ptr = @ptrCast(*i32, opt_ptr);9427 const ptr = @ptrCast(*i32, opt_ptr);
9428 _ = ptr;
9417}9429}
9418 {#code_end#}9430 {#code_end#}
9419 <p>At runtime:</p>9431 <p>At runtime:</p>