authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-07 21:35:02-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-07 21:35:02-05:00
logd69e97ae1677ca487833caf6937fa428563ed0ae
tree9299ebd6762715fd756972458f152ed123bf718f
parent913b4a3f19955296a892ffe4f148f8c097a7732a
parent6039554b26b1aee6502063d9130907369c4ffd38
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13806 from Vexu/stage2-fixes

misc fixes and improvements

30 files changed, 120 insertions(+), 194 deletions(-)

lib/compiler_rt/divdc3.zig+3-1
......@@ -3,7 +3,9 @@ const divc3 = @import("./divc3.zig");
33const Complex = @import("./mulc3.zig").Complex;
44
55comptime {
6 @export(__divdc3, .{ .name = "__divdc3", .linkage = common.linkage });
6 if (@import("builtin").zig_backend != .stage2_c) {
7 @export(__divdc3, .{ .name = "__divdc3", .linkage = common.linkage });
8 }
79}
810
911pub fn __divdc3(a: f64, b: f64, c: f64, d: f64) callconv(.C) Complex(f64) {
lib/compiler_rt/divhc3.zig+3-1
......@@ -3,7 +3,9 @@ const divc3 = @import("./divc3.zig");
33const Complex = @import("./mulc3.zig").Complex;
44
55comptime {
6 @export(__divhc3, .{ .name = "__divhc3", .linkage = common.linkage });
6 if (@import("builtin").zig_backend != .stage2_c) {
7 @export(__divhc3, .{ .name = "__divhc3", .linkage = common.linkage });
8 }
79}
810
911pub fn __divhc3(a: f16, b: f16, c: f16, d: f16) callconv(.C) Complex(f16) {
lib/compiler_rt/divsc3.zig+3-1
......@@ -3,7 +3,9 @@ const divc3 = @import("./divc3.zig");
33const Complex = @import("./mulc3.zig").Complex;
44
55comptime {
6 @export(__divsc3, .{ .name = "__divsc3", .linkage = common.linkage });
6 if (@import("builtin").zig_backend != .stage2_c) {
7 @export(__divsc3, .{ .name = "__divsc3", .linkage = common.linkage });
8 }
79}
810
911pub fn __divsc3(a: f32, b: f32, c: f32, d: f32) callconv(.C) Complex(f32) {
lib/compiler_rt/divtc3.zig+3-1
......@@ -3,7 +3,9 @@ const divc3 = @import("./divc3.zig");
33const Complex = @import("./mulc3.zig").Complex;
44
55comptime {
6 @export(__divtc3, .{ .name = "__divtc3", .linkage = common.linkage });
6 if (@import("builtin").zig_backend != .stage2_c) {
7 @export(__divtc3, .{ .name = "__divtc3", .linkage = common.linkage });
8 }
79}
810
911pub fn __divtc3(a: f128, b: f128, c: f128, d: f128) callconv(.C) Complex(f128) {
lib/compiler_rt/divxc3.zig+3-1
......@@ -3,7 +3,9 @@ const divc3 = @import("./divc3.zig");
33const Complex = @import("./mulc3.zig").Complex;
44
55comptime {
6 @export(__divxc3, .{ .name = "__divxc3", .linkage = common.linkage });
6 if (@import("builtin").zig_backend != .stage2_c) {
7 @export(__divxc3, .{ .name = "__divxc3", .linkage = common.linkage });
8 }
79}
810
911pub fn __divxc3(a: f80, b: f80, c: f80, d: f80) callconv(.C) Complex(f80) {
lib/compiler_rt/muldc3.zig+3-1
......@@ -4,7 +4,9 @@ const mulc3 = @import("./mulc3.zig");
44pub const panic = common.panic;
55
66comptime {
7 @export(__muldc3, .{ .name = "__muldc3", .linkage = common.linkage });
7 if (@import("builtin").zig_backend != .stage2_c) {
8 @export(__muldc3, .{ .name = "__muldc3", .linkage = common.linkage });
9 }
810}
911
1012pub fn __muldc3(a: f64, b: f64, c: f64, d: f64) callconv(.C) mulc3.Complex(f64) {
lib/compiler_rt/mulhc3.zig+3-1
......@@ -4,7 +4,9 @@ const mulc3 = @import("./mulc3.zig");
44pub const panic = common.panic;
55
66comptime {
7 @export(__mulhc3, .{ .name = "__mulhc3", .linkage = common.linkage });
7 if (@import("builtin").zig_backend != .stage2_c) {
8 @export(__mulhc3, .{ .name = "__mulhc3", .linkage = common.linkage });
9 }
810}
911
1012pub fn __mulhc3(a: f16, b: f16, c: f16, d: f16) callconv(.C) mulc3.Complex(f16) {
lib/compiler_rt/mulsc3.zig+3-1
......@@ -4,7 +4,9 @@ const mulc3 = @import("./mulc3.zig");
44pub const panic = common.panic;
55
66comptime {
7 @export(__mulsc3, .{ .name = "__mulsc3", .linkage = common.linkage });
7 if (@import("builtin").zig_backend != .stage2_c) {
8 @export(__mulsc3, .{ .name = "__mulsc3", .linkage = common.linkage });
9 }
810}
911
1012pub fn __mulsc3(a: f32, b: f32, c: f32, d: f32) callconv(.C) mulc3.Complex(f32) {
lib/compiler_rt/multc3.zig+3-1
......@@ -4,7 +4,9 @@ const mulc3 = @import("./mulc3.zig");
44pub const panic = common.panic;
55
66comptime {
7 @export(__multc3, .{ .name = "__multc3", .linkage = common.linkage });
7 if (@import("builtin").zig_backend != .stage2_c) {
8 @export(__multc3, .{ .name = "__multc3", .linkage = common.linkage });
9 }
810}
911
1012pub fn __multc3(a: f128, b: f128, c: f128, d: f128) callconv(.C) mulc3.Complex(f128) {
lib/compiler_rt/mulxc3.zig+3-1
......@@ -4,7 +4,9 @@ const mulc3 = @import("./mulc3.zig");
44pub const panic = common.panic;
55
66comptime {
7 @export(__mulxc3, .{ .name = "__mulxc3", .linkage = common.linkage });
7 if (@import("builtin").zig_backend != .stage2_c) {
8 @export(__mulxc3, .{ .name = "__mulxc3", .linkage = common.linkage });
9 }
810}
911
1012pub fn __mulxc3(a: f80, b: f80, c: f80, d: f80) callconv(.C) mulc3.Complex(f80) {
lib/std/build/OptionsStep.zig+1-1
......@@ -177,7 +177,7 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
177177 .Float,
178178 .Null,
179179 => try out.print("{any}", .{val}),
180 else => @compileError(comptime std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})),
180 else => @compileError(std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})),
181181 }
182182}
183183
lib/std/fmt.zig+1-1
......@@ -199,7 +199,7 @@ pub fn format(
199199 switch (missing_count) {
200200 0 => unreachable,
201201 1 => @compileError("unused argument in '" ++ fmt ++ "'"),
202 else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in '" ++ fmt ++ "'"),
202 else => @compileError(comptimePrint("{d}", .{missing_count}) ++ " unused arguments in '" ++ fmt ++ "'"),
203203 }
204204 }
205205}
lib/std/testing.zig+1-1
......@@ -765,7 +765,7 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
765765 }
766766 const expected_args_tuple_len = fn_args_fields.len - 1;
767767 if (extra_args.len != expected_args_tuple_len) {
768 @compileError("The provided function expects " ++ (comptime std.fmt.comptimePrint("{d}", .{expected_args_tuple_len})) ++ " extra arguments, but the provided tuple contains " ++ (comptime std.fmt.comptimePrint("{d}", .{extra_args.len})));
768 @compileError("The provided function expects " ++ std.fmt.comptimePrint("{d}", .{expected_args_tuple_len}) ++ " extra arguments, but the provided tuple contains " ++ std.fmt.comptimePrint("{d}", .{extra_args.len}));
769769 }
770770
771771 // Setup the tuple that will actually be used with @call (we'll need to insert
lib/std/zig/tokenizer.zig+17-1
......@@ -425,7 +425,16 @@ pub const Tokenizer = struct {
425425 const c = self.buffer[self.index];
426426 switch (state) {
427427 .start => switch (c) {
428 0 => break,
428 0 => {
429 if (self.index != self.buffer.len) {
430 result.tag = .invalid;
431 result.loc.start = self.index;
432 self.index += 1;
433 result.loc.end = self.index;
434 return result;
435 }
436 break;
437 },
429438 ' ', '\n', '\t', '\r' => {
430439 result.loc.start = self.index + 1;
431440 },
......@@ -1851,6 +1860,13 @@ test "saturating operators" {
18511860 try testTokenize("-|=", &.{.minus_pipe_equal});
18521861}
18531862
1863test "null byte before eof" {
1864 try testTokenize("123 \x00 456", &.{ .number_literal, .invalid, .number_literal });
1865 try testTokenize("//\x00", &.{.invalid});
1866 try testTokenize("\\\\\x00", &.{ .multiline_string_literal_line, .invalid });
1867 try testTokenize("\x00", &.{.invalid});
1868}
1869
18541870fn testTokenize(source: [:0]const u8, expected_token_tags: []const Token.Tag) !void {
18551871 var tokenizer = Tokenizer.init(source);
18561872 for (expected_token_tags) |expected_token_tag| {
src/AstGen.zig+4
......@@ -8455,8 +8455,12 @@ fn simpleUnOp(
84558455 operand_node: Ast.Node.Index,
84568456 tag: Zir.Inst.Tag,
84578457) InnerError!Zir.Inst.Ref {
8458 const prev_force_comptime = gz.force_comptime;
8459 defer gz.force_comptime = prev_force_comptime;
8460
84588461 switch (tag) {
84598462 .tag_name, .error_name, .ptr_to_int => try emitDbgNode(gz, node),
8463 .compile_error => gz.force_comptime = true,
84608464 else => {},
84618465 }
84628466 const operand = try expr(gz, scope, operand_ri, operand_node);
src/Sema.zig+17
......@@ -5405,6 +5405,13 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
54055405 },
54065406 else => |e| return e,
54075407 };
5408 {
5409 try sema.mod.ensureDeclAnalyzed(decl_index);
5410 const exported_decl = sema.mod.declPtr(decl_index);
5411 if (exported_decl.val.castTag(.function)) |some| {
5412 return sema.analyzeExport(block, src, options, some.data.owner_decl);
5413 }
5414 }
54085415 try sema.analyzeExport(block, src, options, decl_index);
54095416}
54105417
......@@ -17870,6 +17877,13 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1787017877 operand_ty.fmt(mod),
1787117878 }),
1787217879 };
17880 if (enum_ty.enumFieldCount() == 0) {
17881 // TODO I don't think this is the correct way to handle this but
17882 // it prevents a crash.
17883 return sema.fail(block, operand_src, "cannot get @tagName of empty enum '{}'", .{
17884 enum_ty.fmt(mod),
17885 });
17886 }
1787317887 const enum_decl_index = enum_ty.getOwnerDecl();
1787417888 const casted_operand = try sema.coerce(block, enum_ty, operand, operand_src);
1787517889 if (try sema.resolveDefinedValue(block, operand_src, casted_operand)) |val| {
......@@ -20997,6 +21011,9 @@ fn analyzeMinMax(
2099721011
2099821012 if (rhs_val.isUndef()) return sema.addConstUndef(simd_op.result_ty);
2099921013
21014 try sema.resolveLazyValue(lhs_val);
21015 try sema.resolveLazyValue(rhs_val);
21016
2100021017 const opFunc = switch (air_tag) {
2100121018 .min => Value.numberMin,
2100221019 .max => Value.numberMax,
src/TypedValue.zig+42-8
......@@ -46,6 +46,7 @@ pub fn enumToInt(tv: TypedValue, buffer: *Value.Payload.U64) Value {
4646}
4747
4848const max_aggregate_items = 100;
49const max_string_len = 256;
4950
5051const FormatContext = struct {
5152 tv: TypedValue,
......@@ -141,10 +142,12 @@ pub fn print(
141142 .extern_options_type => return writer.writeAll("std.builtin.ExternOptions"),
142143 .type_info_type => return writer.writeAll("std.builtin.Type"),
143144
144 .empty_struct_value, .aggregate => {
145 .empty_struct_value => return writer.writeAll(".{}"),
146 .aggregate => {
145147 if (level == 0) {
146148 return writer.writeAll(".{ ... }");
147149 }
150 const values = val.castTag(.aggregate).?;
148151 if (ty.zigTypeTag() == .Struct) {
149152 try writer.writeAll(".{");
150153 const max_len = std.math.min(ty.structFieldCount(), max_aggregate_items);
......@@ -159,9 +162,9 @@ pub fn print(
159162 try print(.{
160163 .ty = ty.structFieldType(i),
161164 .val = switch (ty.containerLayout()) {
162 .Packed => val.castTag(.aggregate).?.data[i],
165 .Packed => values.data[i],
163166 else => ty.structFieldValueComptime(i) orelse b: {
164 const vals = val.castTag(.aggregate).?.data;
167 const vals = values.data;
165168 break :b vals[i];
166169 },
167170 },
......@@ -172,17 +175,31 @@ pub fn print(
172175 }
173176 return writer.writeAll("}");
174177 } else {
175 try writer.writeAll(".{ ");
176178 const elem_ty = ty.elemType2();
177179 const len = ty.arrayLen();
178 const max_len = std.math.min(len, max_aggregate_items);
179180
181 if (elem_ty.eql(Type.u8, mod)) str: {
182 const max_len = @intCast(usize, std.math.min(len, max_string_len));
183 var buf: [max_string_len]u8 = undefined;
184
185 var i: u32 = 0;
186 while (i < max_len) : (i += 1) {
187 buf[i] = std.math.cast(u8, values.data[i].toUnsignedInt(target)) orelse break :str;
188 }
189
190 const truncated = if (len > max_string_len) " (truncated)" else "";
191 return writer.print("\"{}{s}\"", .{ std.zig.fmtEscapes(buf[0..max_len]), truncated });
192 }
193
194 try writer.writeAll(".{ ");
195
196 const max_len = std.math.min(len, max_aggregate_items);
180197 var i: u32 = 0;
181198 while (i < max_len) : (i += 1) {
182199 if (i != 0) try writer.writeAll(", ");
183200 try print(.{
184201 .ty = elem_ty,
185 .val = val.castTag(.aggregate).?.data[i],
202 .val = values.data[i],
186203 }, writer, level - 1, mod);
187204 }
188205 if (len > max_aggregate_items) {
......@@ -372,11 +389,28 @@ pub fn print(
372389 return writer.writeAll(".{ ... }");
373390 }
374391 const payload = val.castTag(.slice).?.data;
375 try writer.writeAll(".{ ");
376392 const elem_ty = ty.elemType2();
377393 const len = payload.len.toUnsignedInt(target);
378 const max_len = std.math.min(len, max_aggregate_items);
379394
395 if (elem_ty.eql(Type.u8, mod)) str: {
396 const max_len = @intCast(usize, std.math.min(len, max_string_len));
397 var buf: [max_string_len]u8 = undefined;
398
399 var i: u32 = 0;
400 while (i < max_len) : (i += 1) {
401 var elem_buf: Value.ElemValueBuffer = undefined;
402 const elem_val = payload.ptr.elemValueBuffer(mod, i, &elem_buf);
403 buf[i] = std.math.cast(u8, elem_val.toUnsignedInt(target)) orelse break :str;
404 }
405
406 // TODO would be nice if this had a bit of unicode awareness.
407 const truncated = if (len > max_string_len) " (truncated)" else "";
408 return writer.print("\"{}{s}\"", .{ std.zig.fmtEscapes(buf[0..max_len]), truncated });
409 }
410
411 try writer.writeAll(".{ ");
412
413 const max_len = std.math.min(len, max_aggregate_items);
380414 var i: u32 = 0;
381415 while (i < max_len) : (i += 1) {
382416 if (i != 0) try writer.writeAll(", ");
test/behavior/maximum_minimum.zig+7
......@@ -95,3 +95,10 @@ test "@min for vectors" {
9595 try S.doTheTest();
9696 comptime try S.doTheTest();
9797}
98
99test "@min/@max on lazy values" {
100 const A = extern struct { u8_4: [4]u8 };
101 const B = extern struct { u8_16: [16]u8 };
102 const size = @max(@sizeOf(A), @sizeOf(B));
103 try expect(size == @sizeOf(B));
104}
test/cases/compile_errors/stage1/attempt_to_use_0_bit_type_in_extern_fn.zig deleted-17
......@@ -1,17 +0,0 @@
1extern fn foo(ptr: fn(*void) callconv(.C) void) void;
2
3export fn entry() void {
4 foo(bar);
5}
6
7fn bar(x: *void) callconv(.C) void { _ = x; }
8export fn entry2() void {
9 bar(&{});
10}
11
12// error
13// backend=stage1
14// target=native
15//
16// tmp.zig:1:23: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'
17// tmp.zig:7:11: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'
test/cases/compile_errors/stage1/comptime_ptrcast_of_zero-sized_type.zig deleted-12
......@@ -1,12 +0,0 @@
1fn foo() void {
2 const node: struct {} = undefined;
3 const vla_ptr = @ptrCast([*]const u8, &node);
4 _ = vla_ptr;
5}
6comptime { foo(); }
7
8// error
9// backend=stage1
10// target=native
11//
12// tmp.zig:3:21: error: '*const struct:2:17' and '[*]const u8' do not have the same in-memory representation
test/cases/compile_errors/stage1/error_equality_but_sets_have_no_common_members.zig deleted-16
......@@ -1,16 +0,0 @@
1const Set1 = error{A, C};
2const Set2 = error{B, D};
3export fn entry() void {
4 foo(Set1.A);
5}
6fn foo(x: Set1) void {
7 if (x == Set2.B) {
8
9 }
10}
11
12// error
13// backend=stage1
14// target=native
15//
16// tmp.zig:7:11: error: error sets 'Set1' and 'Set2' have no common errors
test/cases/compile_errors/stage1/implicit_casting_undefined_c_pointer_to_zig_pointer.zig deleted-11
......@@ -1,11 +0,0 @@
1comptime {
2 var c_ptr: [*c]u8 = undefined;
3 var zig_ptr: *u8 = c_ptr;
4 _ = zig_ptr;
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:3:24: error: use of undefined value here causes undefined behavior
test/cases/compile_errors/stage1/issue_2687_coerce_from_undefined_array_pointer_to_slice.zig deleted-27
......@@ -1,27 +0,0 @@
1export fn foo1() void {
2 const a: *[1]u8 = undefined;
3 var b: []u8 = a;
4 _ = b;
5}
6export fn foo2() void {
7 comptime {
8 var a: *[1]u8 = undefined;
9 var b: []u8 = a;
10 _ = b;
11 }
12}
13export fn foo3() void {
14 comptime {
15 const a: *[1]u8 = undefined;
16 var b: []u8 = a;
17 _ = b;
18 }
19}
20
21// error
22// backend=stage1
23// target=native
24//
25// tmp.zig:3:19: error: use of undefined value here causes undefined behavior
26// tmp.zig:9:23: error: use of undefined value here causes undefined behavior
27// tmp.zig:16:23: error: use of undefined value here causes undefined behavior
test/cases/compile_errors/stage1/ptrToInt_on_void.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() bool {
2 return @ptrToInt(&{}) == @ptrToInt(&{});
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:23: error: pointer to size 0 type has no address
test/cases/compile_errors/stage1/ptrToInt_with_pointer_to_zero-sized_type.zig deleted-11
......@@ -1,11 +0,0 @@
1export fn entry() void {
2 var pointer: ?*u0 = null;
3 var x = @ptrToInt(pointer);
4 _ = x;
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:3:23: error: pointer to size 0 type has no address
test/cases/compile_errors/stage1/reify_type.Pointer_with_invalid_address_space.zig deleted-18
......@@ -1,18 +0,0 @@
1export fn entry() void {
2 _ = @Type(.{ .Pointer = .{
3 .size = .One,
4 .is_const = false,
5 .is_volatile = false,
6 .alignment = 1,
7 .address_space = .gs,
8 .child = u8,
9 .is_allowzero = false,
10 .sentinel = null,
11 }});
12}
13
14// error
15// backend=stage1
16// target=native
17//
18// tmp.zig:2:16: error: address space 'gs' not available in stage 1 compiler, must be .generic
test/cases/compile_errors/stage1/reify_type_with_non-constant_expression.zig deleted-11
......@@ -1,11 +0,0 @@
1const builtin = @import("std").builtin;
2var globalTypeInfo : builtin.Type = undefined;
3export fn entry() void {
4 _ = @Type(globalTypeInfo);
5}
6
7// error
8// backend=stage1
9// target=native
10//
11// tmp.zig:4:15: error: unable to evaluate constant expression
test/cases/compile_errors/stage1/slicing_of_global_undefined_pointer.zig deleted-10
......@@ -1,10 +0,0 @@
1var buf: *[1]u8 = undefined;
2export fn entry() void {
3 _ = buf[0..1];
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:3:12: error: non-zero length slice of undefined pointer
test/cases/compile_errors/stage1/switch_with_invalid_expression_parameter.zig deleted-17
......@@ -1,17 +0,0 @@
1export fn entry() void {
2 Test(i32);
3}
4fn Test(comptime T: type) void {
5 const x = switch (T) {
6 []u8 => |x| x,
7 i32 => |x| x,
8 else => unreachable,
9 };
10 _ = x;
11}
12
13// error
14// backend=stage1
15// target=native
16//
17// tmp.zig:7:17: error: switch on type 'type' provides no expression parameter
test/cases/compile_errors/stage1/use_of_comptime-known_undefined_function_value.zig deleted-13
......@@ -1,13 +0,0 @@
1const Cmd = struct {
2 exec: fn () void,
3};
4export fn entry() void {
5 const command = Cmd{ .exec = undefined };
6 command.exec();
7}
8
9// error
10// backend=stage1
11// target=native
12//
13// tmp.zig:6:12: error: use of undefined value here causes undefined behavior