From 61f5ea4c9adc96dbdabca533f77d475233089b1c Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 15:33:32 +0300 Subject: Sema: add error note for wrong pointer dereference syntax Closes #1897 --- src/Sema.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Sema.zig b/src/Sema.zig index a73c1eedcb..859a24ad64 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -16612,7 +16612,17 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air const bitoffset_src: LazySrcLoc = .{ .node_offset_ptr_bitoffset = extra.data.src_node }; const hostsize_src: LazySrcLoc = .{ .node_offset_ptr_hostsize = extra.data.src_node }; - const unresolved_elem_ty = try sema.resolveType(block, elem_ty_src, extra.data.elem_type); + const unresolved_elem_ty = blk: { + const air_inst = try sema.resolveInst(extra.data.elem_type); + const ty = sema.analyzeAsType(block, elem_ty_src, air_inst) catch |err| { + if (err == error.AnalysisFail and sema.err != null and sema.typeOf(air_inst).isSinglePointer()) { + try sema.errNote(block, elem_ty_src, sema.err.?, "use '.*' to dereference pointer", .{}); + } + return err; + }; + if (ty.tag() == .generic_poison) return error.GenericPoison; + break :blk ty; + }; const target = sema.mod.getTarget(); var extra_i = extra.end; -- cgit v1.2.3 From 1ea1228036b6d3424c46a3ce66f4b7cbf461fc21 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 15:59:04 +0300 Subject: Sema: fix floatToInt to zero bit ints Closes #9415 --- src/Sema.zig | 7 +++++++ test/behavior/cast.zig | 8 ++++++++ 2 files changed, 15 insertions(+) (limited to 'src') diff --git a/src/Sema.zig b/src/Sema.zig index 859a24ad64..954eaddab9 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18668,6 +18668,13 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! } try sema.requireRuntimeBlock(block, inst_data.src(), operand_src); + if (dest_ty.intInfo(sema.mod.getTarget()).bits == 0) { + if (block.wantSafety()) { + const ok = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_eq_optimized else .cmp_eq, operand, try sema.addConstant(operand_ty, Value.zero)); + try sema.addSafetyCheck(block, ok, .integer_part_out_of_bounds); + } + return sema.addConstant(dest_ty, Value.zero); + } const result = try block.addTyOp(if (block.float_mode == .Optimized) .float_to_int_optimized else .float_to_int, dest_ty, operand); if (block.wantSafety()) { const back = try block.addTyOp(.int_to_float, operand_ty, result); diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 8473abc3ef..a1764ccfb6 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -1451,3 +1451,11 @@ test "peer type resolution of const and non-const pointer to array" { try std.testing.expect(@TypeOf(a, b) == *const [1024]u8); try std.testing.expect(a == b); } + +test "floatToInt to zero-bit int" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + var a: f32 = 0.0; + comptime try std.testing.expect(@floatToInt(u0, a) == 0); +} -- cgit v1.2.3 From d7314555f2bc413494d58bbafa2d607b88922afb Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 16:37:02 +0300 Subject: Sema: improve compile error for casting double pointer to anyopaque pointer Closes #12042 --- lib/std/meta.zig | 6 ++-- lib/std/start_windows_tls.zig | 2 +- src/Sema.zig | 35 ++++++++++++++++++---- src/type.zig | 3 -- ...t_implicit_cast_double_pointer_to_anyopaque.zig | 14 --------- .../double_pointer_to_anyopaque_pointer.zig | 28 +++++++++++++++++ 6 files changed, 62 insertions(+), 26 deletions(-) delete mode 100644 test/cases/compile_errors/dont_implicit_cast_double_pointer_to_anyopaque.zig create mode 100644 test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig (limited to 'src') diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 67de0cd63d..9daea60e62 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -302,7 +302,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .Array = .{ .len = array_info.len, .child = array_info.child, - .sentinel = &sentinel_val, + .sentinel = @ptrCast(?*const anyopaque, &sentinel_val), }, }), .is_allowzero = info.is_allowzero, @@ -320,7 +320,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .address_space = info.address_space, .child = info.child, .is_allowzero = info.is_allowzero, - .sentinel = &sentinel_val, + .sentinel = @ptrCast(?*const anyopaque, &sentinel_val), }, }), else => {}, @@ -338,7 +338,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { .address_space = ptr_info.address_space, .child = ptr_info.child, .is_allowzero = ptr_info.is_allowzero, - .sentinel = &sentinel_val, + .sentinel = @ptrCast(?*const anyopaque, &sentinel_val), }, }), }, diff --git a/lib/std/start_windows_tls.zig b/lib/std/start_windows_tls.zig index 7c9930fe6b..df25a903a2 100644 --- a/lib/std/start_windows_tls.zig +++ b/lib/std/start_windows_tls.zig @@ -42,7 +42,7 @@ export const _tls_used linksection(".rdata$T") = IMAGE_TLS_DIRECTORY{ .StartAddressOfRawData = &_tls_start, .EndAddressOfRawData = &_tls_end, .AddressOfIndex = &_tls_index, - .AddressOfCallBacks = &__xl_a, + .AddressOfCallBacks = @ptrCast(*anyopaque, &__xl_a), .SizeOfZeroFill = 0, .Characteristics = 0, }; diff --git a/src/Sema.zig b/src/Sema.zig index 954eaddab9..dd0f0ab50a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -23928,9 +23928,20 @@ fn coerceExtra( // cast from ?*T and ?[*]T to ?*anyopaque // but don't do it if the source type is a double pointer if (dest_ty.isPtrLikeOptional() and dest_ty.elemType2().tag() == .anyopaque and - inst_ty.isPtrLikeOptional() and inst_ty.elemType2().zigTypeTag() != .Pointer) - { + inst_ty.isPtrAtRuntime()) + anyopaque_check: { if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :optional; + const elem_ty = inst_ty.elemType2(); + if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) { + in_memory_result = .{ .double_ptr_to_anyopaque = .{ + .actual = inst_ty, + .wanted = dest_ty, + } }; + break :optional; + } + // Let the logic below handle wrapping the optional now that + // it has been checked to correctly coerce. + if (!inst_ty.isPtrLikeOptional()) break: anyopaque_check; return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src); } @@ -24053,9 +24064,16 @@ fn coerceExtra( // cast from *T and [*]T to *anyopaque // but don't do it if the source type is a double pointer - if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer and - inst_ty.childType().zigTypeTag() != .Pointer and sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) - { + if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer) { + if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :pointer; + const elem_ty = inst_ty.elemType2(); + if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) { + in_memory_result = .{ .double_ptr_to_anyopaque = .{ + .actual = inst_ty, + .wanted = dest_ty, + } }; + break :pointer; + } return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src); } @@ -24537,6 +24555,7 @@ const InMemoryCoercionResult = union(enum) { ptr_allowzero: Pair, ptr_bit_range: BitRange, ptr_alignment: IntPair, + double_ptr_to_anyopaque: Pair, const Pair = struct { actual: Type, @@ -24829,6 +24848,12 @@ const InMemoryCoercionResult = union(enum) { }); break; }, + .double_ptr_to_anyopaque => |pair| { + try sema.errNote(block, src, msg, "cannot implicitly cast double pointer '{}' to anyopaque pointer '{}'", .{ + pair.actual.fmt(sema.mod), pair.wanted.fmt(sema.mod), + }); + break; + }, }; } }; diff --git a/src/type.zig b/src/type.zig index b4015427c8..0fc580000a 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3941,10 +3941,7 @@ pub const Type = extern union { .optional => { var buf: Payload.ElemType = undefined; const child_type = self.optionalChild(&buf); - // optionals of zero sized pointers behave like bools - if (!child_type.hasRuntimeBits()) return false; if (child_type.zigTypeTag() != .Pointer) return false; - const info = child_type.ptrInfo().data; switch (info.size) { .Slice, .C => return false, diff --git a/test/cases/compile_errors/dont_implicit_cast_double_pointer_to_anyopaque.zig b/test/cases/compile_errors/dont_implicit_cast_double_pointer_to_anyopaque.zig deleted file mode 100644 index 44ae18ae8a..0000000000 --- a/test/cases/compile_errors/dont_implicit_cast_double_pointer_to_anyopaque.zig +++ /dev/null @@ -1,14 +0,0 @@ -export fn entry() void { - var a: u32 = 1; - var ptr: *align(@alignOf(u32)) anyopaque = &a; - var b: *u32 = @ptrCast(*u32, ptr); - var ptr2: *anyopaque = &b; - _ = ptr2; -} - -// error -// backend=stage2 -// target=native -// -// :5:28: error: expected type '*anyopaque', found '**u32' -// :5:28: note: pointer type child '*u32' cannot cast into pointer type child 'anyopaque' diff --git a/test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig b/test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig new file mode 100644 index 0000000000..6aa9618dbd --- /dev/null +++ b/test/cases/compile_errors/double_pointer_to_anyopaque_pointer.zig @@ -0,0 +1,28 @@ +pub export fn entry1() void { + const x: usize = 5; + + const ptr: *const anyopaque = &(&x); + _ = ptr; +} +pub export fn entry2() void { + var val: [*:0]u8 = undefined; + func(&val); +} +fn func(_: ?*anyopaque) void {} +pub export fn entry3() void { + var x: *?*usize = undefined; + + const ptr: *const anyopaque = x; + _ = ptr; +} + +// error +// backend=stage2 +// target=native +// +// :4:35: error: expected type '*const anyopaque', found '*const *const usize' +// :4:35: note: cannot implicitly cast double pointer '*const *const usize' to anyopaque pointer '*const anyopaque' +// :9:10: error: expected type '?*anyopaque', found '*[*:0]u8' +// :9:10: note: cannot implicitly cast double pointer '*[*:0]u8' to anyopaque pointer '?*anyopaque' +// :15:35: error: expected type '*const anyopaque', found '*?*usize' +// :15:35: note: cannot implicitly cast double pointer '*?*usize' to anyopaque pointer '*const anyopaque' -- cgit v1.2.3 From 5321afcf9c633c5dc0da5148981dfdbd61606f1a Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 17:11:03 +0300 Subject: stage2: make switch on corrupt value panic point to switch condition Closes #13295 --- src/AstGen.zig | 5 +++++ src/Sema.zig | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/AstGen.zig b/src/AstGen.zig index 48e6a480f3..91dbcf7d67 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -6496,9 +6496,14 @@ fn switchExpr( } const operand_ri: ResultInfo = .{ .rl = if (any_payload_is_ref) .ref else .none }; + astgen.advanceSourceCursorToNode(operand_node); + const operand_line = astgen.source_line - parent_gz.decl_line; + const operand_column = astgen.source_column; const raw_operand = try expr(parent_gz, scope, operand_ri, operand_node); const cond_tag: Zir.Inst.Tag = if (any_payload_is_ref) .switch_cond_ref else .switch_cond; const cond = try parent_gz.addUnNode(cond_tag, raw_operand, operand_node); + // Sema expects a dbg_stmt immediately after switch_cond(_ref) + try emitDbgStmt(parent_gz, operand_line, operand_column); // We need the type of the operand to use as the result location for all the prong items. const cond_ty_inst = try parent_gz.addUnNode(.typeof, cond, operand_node); const item_ri: ResultInfo = .{ .rl = .{ .ty = cond_ty_inst } }; diff --git a/src/Sema.zig b/src/Sema.zig index dd0f0ab50a..7be0336d8f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -9663,6 +9663,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError const extra = sema.code.extraData(Zir.Inst.SwitchBlock, inst_data.payload_index); const operand = try sema.resolveInst(extra.data.operand); + // AstGen guarantees that the instruction immediately following + // switch_cond(_ref) is a dbg_stmt + const cond_dbg_node_index = Zir.refToIndex(extra.data.operand).? + 1; var header_extra_index: usize = extra.end; @@ -10359,6 +10362,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError if (backend_supports_is_named_enum and block.wantSafety() and operand_ty.zigTypeTag() == .Enum and (!operand_ty.isNonexhaustiveEnum() or union_originally)) { + try sema.zirDbgStmt(block, cond_dbg_node_index); const ok = try block.addUnOp(.is_named_enum_value, operand); try sema.addSafetyCheck(block, ok, .corrupt_switch); } @@ -10828,6 +10832,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError if (backend_supports_is_named_enum and special.body.len != 0 and block.wantSafety() and operand_ty.zigTypeTag() == .Enum and (!operand_ty.isNonexhaustiveEnum() or union_originally)) { + try sema.zirDbgStmt(&case_block, cond_dbg_node_index); const ok = try case_block.addUnOp(.is_named_enum_value, operand); try sema.addSafetyCheck(&case_block, ok, .corrupt_switch); } @@ -10851,6 +10856,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError // We still need a terminator in this block, but we have proven // that it is unreachable. if (case_block.wantSafety()) { + try sema.zirDbgStmt(&case_block, cond_dbg_node_index); _ = try sema.safetyPanic(&case_block, src, .corrupt_switch); } else { _ = try case_block.addNoOp(.unreach); @@ -23931,7 +23937,7 @@ fn coerceExtra( inst_ty.isPtrAtRuntime()) anyopaque_check: { if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :optional; - const elem_ty = inst_ty.elemType2(); + const elem_ty = inst_ty.elemType2(); if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) { in_memory_result = .{ .double_ptr_to_anyopaque = .{ .actual = inst_ty, @@ -23941,7 +23947,7 @@ fn coerceExtra( } // Let the logic below handle wrapping the optional now that // it has been checked to correctly coerce. - if (!inst_ty.isPtrLikeOptional()) break: anyopaque_check; + if (!inst_ty.isPtrLikeOptional()) break :anyopaque_check; return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src); } @@ -24066,7 +24072,7 @@ fn coerceExtra( // but don't do it if the source type is a double pointer if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer) { if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :pointer; - const elem_ty = inst_ty.elemType2(); + const elem_ty = inst_ty.elemType2(); if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) { in_memory_result = .{ .double_ptr_to_anyopaque = .{ .actual = inst_ty, -- cgit v1.2.3 From 7705a4e8651a493590eed5a79d5361bc6b54985e Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 29 Oct 2022 15:18:07 +0300 Subject: Sema: wrap optionals in `zirPtrCast` when needed --- src/Sema.zig | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/Sema.zig b/src/Sema.zig index 7be0336d8f..5aa3c8b700 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18947,6 +18947,9 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air if (!dest_ty.ptrAllowsZero() and operand_val.isNull()) { return sema.fail(block, operand_src, "null pointer casted to type {}", .{dest_ty.fmt(sema.mod)}); } + if (dest_ty.zigTypeTag() == .Optional and sema.typeOf(ptr).zigTypeTag() != .Optional) { + return sema.addConstant(dest_ty, try Value.Tag.opt_payload.create(sema.arena, operand_val)); + } return sema.addConstant(aligned_dest_ty, operand_val); } -- cgit v1.2.3