authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-17 21:32:13-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-17 21:32:13-05:00
log7f604b6f48905faf0bbe1ad795b53aeb11332063
tree578b321a86fad4e0a4f016f48df5753800a1cc9f
parent73827d2ea9a1275d2351b80fc357896793e7c9c6
parent841b38aae8913972d12b08cf3e0e497be305efb1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14333 from Vexu/fixes

Misc fixes

11 files changed, 150 insertions(+), 27 deletions(-)

lib/std/zig/tokenizer.zig+10-1
......@@ -1151,7 +1151,13 @@ pub const Tokenizer = struct {
11511151 },
11521152 },
11531153 .line_comment => switch (c) {
1154 0 => break,
1154 0 => {
1155 if (self.index != self.buffer.len) {
1156 result.tag = .invalid;
1157 self.index += 1;
1158 }
1159 break;
1160 },
11551161 '\n' => {
11561162 state = .start;
11571163 result.loc.start = self.index + 1;
......@@ -1865,6 +1871,9 @@ test "null byte before eof" {
18651871 try testTokenize("//\x00", &.{.invalid});
18661872 try testTokenize("\\\\\x00", &.{ .multiline_string_literal_line, .invalid });
18671873 try testTokenize("\x00", &.{.invalid});
1874 try testTokenize("// NUL\x00\n", &.{.invalid});
1875 try testTokenize("///\x00\n", &.{ .doc_comment, .invalid });
1876 try testTokenize("/// NUL\x00\n", &.{ .doc_comment, .invalid });
18681877}
18691878
18701879fn testTokenize(source: [:0]const u8, expected_token_tags: []const Token.Tag) !void {
src/AstGen.zig+21-4
......@@ -3341,6 +3341,9 @@ fn ptrType(
33413341 return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});
33423342 }
33433343
3344 const source_offset = gz.astgen.source_offset;
3345 const source_line = gz.astgen.source_line;
3346 const source_column = gz.astgen.source_column;
33443347 const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);
33453348
33463349 var sentinel_ref: Zir.Inst.Ref = .none;
......@@ -3351,17 +3354,31 @@ fn ptrType(
33513354 var trailing_count: u32 = 0;
33523355
33533356 if (ptr_info.ast.sentinel != 0) {
3357 // These attributes can appear in any order and they all come before the
3358 // element type so we need to reset the source cursor before generating them.
3359 gz.astgen.source_offset = source_offset;
3360 gz.astgen.source_line = source_line;
3361 gz.astgen.source_column = source_column;
3362
33543363 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);
33553364 trailing_count += 1;
33563365 }
3357 if (ptr_info.ast.align_node != 0) {
3358 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);
3359 trailing_count += 1;
3360 }
33613366 if (ptr_info.ast.addrspace_node != 0) {
3367 gz.astgen.source_offset = source_offset;
3368 gz.astgen.source_line = source_line;
3369 gz.astgen.source_column = source_column;
3370
33623371 addrspace_ref = try expr(gz, scope, .{ .rl = .{ .ty = .address_space_type } }, ptr_info.ast.addrspace_node);
33633372 trailing_count += 1;
33643373 }
3374 if (ptr_info.ast.align_node != 0) {
3375 gz.astgen.source_offset = source_offset;
3376 gz.astgen.source_line = source_line;
3377 gz.astgen.source_column = source_column;
3378
3379 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);
3380 trailing_count += 1;
3381 }
33653382 if (ptr_info.ast.bit_range_start != 0) {
33663383 assert(ptr_info.ast.bit_range_end != 0);
33673384 bit_start_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start);
src/Sema.zig+22-3
......@@ -11288,6 +11288,7 @@ fn resolveSwitchItemVal(
1128811288 // Only if we know for sure we need to report a compile error do we resolve the
1128911289 // full source locations.
1129011290 if (sema.resolveConstValue(block, .unneeded, item, "")) |val| {
11291 try sema.resolveLazyValue(val);
1129111292 return TypedValue{ .ty = item_ty, .val = val };
1129211293 } else |err| switch (err) {
1129311294 error.NeededSourceLocation => {
......@@ -16258,6 +16259,7 @@ fn typeInfoNamespaceDecls(
1625816259 for (decls) |decl_index| {
1625916260 const decl = sema.mod.declPtr(decl_index);
1626016261 if (decl.kind == .@"usingnamespace") {
16262 if (decl.analysis == .in_progress) continue;
1626116263 try sema.mod.ensureDeclAnalyzed(decl_index);
1626216264 var buf: Value.ToTypeBuffer = undefined;
1626316265 const new_ns = decl.val.toType(&buf).getNamespace().?;
......@@ -24820,8 +24822,13 @@ fn coerceExtra(
2482024822 // empty tuple to zero-length slice
2482124823 // note that this allows coercing to a mutable slice.
2482224824 if (inst_child_ty.structFieldCount() == 0) {
24825 // Optional slice is represented with a null pointer so
24826 // we use a dummy pointer value with the required alignment.
2482324827 const slice_val = try Value.Tag.slice.create(sema.arena, .{
24824 .ptr = Value.undef,
24828 .ptr = if (dest_info.@"align" != 0)
24829 try Value.Tag.int_u64.create(sema.arena, dest_info.@"align")
24830 else
24831 try inst_child_ty.lazyAbiAlignment(target, sema.arena),
2482524832 .len = Value.zero,
2482624833 });
2482724834 return sema.addConstant(dest_ty, slice_val);
......@@ -26065,7 +26072,8 @@ fn coerceVarArgParam(
2606526072) !Air.Inst.Ref {
2606626073 if (block.is_typeof) return inst;
2606726074
26068 const coerced = switch (sema.typeOf(inst).zigTypeTag()) {
26075 const uncasted_ty = sema.typeOf(inst);
26076 const coerced = switch (uncasted_ty.zigTypeTag()) {
2606926077 // TODO consider casting to c_int/f64 if they fit
2607026078 .ComptimeInt, .ComptimeFloat => return sema.fail(
2607126079 block,
......@@ -26079,6 +26087,17 @@ fn coerceVarArgParam(
2607926087 break :blk try sema.analyzeDeclRef(fn_decl);
2608026088 },
2608126089 .Array => return sema.fail(block, inst_src, "arrays must be passed by reference to variadic function", .{}),
26090 .Float => float: {
26091 const target = sema.mod.getTarget();
26092 const double_bits = @import("type.zig").CType.sizeInBits(.double, target);
26093 const inst_bits = uncasted_ty.floatBits(sema.mod.getTarget());
26094 if (inst_bits >= double_bits) break :float inst;
26095 switch (double_bits) {
26096 32 => break :float try sema.coerce(block, Type.f32, inst, inst_src),
26097 64 => break :float try sema.coerce(block, Type.f64, inst, inst_src),
26098 else => unreachable,
26099 }
26100 },
2608226101 else => inst,
2608326102 };
2608426103
......@@ -27316,7 +27335,7 @@ fn coerceCompatiblePtrs(
2731627335 return sema.addConstant(dest_ty, val);
2731727336 }
2731827337 try sema.requireRuntimeBlock(block, inst_src, null);
27319 const inst_allows_zero = (inst_ty.zigTypeTag() == .Pointer and inst_ty.ptrAllowsZero()) or true;
27338 const inst_allows_zero = inst_ty.zigTypeTag() != .Pointer or inst_ty.ptrAllowsZero();
2732027339 if (block.wantSafety() and inst_allows_zero and !dest_ty.ptrAllowsZero() and
2732127340 try sema.typeHasRuntimeBits(dest_ty.elemType2()))
2732227341 {
src/codegen/llvm.zig+9-9
......@@ -3370,7 +3370,7 @@ pub const DeclGen = struct {
33703370 return llvm_int.constIntToPtr(try dg.lowerType(tv.ty));
33713371 },
33723372 .field_ptr, .opt_payload_ptr, .eu_payload_ptr, .elem_ptr => {
3373 return dg.lowerParentPtr(tv.val);
3373 return dg.lowerParentPtr(tv.val, tv.ty.ptrInfo().data.bit_offset % 8 == 0);
33743374 },
33753375 .null_value, .zero => {
33763376 const llvm_type = try dg.lowerType(tv.ty);
......@@ -3378,7 +3378,7 @@ pub const DeclGen = struct {
33783378 },
33793379 .opt_payload => {
33803380 const payload = tv.val.castTag(.opt_payload).?.data;
3381 return dg.lowerParentPtr(payload);
3381 return dg.lowerParentPtr(payload, tv.ty.ptrInfo().data.bit_offset % 8 == 0);
33823382 },
33833383 else => |tag| return dg.todo("implement const of pointer type '{}' ({})", .{
33843384 tv.ty.fmtDebug(), tag,
......@@ -3967,7 +3967,7 @@ pub const DeclGen = struct {
39673967 return try dg.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index);
39683968 }
39693969
3970 fn lowerParentPtr(dg: *DeclGen, ptr_val: Value) Error!*llvm.Value {
3970 fn lowerParentPtr(dg: *DeclGen, ptr_val: Value, byte_aligned: bool) Error!*llvm.Value {
39713971 const target = dg.module.getTarget();
39723972 switch (ptr_val.tag()) {
39733973 .decl_ref_mut => {
......@@ -3996,7 +3996,7 @@ pub const DeclGen = struct {
39963996 },
39973997 .field_ptr => {
39983998 const field_ptr = ptr_val.castTag(.field_ptr).?.data;
3999 const parent_llvm_ptr = try dg.lowerParentPtr(field_ptr.container_ptr);
3999 const parent_llvm_ptr = try dg.lowerParentPtr(field_ptr.container_ptr, byte_aligned);
40004000 const parent_ty = field_ptr.container_ty;
40014001
40024002 const field_index = @intCast(u32, field_ptr.field_index);
......@@ -4026,6 +4026,7 @@ pub const DeclGen = struct {
40264026 },
40274027 .Struct => {
40284028 if (parent_ty.containerLayout() == .Packed) {
4029 if (!byte_aligned) return parent_llvm_ptr;
40294030 const llvm_usize = dg.context.intType(target.cpu.arch.ptrBitWidth());
40304031 const base_addr = parent_llvm_ptr.constPtrToInt(llvm_usize);
40314032 // count bits of fields before this one
......@@ -4072,7 +4073,7 @@ pub const DeclGen = struct {
40724073 },
40734074 .elem_ptr => {
40744075 const elem_ptr = ptr_val.castTag(.elem_ptr).?.data;
4075 const parent_llvm_ptr = try dg.lowerParentPtr(elem_ptr.array_ptr);
4076 const parent_llvm_ptr = try dg.lowerParentPtr(elem_ptr.array_ptr, true);
40764077
40774078 const llvm_usize = try dg.lowerType(Type.usize);
40784079 const indices: [1]*llvm.Value = .{
......@@ -4083,7 +4084,7 @@ pub const DeclGen = struct {
40834084 },
40844085 .opt_payload_ptr => {
40854086 const opt_payload_ptr = ptr_val.castTag(.opt_payload_ptr).?.data;
4086 const parent_llvm_ptr = try dg.lowerParentPtr(opt_payload_ptr.container_ptr);
4087 const parent_llvm_ptr = try dg.lowerParentPtr(opt_payload_ptr.container_ptr, true);
40874088 var buf: Type.Payload.ElemType = undefined;
40884089
40894090 const payload_ty = opt_payload_ptr.container_ty.optionalChild(&buf);
......@@ -4105,7 +4106,7 @@ pub const DeclGen = struct {
41054106 },
41064107 .eu_payload_ptr => {
41074108 const eu_payload_ptr = ptr_val.castTag(.eu_payload_ptr).?.data;
4108 const parent_llvm_ptr = try dg.lowerParentPtr(eu_payload_ptr.container_ptr);
4109 const parent_llvm_ptr = try dg.lowerParentPtr(eu_payload_ptr.container_ptr, true);
41094110
41104111 const payload_ty = eu_payload_ptr.container_ty.errorUnionPayload();
41114112 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -10667,8 +10668,7 @@ const ParamTypeIterator = struct {
1066710668 .memory => {
1066810669 it.zig_index += 1;
1066910670 it.llvm_index += 1;
10670 it.byval_attr = true;
10671 return .byref;
10671 return .byref_mut;
1067210672 },
1067310673 .sse => {
1067410674 it.zig_index += 1;
test/behavior/packed-struct.zig+26
......@@ -599,3 +599,29 @@ test "packed struct initialized in bitcast" {
599599 const t = @bitCast(u8, T{ .val = val });
600600 try expect(t == val);
601601}
602
603test "pointer to container level packed struct field" {
604 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
605 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
606 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
607 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
608 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
609
610 const S = packed struct(u32) {
611 test_bit: bool,
612 someother_data: u12,
613 other_test_bit: bool,
614 someother_more_different_data: u12,
615 other_bits: packed struct(u6) {
616 enable_1: bool,
617 enable_2: bool,
618 enable_3: bool,
619 enable_4: bool,
620 enable_5: bool,
621 enable_6: bool,
622 },
623 var arr = [_]u32{0} ** 2;
624 };
625 @ptrCast(*S, &S.arr[0]).other_bits.enable_3 = true;
626 try expect(S.arr[0] == 0x10000000);
627}
test/behavior/pointers.zig+10
......@@ -522,3 +522,13 @@ test "ptrToInt on a generic function" {
522522 };
523523 try S.doTheTest(&S.generic);
524524}
525
526test "pointer alignment and element type include call expression" {
527 const S = struct {
528 fn T() type {
529 return struct { _: i32 };
530 }
531 const P = *align(@alignOf(T())) [@sizeOf(T())]u8;
532 };
533 try expect(@alignOf(S.P) > 0);
534}
test/behavior/switch.zig+14
......@@ -686,3 +686,17 @@ test "enum value without tag name used as switch item" {
686686 _ => return error.TestFailed,
687687 }
688688}
689
690test "switch item sizeof" {
691 const S = struct {
692 fn doTheTest() !void {
693 var a: usize = 0;
694 switch (a) {
695 @sizeOf(struct {}) => {},
696 else => return error.TestFailed,
697 }
698 }
699 };
700 try S.doTheTest();
701 comptime try S.doTheTest();
702}
test/behavior/type_info.zig+13
......@@ -590,3 +590,16 @@ test "@typeInfo decls and usingnamespace" {
590590 try expectEqualStrings(decls[1].name, "y");
591591 try expectEqualStrings(decls[2].name, "z");
592592}
593
594test "@typeInfo decls ignore dependency loops" {
595 const S = struct {
596 fn Def(comptime T: type) type {
597 std.debug.assert(@typeInfo(T).Struct.decls.len == 1);
598 return struct {
599 const foo = u32;
600 };
601 }
602 usingnamespace Def(@This());
603 };
604 _ = S.foo;
605}
test/c_abi/main.zig-1
......@@ -1032,7 +1032,6 @@ extern fn c_modify_by_ref_param(ByRef) ByRef;
10321032
10331033test "C function modifies by ref param" {
10341034 if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest;
1035 if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows and builtin.mode != .Debug) return error.SkipZigTest;
10361035
10371036 const res = c_modify_by_ref_param(.{ .val = 1, .arr = undefined });
10381037 try expect(res.val == 42);
test/cases/compile_errors/invalid_store_to_comptime_field.zig+10-9
......@@ -44,15 +44,15 @@ pub export fn entry5() void {
4444 comptime var y = .{ 1, 2 };
4545 y = .{ 3, 4 };
4646}
47// pub export fn entry5() void {
48// var x: u32 = 15;
49// const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
50// const S = struct {
51// fn foo(_: T) void {}
52// };
53// _ = S.foo(.{ -1234, 5679, x });
54// }
5547pub export fn entry6() void {
48 var x: u32 = 15;
49 const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
50 const S = struct {
51 fn foo(_: T) void {}
52 };
53 _ = S.foo(.{ -1234, 5679, x });
54}
55pub export fn entry7() void {
5656 const State = struct {
5757 comptime id: bool = true,
5858 fn init(comptime id: bool) @This() {
......@@ -61,7 +61,7 @@ pub export fn entry6() void {
6161 };
6262 _ = State.init(false);
6363}
64pub export fn entry7() void {
64pub export fn entry8() void {
6565 const list1 = .{ "sss", 1, 2, 3 };
6666 const list2 = @TypeOf(list1){ .@"0" = "xxx", .@"1" = 4, .@"2" = 5, .@"3" = 6 };
6767 _ = list2;
......@@ -73,6 +73,7 @@ pub export fn entry7() void {
7373//
7474// :6:19: error: value stored in comptime field does not match the default value of the field
7575// :14:19: error: value stored in comptime field does not match the default value of the field
76// :53:16: error: value stored in comptime field does not match the default value of the field
7677// :19:38: error: value stored in comptime field does not match the default value of the field
7778// :31:19: error: value stored in comptime field does not match the default value of the field
7879// :25:29: note: default value set here
test/cases/f32_passed_to_variadic_fn.zig created+15
......@@ -0,0 +1,15 @@
1extern fn printf(format: [*:0]const u8, ...) c_int;
2pub fn main() void {
3 var a: f64 = 2.0;
4 var b: f32 = 10.0;
5 _ = printf("f64: %f\n", a);
6 _ = printf("f32: %f\n", b);
7}
8
9// run
10// backend=llvm
11// target=x86_64-linux-gnu
12//
13// f64: 2.000000
14// f32: 10.000000
15//
\ No newline at end of file