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()...@@ -616,6 +616,7 @@ endif()
616616
617set(BUILD_LIBSTAGE2_ARGS "build-lib"617set(BUILD_LIBSTAGE2_ARGS "build-lib"
618 "src-self-hosted/stage2.zig"618 "src-self-hosted/stage2.zig"
619 -mcpu=baseline
619 --name zigstage2620 --name zigstage2
620 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"621 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
621 --cache on622 --cache on
lib/std/array_list.zig+25
...@@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {...@@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
188 self.len += items.len;188 self.len += items.len;
189 }189 }
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
191 /// Adjust the list's length to `new_len`. Doesn't initialize199 /// Adjust the list's length to `new_len`. Doesn't initialize
192 /// added items if any.200 /// added items if any.
193 pub fn resize(self: *Self, new_len: usize) !void {201 pub fn resize(self: *Self, new_len: usize) !void {
...@@ -311,6 +319,23 @@ test "std.ArrayList.basic" {...@@ -311,6 +319,23 @@ test "std.ArrayList.basic" {
311 testing.expect(list.pop() == 33);319 testing.expect(list.pop() == 33);
312}320}
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
314test "std.ArrayList.orderedRemove" {339test "std.ArrayList.orderedRemove" {
315 var list = ArrayList(i32).init(testing.allocator);340 var list = ArrayList(i32).init(testing.allocator);
316 defer list.deinit();341 defer list.deinit();
lib/std/fs.zig+1
...@@ -864,6 +864,7 @@ pub const Dir = struct {...@@ -864,6 +864,7 @@ pub const Dir = struct {
864 .OBJECT_NAME_INVALID => unreachable,864 .OBJECT_NAME_INVALID => unreachable,
865 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,865 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
866 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,866 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
867 .NO_MEDIA_IN_DEVICE => return error.NoDevice,
867 .INVALID_PARAMETER => unreachable,868 .INVALID_PARAMETER => unreachable,
868 .SHARING_VIOLATION => return error.SharingViolation,869 .SHARING_VIOLATION => return error.SharingViolation,
869 .ACCESS_DENIED => return error.AccessDenied,870 .ACCESS_DENIED => return error.AccessDenied,
lib/std/io.zig-67
...@@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct {...@@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct {
790 }790 }
791};791};
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
860pub const Packing = enum {793pub const Packing = enum {
861 /// Pack data to byte alignment794 /// Pack data to byte alignment
862 Byte,795 Byte,
src/ir.cpp+12-2
...@@ -26677,9 +26677,19 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26677,9 +26677,19 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2667726677
26678 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,26678 IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
26679 return_type, nullptr, true, true);26679 return_type, nullptr, true, true);
26680 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {26680
26681 return result_loc;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 return ir_build_slice_gen(ira, &instruction->base.base, return_type,26693 return ir_build_slice_gen(ira, &instruction->base.base, return_type,
26684 ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc);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,11 +590,6 @@ static void ir_print_const_value(CodeGen *g, FILE *f, ZigValue *const_val) {
590static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {590static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {
591 if (inst == nullptr) {591 if (inst == nullptr) {
592 fprintf(irp->f, "(null)");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 } else {593 } else {
599 ir_print_var_gen(irp, inst);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,6 +103,7 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
103 target->builtin_str = "Target.Cpu.baseline(arch);\n";103 target->builtin_str = "Target.Cpu.baseline(arch);\n";
104 target->cache_hash = "native\n\n";104 target->cache_hash = "native\n\n";
105 } else if (strcmp(mcpu, "baseline") == 0) {105 } else if (strcmp(mcpu, "baseline") == 0) {
106 target->is_native = false;
106 target->llvm_cpu_name = "";107 target->llvm_cpu_name = "";
107 target->llvm_cpu_features = "";108 target->llvm_cpu_features = "";
108 target->builtin_str = "Target.Cpu.baseline(arch);\n";109 target->builtin_str = "Target.Cpu.baseline(arch);\n";
test/compile_errors.zig+12
...@@ -3,6 +3,18 @@ const builtin = @import("builtin");...@@ -3,6 +3,18 @@ const builtin = @import("builtin");
3const Target = @import("std").Target;3const Target = @import("std").Target;
44
5pub fn addCases(cases: *tests.CompileErrorContext) void {5pub 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 cases.addTest("access invalid @typeInfo decl",18 cases.addTest("access invalid @typeInfo decl",
7 \\const A = B;19 \\const A = B;
8 \\test "Crash" {20 \\test "Crash" {
test/standalone/guess_number/main.zig+7-7
...@@ -5,6 +5,7 @@ const fmt = std.fmt;...@@ -5,6 +5,7 @@ const fmt = std.fmt;
55
6pub fn main() !void {6pub fn main() !void {
7 const stdout = &io.getStdOut().outStream().stream;7 const stdout = &io.getStdOut().outStream().stream;
8 const stdin = io.getStdIn();
89
9 try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{});10 try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{});
1011
...@@ -22,13 +23,12 @@ pub fn main() !void {...@@ -22,13 +23,12 @@ pub fn main() !void {
22 try stdout.print("\nGuess a number between 1 and 100: ", .{});23 try stdout.print("\nGuess a number between 1 and 100: ", .{});
23 var line_buf: [20]u8 = undefined;24 var line_buf: [20]u8 = undefined;
2425
25 const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) {26 const amt = try stdin.read(&line_buf);
26 error.OutOfMemory => {27 if (amt == line_buf.len) {
27 try stdout.print("Input too long.\n", .{});28 try stdout.print("Input too long.\n", .{});
28 continue;29 continue;
29 },30 }
30 else => return err,31 const line = std.mem.trimRight(u8, line_buf[0..amt], "\r\n");
31 };
3232
33 const guess = fmt.parseUnsigned(u8, line, 10) catch {33 const guess = fmt.parseUnsigned(u8, line, 10) catch {
34 try stdout.print("Invalid number.\n", .{});34 try stdout.print("Invalid number.\n", .{});
test/tests.zig+4-2
...@@ -650,8 +650,10 @@ pub const StackTracesContext = struct {...@@ -650,8 +650,10 @@ pub const StackTracesContext = struct {
650 const got: []const u8 = got_result: {650 const got: []const u8 = got_result: {
651 var buf = try Buffer.initSize(b.allocator, 0);651 var buf = try Buffer.initSize(b.allocator, 0);
652 defer buf.deinit();652 defer buf.deinit();
653 var bytes = stderr.toSliceConst();653 const bytes = if (stderr.endsWith("\n"))
654 if (bytes.len != 0 and bytes[bytes.len - 1] == '\n') bytes = bytes[0 .. bytes.len - 1];654 stderr.toSliceConst()[0 .. stderr.len() - 1]
655 else
656 stderr.toSliceConst()[0..stderr.len()];
655 var it = mem.separate(bytes, "\n");657 var it = mem.separate(bytes, "\n");
656 process_lines: while (it.next()) |line| {658 process_lines: while (it.next()) |line| {
657 if (line.len == 0) continue;659 if (line.len == 0) continue;