From 61f5ea4c9adc96dbdabca533f77d475233089b1c Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 15:33:32 +0300 Subject: [PATCH 1/8] Sema: add error note for wrong pointer dereference syntax Closes #1897 --- src/Sema.zig | 12 +++++++++++- .../incorrect_pointer_dereference_syntax.zig | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/cases/compile_errors/incorrect_pointer_dereference_syntax.zig 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; diff --git a/test/cases/compile_errors/incorrect_pointer_dereference_syntax.zig b/test/cases/compile_errors/incorrect_pointer_dereference_syntax.zig new file mode 100644 index 0000000000..33b5d1f3b6 --- /dev/null +++ b/test/cases/compile_errors/incorrect_pointer_dereference_syntax.zig @@ -0,0 +1,11 @@ +pub export fn entry() void { + var a: *u32 = undefined; + _ = *a; +} + +// error +// backend=stage2 +// target=native +// +// :3:10: error: expected type 'type', found '*u32' +// :3:10: note: use '.*' to dereference pointer From 1ea1228036b6d3424c46a3ce66f4b7cbf461fc21 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 15:59:04 +0300 Subject: [PATCH 2/8] Sema: fix floatToInt to zero bit ints Closes #9415 --- src/Sema.zig | 7 +++++++ test/behavior/cast.zig | 8 ++++++++ 2 files changed, 15 insertions(+) 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); +} From 9607bd90e6927eea0fc7d57e042d03657afbf70d Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 16:09:24 +0300 Subject: [PATCH 3/8] parser: improve error message for missing var/const before local variable Closes #12721 --- lib/std/zig/Ast.zig | 6 +++++- lib/std/zig/parse.zig | 13 ++++++++++++- lib/std/zig/parser_test.zig | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig index 62a567387f..62eea275a8 100644 --- a/lib/std/zig/Ast.zig +++ b/lib/std/zig/Ast.zig @@ -197,7 +197,7 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void { }); }, .expected_labelable => { - return stream.print("expected 'while', 'for', 'inline', 'suspend', or '{{', found '{s}'", .{ + return stream.print("expected 'while', 'for', 'inline', or '{{', found '{s}'", .{ token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)].symbol(), }); }, @@ -356,6 +356,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void { .next_field => { return stream.writeAll("field after declarations here"); }, + .expected_var_const => { + return stream.writeAll("expected 'var' or 'const' before variable declaration"); + }, .expected_token => { const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)]; @@ -2579,6 +2582,7 @@ pub const Error = struct { mismatched_binary_op_whitespace, invalid_ampersand_ampersand, c_style_container, + expected_var_const, zig_style_container, previous_field, diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index db56cef21e..6429372d74 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -1118,7 +1118,18 @@ const Parser = struct { if (loop_stmt != 0) return loop_stmt; if (label_token != 0) { - return p.fail(.expected_labelable); + const after_colon = p.tok_i; + const node = try p.parseTypeExpr(); + if (node != 0) { + const a = try p.parseByteAlign(); + const b = try p.parseAddrSpace(); + const c = try p.parseLinkSection(); + const d = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); + if (a != 0 or b != 0 or c != 0 or d != 0) { + return p.failMsg(.{ .tag = .expected_var_const, .token = label_token }); + } + } + return p.failMsg(.{ .tag = .expected_labelable, .token = after_colon }); } return null_node; diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 4e155df6d8..06acd84092 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -5145,6 +5145,32 @@ test "zig fmt: make single-line if no trailing comma" { ); } +test "zig fmt: missing const/var before local variable" { + try testError( + \\comptime { + \\ z: u32; + \\} + \\comptime { + \\ z: u32 align(1); + \\} + \\comptime { + \\ z: u32 addrspace(.generic); + \\} + \\comptime { + \\ z: u32 linksection("foo"); + \\} + \\comptime { + \\ z: u32 = 1; + \\} + , &.{ + .expected_labelable, + .expected_var_const, + .expected_var_const, + .expected_var_const, + .expected_var_const, + }); +} + test "zig fmt: while continue expr" { try testCanonical( \\test { From d7314555f2bc413494d58bbafa2d607b88922afb Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 16:37:02 +0300 Subject: [PATCH 4/8] 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 -- ...licit_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 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' From 5321afcf9c633c5dc0da5148981dfdbd61606f1a Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 17:11:03 +0300 Subject: [PATCH 5/8] 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(-) 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, From 278c32976e93f4900c634913fb2bb06f6a7ecf87 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 28 Oct 2022 21:01:12 +0300 Subject: [PATCH 6/8] parser: add helpful error for extra = in variable initializer Closes #12768 --- lib/std/zig/Ast.zig | 4 ++++ lib/std/zig/parse.zig | 13 ++++++++++++- lib/std/zig/parser_test.zig | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig index 62eea275a8..5dd0cdd5af 100644 --- a/lib/std/zig/Ast.zig +++ b/lib/std/zig/Ast.zig @@ -359,6 +359,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void { .expected_var_const => { return stream.writeAll("expected 'var' or 'const' before variable declaration"); }, + .wrong_equal_var_decl => { + return stream.writeAll("variable initialized with '==' instead of '='"); + }, .expected_token => { const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)]; @@ -2583,6 +2586,7 @@ pub const Error = struct { invalid_ampersand_ampersand, c_style_container, expected_var_const, + wrong_equal_var_decl, zig_style_container, previous_field, diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 6429372d74..1be074fc27 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -812,7 +812,18 @@ const Parser = struct { const align_node = try p.parseByteAlign(); const addrspace_node = try p.parseAddrSpace(); const section_node = try p.parseLinkSection(); - const init_node: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr(); + const init_node: Node.Index = switch (p.token_tags[p.tok_i]) { + .equal_equal => blk: { + try p.warn(.wrong_equal_var_decl); + p.tok_i += 1; + break :blk try p.expectExpr(); + }, + .equal => blk: { + p.tok_i += 1; + break :blk try p.expectExpr(); + }, + else => 0, + }; if (section_node == 0 and addrspace_node == 0) { if (align_node == 0) { return p.addNode(.{ diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 06acd84092..4ecc0d283d 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -5145,6 +5145,14 @@ test "zig fmt: make single-line if no trailing comma" { ); } +test "zig fmt: variable initialized with ==" { + try testError( + \\comptime { + \\ var z: u32 == 12 + 1; + \\} + , &.{.wrong_equal_var_decl}); +} + test "zig fmt: missing const/var before local variable" { try testError( \\comptime { From 7705a4e8651a493590eed5a79d5361bc6b54985e Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 29 Oct 2022 15:18:07 +0300 Subject: [PATCH 7/8] Sema: wrap optionals in `zirPtrCast` when needed --- src/Sema.zig | 3 +++ 1 file changed, 3 insertions(+) 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); } From 577daab08cd2119604b5ee501e4bd8aeb2619668 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 3 Nov 2022 22:11:25 +0200 Subject: [PATCH 8/8] CI: windows: update tarball --- ci/azure/pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/azure/pipelines.yml b/ci/azure/pipelines.yml index b0fbf0fb72..69ce774cff 100644 --- a/ci/azure/pipelines.yml +++ b/ci/azure/pipelines.yml @@ -16,7 +16,7 @@ jobs: vmImage: 'windows-2019' variables: TARGET: 'x86_64-windows-gnu' - ZIG_LLVM_CLANG_LLD_NAME: 'zig+llvm+lld+clang-${{ variables.TARGET }}-0.10.0-dev.4560+828735ac0' + ZIG_LLVM_CLANG_LLD_NAME: 'zig+llvm+lld+clang-${{ variables.TARGET }}-0.11.0-dev.25+499dddb4c' ZIG_LLVM_CLANG_LLD_URL: 'https://ziglang.org/deps/${{ variables.ZIG_LLVM_CLANG_LLD_NAME }}.zip' steps: - pwsh: |