commit 903127f36cf995d251a474438d3ecc6c6e0e30d2 (tree)
parent 0f016b368de2ac93a298ab771646931329062fb5
Author: Andrew Kelley <andrew@ziglang.org>
Date: Thu, 20 Feb 2020 18:36:04 -0500
Merge remote-tracking branch 'origin/master' into sub-architecture-annihilation
Diffstat:
10 files changed, 63 insertions(+), 83 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -616,6 +616,7 @@ endif()
set(BUILD_LIBSTAGE2_ARGS "build-lib"
"src-self-hosted/stage2.zig"
+ -mcpu=baseline
--name zigstage2
--override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
--cache on
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
@@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
self.len += items.len;
}
+ /// Append a value to the list `n` times. Allocates more memory
+ /// as necessary.
+ pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
+ const old_len = self.len;
+ try self.resize(self.len + n);
+ mem.set(T, self.items[old_len..self.len], value);
+ }
+
/// Adjust the list's length to `new_len`. Doesn't initialize
/// added items if any.
pub fn resize(self: *Self, new_len: usize) !void {
@@ -311,6 +319,23 @@ test "std.ArrayList.basic" {
testing.expect(list.pop() == 33);
}
+test "std.ArrayList.appendNTimes" {
+ var list = ArrayList(i32).init(testing.allocator);
+ defer list.deinit();
+
+ try list.appendNTimes(2, 10);
+ testing.expectEqual(@as(usize, 10), list.len);
+ for (list.toSlice()) |element| {
+ testing.expectEqual(@as(i32, 2), element);
+ }
+}
+
+test "std.ArrayList.appendNTimes with failing allocator" {
+ var list = ArrayList(i32).init(testing.failing_allocator);
+ defer list.deinit();
+ testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10));
+}
+
test "std.ArrayList.orderedRemove" {
var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
@@ -864,6 +864,7 @@ pub const Dir = struct {
.OBJECT_NAME_INVALID => unreachable,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
+ .NO_MEDIA_IN_DEVICE => return error.NoDevice,
.INVALID_PARAMETER => unreachable,
.SHARING_VIOLATION => return error.SharingViolation,
.ACCESS_DENIED => return error.AccessDenied,
diff --git a/lib/std/io.zig b/lib/std/io.zig
@@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct {
}
};
-pub fn readLine(buf: *std.Buffer) ![]u8 {
- var stdin_stream = getStdIn().inStream();
- return readLineFrom(&stdin_stream.stream, buf);
-}
-
-/// Reads all characters until the next newline into buf, and returns
-/// a slice of the characters read (excluding the newline character(s)).
-pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {
- const start = buf.len();
- while (true) {
- const byte = try stream.readByte();
- switch (byte) {
- '\r' => {
- // trash the following \n
- _ = try stream.readByte();
- return buf.toSlice()[start..];
- },
- '\n' => return buf.toSlice()[start..],
- else => try buf.appendByte(byte),
- }
- }
-}
-
-test "io.readLineFrom" {
- var buf = try std.Buffer.initSize(testing.allocator, 0);
- defer buf.deinit();
- var mem_stream = SliceInStream.init(
- \\Line 1
- \\Line 22
- \\Line 333
- );
- const stream = &mem_stream.stream;
-
- testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf));
- testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf));
- testing.expectError(error.EndOfStream, readLineFrom(stream, &buf));
- testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice());
-}
-
-pub fn readLineSlice(slice: []u8) ![]u8 {
- var stdin_stream = getStdIn().inStream();
- return readLineSliceFrom(&stdin_stream.stream, slice);
-}
-
-/// Reads all characters until the next newline into slice, and returns
-/// a slice of the characters read (excluding the newline character(s)).
-pub fn readLineSliceFrom(stream: var, slice: []u8) ![]u8 {
- // We cannot use Buffer.fromOwnedSlice, as it wants to append a null byte
- // after taking ownership, which would always require an allocation.
- var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(testing.failing_allocator, slice) };
- try buf.resize(0);
- return try readLineFrom(stream, &buf);
-}
-
-test "io.readLineSliceFrom" {
- var buf: [7]u8 = undefined;
- var mem_stream = SliceInStream.init(
- \\Line 1
- \\Line 22
- \\Line 333
- );
- const stream = &mem_stream.stream;
-
- testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]));
- testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..]));
-}
-
pub const Packing = enum {
/// Pack data to byte alignment
Byte,
diff --git a/src/ir.cpp b/src/ir.cpp
@@ -26677,9 +26677,19 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
return_type, nullptr, true, true);
- if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
- return result_loc;
+
+ if (result_loc != nullptr) {
+ if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
+ return result_loc;
+ }
+ IrInstGen *dummy_value = ir_const(ira, &instruction->base.base, return_type);
+ dummy_value->value->special = ConstValSpecialRuntime;
+ IrInstGen *dummy_result = ir_implicit_cast2(ira, &instruction->base.base,
+ dummy_value, result_loc->value->type->data.pointer.child_type);
+ if (type_is_invalid(dummy_result->value->type))
+ return ira->codegen->invalid_inst_gen;
}
+
return ir_build_slice_gen(ira, &instruction->base.base, return_type,
ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc);
}
diff --git a/src/ir_print.cpp b/src/ir_print.cpp
@@ -590,11 +590,6 @@ static void ir_print_const_value(CodeGen *g, FILE *f, ZigValue *const_val) {
static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {
if (inst == nullptr) {
fprintf(irp->f, "(null)");
- return;
- }
-
- if (inst->value->special != ConstValSpecialRuntime) {
- ir_print_const_value(irp->codegen, irp->f, inst->value);
} else {
ir_print_var_gen(irp, inst);
}
diff --git a/src/stage2.cpp b/src/stage2.cpp
@@ -103,6 +103,7 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
target->builtin_str = "Target.Cpu.baseline(arch);\n";
target->cache_hash = "native\n\n";
} else if (strcmp(mcpu, "baseline") == 0) {
+ target->is_native = false;
target->llvm_cpu_name = "";
target->llvm_cpu_features = "";
target->builtin_str = "Target.Cpu.baseline(arch);\n";
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
@@ -3,6 +3,18 @@ const builtin = @import("builtin");
const Target = @import("std").Target;
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.addTest("slice to pointer conversion mismatch",
+ \\pub fn bytesAsSlice(bytes: var) [*]align(1) const u16 {
+ \\ return @ptrCast([*]align(1) const u16, bytes.ptr)[0..1];
+ \\}
+ \\test "bytesAsSlice" {
+ \\ const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
+ \\ const slice = bytesAsSlice(bytes[0..]);
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:2:54: error: expected type '[*]align(1) const u16', found '[]align(1) const u16'",
+ });
+
cases.addTest("access invalid @typeInfo decl",
\\const A = B;
\\test "Crash" {
diff --git a/test/standalone/guess_number/main.zig b/test/standalone/guess_number/main.zig
@@ -5,6 +5,7 @@ const fmt = std.fmt;
pub fn main() !void {
const stdout = &io.getStdOut().outStream().stream;
+ const stdin = io.getStdIn();
try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{});
@@ -22,13 +23,12 @@ pub fn main() !void {
try stdout.print("\nGuess a number between 1 and 100: ", .{});
var line_buf: [20]u8 = undefined;
- const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) {
- error.OutOfMemory => {
- try stdout.print("Input too long.\n", .{});
- continue;
- },
- else => return err,
- };
+ const amt = try stdin.read(&line_buf);
+ if (amt == line_buf.len) {
+ try stdout.print("Input too long.\n", .{});
+ continue;
+ }
+ const line = std.mem.trimRight(u8, line_buf[0..amt], "\r\n");
const guess = fmt.parseUnsigned(u8, line, 10) catch {
try stdout.print("Invalid number.\n", .{});
diff --git a/test/tests.zig b/test/tests.zig
@@ -650,8 +650,10 @@ pub const StackTracesContext = struct {
const got: []const u8 = got_result: {
var buf = try Buffer.initSize(b.allocator, 0);
defer buf.deinit();
- var bytes = stderr.toSliceConst();
- if (bytes.len != 0 and bytes[bytes.len - 1] == '\n') bytes = bytes[0 .. bytes.len - 1];
+ const bytes = if (stderr.endsWith("\n"))
+ stderr.toSliceConst()[0 .. stderr.len() - 1]
+ else
+ stderr.toSliceConst()[0..stderr.len()];
var it = mem.separate(bytes, "\n");
process_lines: while (it.next()) |line| {
if (line.len == 0) continue;