From a3855477866bbe59b0583ca07170a981e7ca5839 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 20 Feb 2020 13:14:19 +0100 Subject: [PATCH 1/3] ir: Compile error on result_loc type mismatch w/ slicing Closes #4508 --- src/ir.cpp | 14 ++++++++++++-- test/compile_errors.zig | 12 ++++++++++++ test/tests.zig | 6 ++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 3044d54d17..5f50586f72 100644 --- 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/test/compile_errors.zig b/test/compile_errors.zig index 9f2b3716b0..1b32786e40 100644 --- 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/tests.zig b/test/tests.zig index f079b4664c..64a46abbc2 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -677,8 +677,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; From ae16a6773fd7853a3589b5650a33b4b8d37b9ef8 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 20 Feb 2020 13:25:45 +0100 Subject: [PATCH 2/3] std: Handle NO_MEDIA_IN_DEVICE error in openFileWindows Closes #4507 --- lib/std/fs.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index a00ba6a63f..3bb89f5d7e 100644 --- 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.FileNotFound, .INVALID_PARAMETER => unreachable, .SHARING_VIOLATION => return error.SharingViolation, .ACCESS_DENIED => return error.AccessDenied, From ec889d5888c57d0337a1e00398d71241a9716ebe Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 20 Feb 2020 12:39:46 -0500 Subject: [PATCH 3/3] NO_MEDIA_IN_DEVICE => return error.NoDevice --- lib/std/fs.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 3bb89f5d7e..c3d418eb8c 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -864,7 +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.FileNotFound, + .NO_MEDIA_IN_DEVICE => return error.NoDevice, .INVALID_PARAMETER => unreachable, .SHARING_VIOLATION => return error.SharingViolation, .ACCESS_DENIED => return error.AccessDenied,