| author | |
| committer | |
| log | 903127f36cf995d251a474438d3ecc6c6e0e30d2 |
| tree | 10b45461abfb8933b2ac6c8121026490b22d1c6d |
| parent | 0f016b368de2ac93a298ab771646931329062fb5 |
| parent | e381a42de9c0f0c5439a926b0ac99026a0373f49 |
| signature |
10 files changed, 63 insertions(+), 83 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -616,6 +616,7 @@ endif() |
| 616 | 616 | |
| 617 | 617 | set(BUILD_LIBSTAGE2_ARGS "build-lib" |
| 618 | 618 | "src-self-hosted/stage2.zig" |
| 619 | -mcpu=baseline | |
| 619 | 620 | --name zigstage2 |
| 620 | 621 | --override-lib-dir "${CMAKE_SOURCE_DIR}/lib" |
| 621 | 622 | --cache on |
lib/std/array_list.zig+25| ... | ... | @@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 188 | 188 | self.len += items.len; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | /// Append a value to the list `n` times. Allocates more memory | |
| 192 | /// as necessary. | |
| 193 | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { | |
| 194 | const old_len = self.len; | |
| 195 | try self.resize(self.len + n); | |
| 196 | mem.set(T, self.items[old_len..self.len], value); | |
| 197 | } | |
| 198 | ||
| 191 | 199 | /// Adjust the list's length to `new_len`. Doesn't initialize |
| 192 | 200 | /// added items if any. |
| 193 | 201 | pub fn resize(self: *Self, new_len: usize) !void { |
| ... | ... | @@ -311,6 +319,23 @@ test "std.ArrayList.basic" { |
| 311 | 319 | testing.expect(list.pop() == 33); |
| 312 | 320 | } |
| 313 | 321 | |
| 322 | test "std.ArrayList.appendNTimes" { | |
| 323 | var list = ArrayList(i32).init(testing.allocator); | |
| 324 | defer list.deinit(); | |
| 325 | ||
| 326 | try list.appendNTimes(2, 10); | |
| 327 | testing.expectEqual(@as(usize, 10), list.len); | |
| 328 | for (list.toSlice()) |element| { | |
| 329 | testing.expectEqual(@as(i32, 2), element); | |
| 330 | } | |
| 331 | } | |
| 332 | ||
| 333 | test "std.ArrayList.appendNTimes with failing allocator" { | |
| 334 | var list = ArrayList(i32).init(testing.failing_allocator); | |
| 335 | defer list.deinit(); | |
| 336 | testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10)); | |
| 337 | } | |
| 338 | ||
| 314 | 339 | test "std.ArrayList.orderedRemove" { |
| 315 | 340 | var list = ArrayList(i32).init(testing.allocator); |
| 316 | 341 | defer list.deinit(); |
lib/std/fs.zig+1| ... | ... | @@ -864,6 +864,7 @@ pub const Dir = struct { |
| 864 | 864 | .OBJECT_NAME_INVALID => unreachable, |
| 865 | 865 | .OBJECT_NAME_NOT_FOUND => return error.FileNotFound, |
| 866 | 866 | .OBJECT_PATH_NOT_FOUND => return error.FileNotFound, |
| 867 | .NO_MEDIA_IN_DEVICE => return error.NoDevice, | |
| 867 | 868 | .INVALID_PARAMETER => unreachable, |
| 868 | 869 | .SHARING_VIOLATION => return error.SharingViolation, |
| 869 | 870 | .ACCESS_DENIED => return error.AccessDenied, |
lib/std/io.zig-67| ... | ... | @@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct { |
| 790 | 790 | } |
| 791 | 791 | }; |
| 792 | 792 | |
| 793 | pub fn readLine(buf: *std.Buffer) ![]u8 { | |
| 794 | var stdin_stream = getStdIn().inStream(); | |
| 795 | return readLineFrom(&stdin_stream.stream, buf); | |
| 796 | } | |
| 797 | ||
| 798 | /// Reads all characters until the next newline into buf, and returns | |
| 799 | /// a slice of the characters read (excluding the newline character(s)). | |
| 800 | pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 { | |
| 801 | const start = buf.len(); | |
| 802 | while (true) { | |
| 803 | const byte = try stream.readByte(); | |
| 804 | switch (byte) { | |
| 805 | '\r' => { | |
| 806 | // trash the following \n | |
| 807 | _ = try stream.readByte(); | |
| 808 | return buf.toSlice()[start..]; | |
| 809 | }, | |
| 810 | '\n' => return buf.toSlice()[start..], | |
| 811 | else => try buf.appendByte(byte), | |
| 812 | } | |
| 813 | } | |
| 814 | } | |
| 815 | ||
| 816 | test "io.readLineFrom" { | |
| 817 | var buf = try std.Buffer.initSize(testing.allocator, 0); | |
| 818 | defer buf.deinit(); | |
| 819 | var mem_stream = SliceInStream.init( | |
| 820 | \\Line 1 | |
| 821 | \\Line 22 | |
| 822 | \\Line 333 | |
| 823 | ); | |
| 824 | const stream = &mem_stream.stream; | |
| 825 | ||
| 826 | testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf)); | |
| 827 | testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf)); | |
| 828 | testing.expectError(error.EndOfStream, readLineFrom(stream, &buf)); | |
| 829 | testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice()); | |
| 830 | } | |
| 831 | ||
| 832 | pub fn readLineSlice(slice: []u8) ![]u8 { | |
| 833 | var stdin_stream = getStdIn().inStream(); | |
| 834 | return readLineSliceFrom(&stdin_stream.stream, slice); | |
| 835 | } | |
| 836 | ||
| 837 | /// Reads all characters until the next newline into slice, and returns | |
| 838 | /// a slice of the characters read (excluding the newline character(s)). | |
| 839 | pub fn readLineSliceFrom(stream: var, slice: []u8) ![]u8 { | |
| 840 | // We cannot use Buffer.fromOwnedSlice, as it wants to append a null byte | |
| 841 | // after taking ownership, which would always require an allocation. | |
| 842 | var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(testing.failing_allocator, slice) }; | |
| 843 | try buf.resize(0); | |
| 844 | return try readLineFrom(stream, &buf); | |
| 845 | } | |
| 846 | ||
| 847 | test "io.readLineSliceFrom" { | |
| 848 | var buf: [7]u8 = undefined; | |
| 849 | var mem_stream = SliceInStream.init( | |
| 850 | \\Line 1 | |
| 851 | \\Line 22 | |
| 852 | \\Line 333 | |
| 853 | ); | |
| 854 | const stream = &mem_stream.stream; | |
| 855 | ||
| 856 | testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..])); | |
| 857 | testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..])); | |
| 858 | } | |
| 859 | ||
| 860 | 793 | pub const Packing = enum { |
| 861 | 794 | /// Pack data to byte alignment |
| 862 | 795 | Byte, |
src/ir.cpp+12-2| ... | ... | @@ -26677,9 +26677,19 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i |
| 26677 | 26677 | |
| 26678 | 26678 | IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc, |
| 26679 | 26679 | return_type, nullptr, true, true); |
| 26680 | if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) { | |
| 26681 | return result_loc; | |
| 26680 | ||
| 26681 | if (result_loc != nullptr) { | |
| 26682 | if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) { | |
| 26683 | return result_loc; | |
| 26684 | } | |
| 26685 | IrInstGen *dummy_value = ir_const(ira, &instruction->base.base, return_type); | |
| 26686 | dummy_value->value->special = ConstValSpecialRuntime; | |
| 26687 | IrInstGen *dummy_result = ir_implicit_cast2(ira, &instruction->base.base, | |
| 26688 | dummy_value, result_loc->value->type->data.pointer.child_type); | |
| 26689 | if (type_is_invalid(dummy_result->value->type)) | |
| 26690 | return ira->codegen->invalid_inst_gen; | |
| 26682 | 26691 | } |
| 26692 | ||
| 26683 | 26693 | return ir_build_slice_gen(ira, &instruction->base.base, return_type, |
| 26684 | 26694 | ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc); |
| 26685 | 26695 | } |
src/ir_print.cpp-5| ... | ... | @@ -590,11 +590,6 @@ static void ir_print_const_value(CodeGen *g, FILE *f, ZigValue *const_val) { |
| 590 | 590 | static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) { |
| 591 | 591 | if (inst == nullptr) { |
| 592 | 592 | fprintf(irp->f, "(null)"); |
| 593 | return; | |
| 594 | } | |
| 595 | ||
| 596 | if (inst->value->special != ConstValSpecialRuntime) { | |
| 597 | ir_print_const_value(irp->codegen, irp->f, inst->value); | |
| 598 | 593 | } else { |
| 599 | 594 | ir_print_var_gen(irp, inst); |
| 600 | 595 | } |
src/stage2.cpp+1| ... | ... | @@ -103,6 +103,7 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons |
| 103 | 103 | target->builtin_str = "Target.Cpu.baseline(arch);\n"; |
| 104 | 104 | target->cache_hash = "native\n\n"; |
| 105 | 105 | } else if (strcmp(mcpu, "baseline") == 0) { |
| 106 | target->is_native = false; | |
| 106 | 107 | target->llvm_cpu_name = ""; |
| 107 | 108 | target->llvm_cpu_features = ""; |
| 108 | 109 | target->builtin_str = "Target.Cpu.baseline(arch);\n"; |
test/compile_errors.zig+12| ... | ... | @@ -3,6 +3,18 @@ const builtin = @import("builtin"); |
| 3 | 3 | const Target = @import("std").Target; |
| 4 | 4 | |
| 5 | 5 | pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 6 | cases.addTest("slice to pointer conversion mismatch", | |
| 7 | \\pub fn bytesAsSlice(bytes: var) [*]align(1) const u16 { | |
| 8 | \\ return @ptrCast([*]align(1) const u16, bytes.ptr)[0..1]; | |
| 9 | \\} | |
| 10 | \\test "bytesAsSlice" { | |
| 11 | \\ const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; | |
| 12 | \\ const slice = bytesAsSlice(bytes[0..]); | |
| 13 | \\} | |
| 14 | , &[_][]const u8{ | |
| 15 | "tmp.zig:2:54: error: expected type '[*]align(1) const u16', found '[]align(1) const u16'", | |
| 16 | }); | |
| 17 | ||
| 6 | 18 | cases.addTest("access invalid @typeInfo decl", |
| 7 | 19 | \\const A = B; |
| 8 | 20 | \\test "Crash" { |
test/standalone/guess_number/main.zig+7-7| ... | ... | @@ -5,6 +5,7 @@ const fmt = std.fmt; |
| 5 | 5 | |
| 6 | 6 | pub fn main() !void { |
| 7 | 7 | const stdout = &io.getStdOut().outStream().stream; |
| 8 | const stdin = io.getStdIn(); | |
| 8 | 9 | |
| 9 | 10 | try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{}); |
| 10 | 11 | |
| ... | ... | @@ -22,13 +23,12 @@ pub fn main() !void { |
| 22 | 23 | try stdout.print("\nGuess a number between 1 and 100: ", .{}); |
| 23 | 24 | var line_buf: [20]u8 = undefined; |
| 24 | 25 | |
| 25 | const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) { | |
| 26 | error.OutOfMemory => { | |
| 27 | try stdout.print("Input too long.\n", .{}); | |
| 28 | continue; | |
| 29 | }, | |
| 30 | else => return err, | |
| 31 | }; | |
| 26 | const amt = try stdin.read(&line_buf); | |
| 27 | if (amt == line_buf.len) { | |
| 28 | try stdout.print("Input too long.\n", .{}); | |
| 29 | continue; | |
| 30 | } | |
| 31 | const line = std.mem.trimRight(u8, line_buf[0..amt], "\r\n"); | |
| 32 | 32 | |
| 33 | 33 | const guess = fmt.parseUnsigned(u8, line, 10) catch { |
| 34 | 34 | try stdout.print("Invalid number.\n", .{}); |
test/tests.zig+4-2| ... | ... | @@ -650,8 +650,10 @@ pub const StackTracesContext = struct { |
| 650 | 650 | const got: []const u8 = got_result: { |
| 651 | 651 | var buf = try Buffer.initSize(b.allocator, 0); |
| 652 | 652 | defer buf.deinit(); |
| 653 | var bytes = stderr.toSliceConst(); | |
| 654 | if (bytes.len != 0 and bytes[bytes.len - 1] == '\n') bytes = bytes[0 .. bytes.len - 1]; | |
| 653 | const bytes = if (stderr.endsWith("\n")) | |
| 654 | stderr.toSliceConst()[0 .. stderr.len() - 1] | |
| 655 | else | |
| 656 | stderr.toSliceConst()[0..stderr.len()]; | |
| 655 | 657 | var it = mem.separate(bytes, "\n"); |
| 656 | 658 | process_lines: while (it.next()) |line| { |
| 657 | 659 | if (line.len == 0) continue; |