diff options
| -rw-r--r-- | lib/std/debug.zig | 2 | ||||
| -rw-r--r-- | lib/std/special/docs/index.html | 21 | ||||
| -rw-r--r-- | lib/std/testing.zig | 6 | ||||
| -rw-r--r-- | lib/std/zig.zig | 25 | ||||
| -rw-r--r-- | lib/std/zig/parser_test.zig | 5 | ||||
| -rw-r--r-- | lib/std/zig/render.zig | 12 | ||||
| -rw-r--r-- | src/Compilation.zig | 41 | ||||
| -rw-r--r-- | src/main.zig | 6 |
8 files changed, 91 insertions, 27 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig index a7badf7ed1..c84a0e0f18 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -462,7 +462,7 @@ pub const TTY = struct { // TODO give this a payload of file handle windows_api, - fn setColor(conf: Config, out_stream: anytype, color: Color) void { + pub fn setColor(conf: Config, out_stream: anytype, color: Color) void { nosuspend switch (conf) { .no_color => return, .escape_codes => switch (color) { diff --git a/lib/std/special/docs/index.html b/lib/std/special/docs/index.html index 98f1938cc6..61e3bb4ed2 100644 --- a/lib/std/special/docs/index.html +++ b/lib/std/special/docs/index.html @@ -43,6 +43,8 @@ /* layout */ .canvas { + display: flex; + flex-direction: column; width: 100vw; height: 100vh; overflow: hidden; @@ -53,12 +55,21 @@ background-color: var(--bg-color); } + .banner { + background-color: darkred; + text-align: center; + color: white; + padding: 15px 5px; + } + + .banner a { + color: bisque; + text-decoration: underline; + } + .flex-main { display: flex; - width: 100%; - height: 100%; - justify-content: center; - + overflow-y: hidden; z-index: 100; } @@ -515,7 +526,7 @@ </style> </head> <body class="canvas"> - <div style="background-color: darkred; width: 100vw; text-align: center; color: white; padding: 15px 5px;">These docs are experimental. <a style="color: bisque;text-decoration: underline;" href="https://kristoff.it/blog/zig-new-relationship-llvm/">Progress depends on the self-hosted compiler</a>, <a style="color: bisque;text-decoration: underline;" href="https://github.com/ziglang/zig/wiki/How-to-read-the-standard-library-source-code">consider reading the stlib source in the meantime</a>.</div> + <div class="banner">These docs are experimental. <a href="https://kristoff.it/blog/zig-new-relationship-llvm/">Progress depends on the self-hosted compiler</a>, <a href="https://github.com/ziglang/zig/wiki/How-to-read-the-standard-library-source-code">consider reading the stdlib source in the meantime</a>.</div> <div class="flex-main"> <div class="flex-filler"></div> <div class="flex-left sidebar"> diff --git a/lib/std/testing.zig b/lib/std/testing.zig index eb2b6e87b3..ac1aa3bf6d 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -431,7 +431,7 @@ fn printIndicatorLine(source: []const u8, indicator_index: usize) void { fn printWithVisibleNewlines(source: []const u8) void { var i: usize = 0; - while (std.mem.indexOf(u8, source[i..], "\n")) |nl| : (i += nl + 1) { + while (std.mem.indexOfScalar(u8, source[i..], '\n')) |nl| : (i += nl + 1) { printLine(source[i .. i + nl]); } print("{s}␃\n", .{source[i..]}); // End of Text symbol (ETX) @@ -439,7 +439,7 @@ fn printWithVisibleNewlines(source: []const u8) void { fn printLine(line: []const u8) void { if (line.len != 0) switch (line[line.len - 1]) { - ' ', '\t' => print("{s}⏎\n", .{line}), // Carriage return symbol, + ' ', '\t' => return print("{s}⏎\n", .{line}), // Carriage return symbol, else => {}, }; print("{s}\n", .{line}); @@ -451,7 +451,7 @@ test { /// Given a type, reference all the declarations inside, so that the semantic analyzer sees them. pub fn refAllDecls(comptime T: type) void { - if (!@import("builtin").is_test) return; + if (!std.builtin.is_test) return; inline for (std.meta.declarations(T)) |decl| { _ = decl; } diff --git a/lib/std/zig.zig b/lib/std/zig.zig index cff07a2bd2..f60b15f81b 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -31,21 +31,38 @@ pub fn hashSrc(src: []const u8) SrcHash { return out; } -pub fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } { +pub const Loc = struct { + line: usize, + column: usize, + /// Does not include the trailing newline. + source_line: []const u8, +}; + +pub fn findLineColumn(source: []const u8, byte_offset: usize) Loc { var line: usize = 0; var column: usize = 0; - for (source[0..byte_offset]) |byte| { - switch (byte) { + var line_start: usize = 0; + var i: usize = 0; + while (i < byte_offset) : (i += 1) { + switch (source[i]) { '\n' => { line += 1; column = 0; + line_start = i + 1; }, else => { column += 1; }, } } - return .{ .line = line, .column = column }; + while (i < source.len and source[i] != '\n') { + i += 1; + } + return .{ + .line = line, + .column = column, + .source_line = source[line_start..i], + }; } pub fn lineDelta(source: []const u8, start: usize, end: usize) isize { diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 2a343a6edc..321f37c422 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1812,6 +1812,8 @@ test "zig fmt: array literal veritical column alignment" { \\ 4,5,600,7, \\ 80, \\ 9, 10, 11, 0, 13, 14, 15}; + \\const a = [12]u8{ + \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; \\ , \\const a = []u8{ @@ -1825,6 +1827,9 @@ test "zig fmt: array literal veritical column alignment" { \\ 9, 10, 11, 0, 13, \\ 14, 15, \\}; + \\const a = [12]u8{ + \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, + \\}; \\ ); } diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 7add8383f3..1aa2fe8af6 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1653,7 +1653,8 @@ fn renderArrayInit( try renderToken(ais, tree, array_init.ast.lbrace, .newline); var expr_index: usize = 0; - while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| { + while (true) { + const row_size = rowSize(tree, array_init.ast.elements[expr_index..], rbrace); const row_exprs = array_init.ast.elements[expr_index..]; // A place to store the width of each expression and its column's maximum const widths = try gpa.alloc(usize, row_exprs.len + row_size); @@ -1686,7 +1687,7 @@ fn renderArrayInit( const maybe_comma = expr_last_token + 1; if (token_tags[maybe_comma] == .comma) { if (hasSameLineComment(tree, maybe_comma)) - break :sec_end i - this_line_size.? + 1; + break :sec_end i - this_line_size + 1; } } break :sec_end row_exprs.len; @@ -2500,9 +2501,8 @@ fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool { }; } -// Returns the number of nodes in `expr` that are on the same line as `rtoken`, -// or null if they all are on the same line. -fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) ?usize { +// Returns the number of nodes in `expr` that are on the same line as `rtoken`. +fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) usize { const token_tags = tree.tokens.items(.tag); const first_token = tree.firstToken(exprs[0]); @@ -2510,7 +2510,7 @@ fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex const maybe_comma = rtoken - 1; if (token_tags[maybe_comma] == .comma) return 1; - return null; // no newlines + return exprs.len; // no newlines } var count: usize = 1; diff --git a/src/Compilation.zig b/src/Compilation.zig index 080d4bddaa..349963ebe3 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -272,28 +272,52 @@ pub const AllErrors = struct { line: u32, column: u32, byte_offset: u32, + /// Does not include the trailing newline. + source_line: ?[]const u8, notes: []Message = &.{}, }, plain: struct { msg: []const u8, }, - pub fn renderToStdErr(msg: Message) void { - return msg.renderToStdErrInner("error"); + pub fn renderToStdErr(msg: Message, ttyconf: std.debug.TTY.Config) void { + const stderr_mutex = std.debug.getStderrMutex(); + const held = std.debug.getStderrMutex().acquire(); + defer held.release(); + const stderr = std.io.getStdErr(); + return msg.renderToStdErrInner(ttyconf, stderr, "error", .Red) catch return; } - fn renderToStdErrInner(msg: Message, kind: []const u8) void { + fn renderToStdErrInner( + msg: Message, + ttyconf: std.debug.TTY.Config, + stderr_file: std.fs.File, + kind: []const u8, + color: std.debug.TTY.Color, + ) anyerror!void { + const stderr = stderr_file.writer(); switch (msg) { .src => |src| { - std.debug.print("{s}:{d}:{d}: {s}: {s}\n", .{ + try stderr.print("{s}:{d}:{d}: ", .{ src.src_path, src.line + 1, src.column + 1, - kind, - src.msg, }); + ttyconf.setColor(stderr, color); + try stderr.writeAll(kind); + ttyconf.setColor(stderr, .Bold); + try stderr.print(" {s}\n", .{src.msg}); + ttyconf.setColor(stderr, .Reset); + if (src.source_line) |line| { + try stderr.writeAll(line); + try stderr.writeByte('\n'); + try stderr.writeByteNTimes(' ', src.column); + ttyconf.setColor(stderr, .Green); + try stderr.writeAll("^\n"); + ttyconf.setColor(stderr, .Reset); + } for (src.notes) |note| { - note.renderToStdErrInner("note"); + try note.renderToStdErrInner(ttyconf, stderr_file, "note", .Cyan); } }, .plain => |plain| { @@ -327,6 +351,7 @@ pub const AllErrors = struct { .byte_offset = byte_offset, .line = @intCast(u32, loc.line), .column = @intCast(u32, loc.column), + .source_line = try arena.allocator.dupe(u8, loc.source_line), }, }; } @@ -342,6 +367,7 @@ pub const AllErrors = struct { .line = @intCast(u32, loc.line), .column = @intCast(u32, loc.column), .notes = notes, + .source_line = try arena.allocator.dupe(u8, loc.source_line), }, }); } @@ -1489,6 +1515,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors { .byte_offset = 0, .line = err_msg.line, .column = err_msg.column, + .source_line = null, // TODO }, }); } diff --git a/src/main.zig b/src/main.zig index 5fb74db61f..11857fff22 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2106,8 +2106,12 @@ fn updateModule(gpa: *Allocator, comp: *Compilation, hook: AfterUpdateHook) !voi defer errors.deinit(comp.gpa); if (errors.list.len != 0) { + const ttyconf: std.debug.TTY.Config = switch (comp.color) { + .auto, .on => std.debug.detectTTYConfig(), + .off => .no_color, + }; for (errors.list) |full_err_msg| { - full_err_msg.renderToStdErr(); + full_err_msg.renderToStdErr(ttyconf); } const log_text = comp.getCompileLogOutput(); if (log_text.len != 0) { |
