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

master
Andrew Kelley 2020-02-20 18:36:04 -05:00
commit 903127f36c
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
10 changed files with 63 additions and 83 deletions

View File

@ -616,6 +616,7 @@ endif()
set(BUILD_LIBSTAGE2_ARGS "build-lib" set(BUILD_LIBSTAGE2_ARGS "build-lib"
"src-self-hosted/stage2.zig" "src-self-hosted/stage2.zig"
-mcpu=baseline
--name zigstage2 --name zigstage2
--override-lib-dir "${CMAKE_SOURCE_DIR}/lib" --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
--cache on --cache on

View File

@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
self.len += items.len; 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 /// Adjust the list's length to `new_len`. Doesn't initialize
/// added items if any. /// added items if any.
pub fn resize(self: *Self, new_len: usize) !void { pub fn resize(self: *Self, new_len: usize) !void {
@ -311,6 +319,23 @@ test "std.ArrayList.basic" {
testing.expect(list.pop() == 33); 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" { test "std.ArrayList.orderedRemove" {
var list = ArrayList(i32).init(testing.allocator); var list = ArrayList(i32).init(testing.allocator);
defer list.deinit(); defer list.deinit();

View File

@ -864,6 +864,7 @@ pub const Dir = struct {
.OBJECT_NAME_INVALID => unreachable, .OBJECT_NAME_INVALID => unreachable,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound, .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound, .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
.NO_MEDIA_IN_DEVICE => return error.NoDevice,
.INVALID_PARAMETER => unreachable, .INVALID_PARAMETER => unreachable,
.SHARING_VIOLATION => return error.SharingViolation, .SHARING_VIOLATION => return error.SharingViolation,
.ACCESS_DENIED => return error.AccessDenied, .ACCESS_DENIED => return error.AccessDenied,

View File

@ -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 { pub const Packing = enum {
/// Pack data to byte alignment /// Pack data to byte alignment
Byte, Byte,

View File

@ -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, IrInstGen *result_loc = ir_resolve_result(ira, &instruction->base.base, instruction->result_loc,
return_type, nullptr, true, true); 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, return ir_build_slice_gen(ira, &instruction->base.base, return_type,
ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc); ptr_ptr, casted_start, end, instruction->safety_check_on, result_loc);
} }

View File

@ -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) { static void ir_print_other_inst_gen(IrPrintGen *irp, IrInstGen *inst) {
if (inst == nullptr) { if (inst == nullptr) {
fprintf(irp->f, "(null)"); fprintf(irp->f, "(null)");
return;
}
if (inst->value->special != ConstValSpecialRuntime) {
ir_print_const_value(irp->codegen, irp->f, inst->value);
} else { } else {
ir_print_var_gen(irp, inst); ir_print_var_gen(irp, inst);
} }

View File

@ -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->builtin_str = "Target.Cpu.baseline(arch);\n";
target->cache_hash = "native\n\n"; target->cache_hash = "native\n\n";
} else if (strcmp(mcpu, "baseline") == 0) { } else if (strcmp(mcpu, "baseline") == 0) {
target->is_native = false;
target->llvm_cpu_name = ""; target->llvm_cpu_name = "";
target->llvm_cpu_features = ""; target->llvm_cpu_features = "";
target->builtin_str = "Target.Cpu.baseline(arch);\n"; target->builtin_str = "Target.Cpu.baseline(arch);\n";

View File

@ -3,6 +3,18 @@ const builtin = @import("builtin");
const Target = @import("std").Target; const Target = @import("std").Target;
pub fn addCases(cases: *tests.CompileErrorContext) void { 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", cases.addTest("access invalid @typeInfo decl",
\\const A = B; \\const A = B;
\\test "Crash" { \\test "Crash" {

View File

@ -5,6 +5,7 @@ const fmt = std.fmt;
pub fn main() !void { pub fn main() !void {
const stdout = &io.getStdOut().outStream().stream; const stdout = &io.getStdOut().outStream().stream;
const stdin = io.getStdIn();
try stdout.print("Welcome to the Guess Number Game in Zig.\n", .{}); 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: ", .{}); try stdout.print("\nGuess a number between 1 and 100: ", .{});
var line_buf: [20]u8 = undefined; var line_buf: [20]u8 = undefined;
const line = io.readLineSlice(line_buf[0..]) catch |err| switch (err) { const amt = try stdin.read(&line_buf);
error.OutOfMemory => { if (amt == line_buf.len) {
try stdout.print("Input too long.\n", .{}); try stdout.print("Input too long.\n", .{});
continue; continue;
}, }
else => return err, const line = std.mem.trimRight(u8, line_buf[0..amt], "\r\n");
};
const guess = fmt.parseUnsigned(u8, line, 10) catch { const guess = fmt.parseUnsigned(u8, line, 10) catch {
try stdout.print("Invalid number.\n", .{}); try stdout.print("Invalid number.\n", .{});

View File

@ -650,8 +650,10 @@ pub const StackTracesContext = struct {
const got: []const u8 = got_result: { const got: []const u8 = got_result: {
var buf = try Buffer.initSize(b.allocator, 0); var buf = try Buffer.initSize(b.allocator, 0);
defer buf.deinit(); defer buf.deinit();
var bytes = stderr.toSliceConst(); const bytes = if (stderr.endsWith("\n"))
if (bytes.len != 0 and bytes[bytes.len - 1] == '\n') bytes = bytes[0 .. bytes.len - 1]; stderr.toSliceConst()[0 .. stderr.len() - 1]
else
stderr.toSliceConst()[0..stderr.len()];
var it = mem.separate(bytes, "\n"); var it = mem.separate(bytes, "\n");
process_lines: while (it.next()) |line| { process_lines: while (it.next()) |line| {
if (line.len == 0) continue; if (line.len == 0) continue;