authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-20 18:36:04-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-20 18:36:04-05:00
log903127f36cf995d251a474438d3ecc6c6e0e30d2
tree10b45461abfb8933b2ac6c8121026490b22d1c6d
parent0f016b368de2ac93a298ab771646931329062fb5
parente381a42de9c0f0c5439a926b0ac99026a0373f49
signaturelock-open Commit is signed but in an unrecognized format.

Merge remote-tracking branch 'origin/master' into sub-architecture-annihilation


10 files changed, 63 insertions(+), 83 deletions(-)

CMakeLists.txt+1
......@@ -616,6 +616,7 @@ endif()
616616
617617set(BUILD_LIBSTAGE2_ARGS "build-lib"
618618 "src-self-hosted/stage2.zig"
619 -mcpu=baseline
619620 --name zigstage2
620621 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
621622 --cache on
lib/std/array_list.zig+25
......@@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
188188 self.len += items.len;
189189 }
190190
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
191199 /// Adjust the list's length to `new_len`. Doesn't initialize
192200 /// added items if any.
193201 pub fn resize(self: *Self, new_len: usize) !void {
......@@ -311,6 +319,23 @@ test "std.ArrayList.basic" {
311319 testing.expect(list.pop() == 33);
312320}
313321
322test "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
333test "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
314339test "std.ArrayList.orderedRemove" {
315340 var list = ArrayList(i32).init(testing.allocator);
316341 defer list.deinit();
lib/std/fs.zig+1
......@@ -864,6 +864,7 @@ pub const Dir = struct {
864864 .OBJECT_NAME_INVALID => unreachable,
865865 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
866866 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
867 .NO_MEDIA_IN_DEVICE => return error.NoDevice,
867868 .INVALID_PARAMETER => unreachable,
868869 .SHARING_VIOLATION => return error.SharingViolation,
869870 .ACCESS_DENIED => return error.AccessDenied,
lib/std/io.zig-67
......@@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct {
790790 }
791791};
792792
793pub 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)).
800pub 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
816test "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
832pub 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)).
839pub 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
847test "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
860793pub const Packing = enum {
861794 /// Pack data to byte alignment
862795 Byte,
src/ir.cpp+12-2
......@@ -26677,9 +26677,19 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2667726677
2667826678 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
2667926679 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;
2668226691 }
26692
2668326693 return ir_build_slice_gen(ira, &instruction->base.base, return_type,
2668426694 ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc);
2668526695}
src/ir_print.cpp-5
......@@ -590,11 +590,6 @@ static void ir_print_const_value(CodeGen *g, FILE *f, ZigValue *const_val) {
590590static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {
591591 if (inst == nullptr) {
592592 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);
598593 } else {
599594 ir_print_var_gen(irp, inst);
600595 }
src/stage2.cpp+1
......@@ -103,6 +103,7 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
103103 target->builtin_str = "Target.Cpu.baseline(arch);\n";
104104 target->cache_hash = "native\n\n";
105105 } else if (strcmp(mcpu, "baseline") == 0) {
106 target->is_native = false;
106107 target->llvm_cpu_name = "";
107108 target->llvm_cpu_features = "";
108109 target->builtin_str = "Target.Cpu.baseline(arch);\n";
test/compile_errors.zig+12
......@@ -3,6 +3,18 @@ const builtin = @import("builtin");
33const Target = @import("std").Target;
44
55pub 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
618 cases.addTest("access invalid @typeInfo decl",
719 \\const A = B;
820 \\test "Crash" {
test/standalone/guess_number/main.zig+7-7
......@@ -5,6 +5,7 @@ const fmt = std.fmt;
55
66pub fn main() !void {
77 const stdout = &io.getStdOut().outStream().stream;
8 const stdin = io.getStdIn();
89
910 try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{});
1011
......@@ -22,13 +23,12 @@ pub fn main() !void {
2223 try stdout.print("\nGuess a number between 1 and 100: ", .{});
2324 var line_buf: [20]u8 = undefined;
2425
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");
3232
3333 const guess = fmt.parseUnsigned(u8, line, 10) catch {
3434 try stdout.print("Invalid number.\n", .{});
test/tests.zig+4-2
......@@ -650,8 +650,10 @@ pub const StackTracesContext = struct {
650650 const got: []const u8 = got_result: {
651651 var buf = try Buffer.initSize(b.allocator, 0);
652652 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()];
655657 var it = mem.separate(bytes, "\n");
656658 process_lines: while (it.next()) |line| {
657659 if (line.len == 0) continue;