commit 61fe307d0f05eea901577900f4ab2bdaf0ffb35f (tree)
parent 941677e08318c2baaabc9d0fc87892d1b63487ae
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date: Wed, 22 Jan 2025 16:47:45 +0000
Merge pull request #22571 from mlugg/various-fixes-again
compiler: a few fixes
Diffstat:
7 files changed, 78 insertions(+), 15 deletions(-)
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
@@ -413,16 +413,9 @@ pub fn assertReadable(slice: []const volatile u8) void {
for (slice) |*byte| _ = byte.*;
}
-/// By including a call to this function, the caller gains an error return trace
-/// secret parameter, making `@errorReturnTrace()` more useful. This is not
-/// necessary if the function already contains a call to an errorable function
-/// elsewhere.
-pub fn errorReturnTraceHelper() anyerror!void {}
-
/// Equivalent to `@panic` but with a formatted message.
pub fn panic(comptime format: []const u8, args: anytype) noreturn {
@branchHint(.cold);
- errorReturnTraceHelper() catch unreachable;
panicExtra(@errorReturnTrace(), @returnAddress(), format, args);
}
diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig
@@ -282,11 +282,9 @@ pub fn reallocAdvanced(
const old_byte_slice = mem.sliceAsBytes(old_mem);
const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
// Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure
- if (mem.isAligned(@intFromPtr(old_byte_slice.ptr), Slice.alignment)) {
- if (self.rawResize(old_byte_slice, log2a(Slice.alignment), byte_count, return_address)) {
- const new_bytes: []align(Slice.alignment) u8 = @alignCast(old_byte_slice.ptr[0..byte_count]);
- return mem.bytesAsSlice(T, new_bytes);
- }
+ if (self.rawResize(old_byte_slice, log2a(Slice.alignment), byte_count, return_address)) {
+ const new_bytes: []align(Slice.alignment) u8 = @alignCast(old_byte_slice.ptr[0..byte_count]);
+ return mem.bytesAsSlice(T, new_bytes);
}
const new_mem = self.rawAlloc(byte_count, log2a(Slice.alignment), return_address) orelse
diff --git a/lib/std/zig.zig b/lib/std/zig.zig
@@ -788,6 +788,7 @@ pub const SimpleComptimeReason = enum(u32) {
// Miscellaneous reasons.
comptime_keyword,
comptime_call_modifier,
+ inline_loop_operand,
switch_item,
tuple_field_default_value,
struct_field_default_value,
@@ -863,6 +864,7 @@ pub const SimpleComptimeReason = enum(u32) {
.comptime_keyword => "'comptime' keyword forces comptime evaluation",
.comptime_call_modifier => "'.compile_time' call modifier forces comptime evaluation",
+ .inline_loop_operand => "inline loop condition must be comptime-known",
.switch_item => "switch prong values must be comptime-known",
.tuple_field_default_value => "tuple field default value must be comptime-known",
.struct_field_default_value => "struct field default value must be comptime-known",
diff --git a/src/Sema.zig b/src/Sema.zig
@@ -1824,7 +1824,14 @@ fn analyzeBodyInner(
);
const uncasted_cond = try sema.resolveInst(extra.data.condition);
const cond = try sema.coerce(block, Type.bool, uncasted_cond, cond_src);
- const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, null);
+ const cond_val = try sema.resolveConstDefinedValue(
+ block,
+ cond_src,
+ cond,
+ // If this block is comptime, it's more helpful to just give the outer message.
+ // This is particularly true if this came from a comptime `condbr` above.
+ if (block.isComptime()) null else .{ .simple = .inline_loop_operand },
+ );
const inline_body = if (cond_val.toBool()) then_body else else_body;
try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
diff --git a/src/Zcu.zig b/src/Zcu.zig
@@ -1864,15 +1864,16 @@ pub const SrcLoc = struct {
if (want_case_idx.isSpecial()) {
break case;
}
+ continue;
}
const is_multi = case.ast.values.len != 1 or
node_tags[case.ast.values[0]] == .switch_range;
- if (!want_case_idx.isSpecial()) switch (want_case_idx.kind) {
+ switch (want_case_idx.kind) {
.scalar => if (!is_multi and want_case_idx.index == scalar_i) break case,
.multi => if (is_multi and want_case_idx.index == multi_i) break case,
- };
+ }
if (is_multi) {
multi_i += 1;
diff --git a/test/cases/compile_errors/invalid_switch_item.zig b/test/cases/compile_errors/invalid_switch_item.zig
@@ -0,0 +1,46 @@
+const E = enum { a, b, c };
+var my_e: E = .a;
+
+export fn f0() void {
+ switch (my_e) {
+ .a => {},
+ .b => {},
+ .x => {},
+ .c => {},
+ }
+}
+
+export fn f1() void {
+ switch (my_e) {
+ else => {},
+ .x, .y => {},
+ }
+}
+
+export fn f2() void {
+ switch (my_e) {
+ else => {},
+ .a => {},
+ .x, .y => {},
+ .b => {},
+ }
+}
+
+export fn f3() void {
+ switch (my_e) {
+ .a, .b => {},
+ .x, .y => {},
+ else => {},
+ }
+}
+
+// error
+//
+// :8:10: error: no field named 'x' in enum 'tmp.E'
+// :1:11: note: enum declared here
+// :16:10: error: no field named 'x' in enum 'tmp.E'
+// :1:11: note: enum declared here
+// :24:10: error: no field named 'x' in enum 'tmp.E'
+// :1:11: note: enum declared here
+// :32:10: error: no field named 'x' in enum 'tmp.E'
+// :1:11: note: enum declared here
diff --git a/test/cases/compile_errors/runtime_condition_in_inline_loop.zig b/test/cases/compile_errors/runtime_condition_in_inline_loop.zig
@@ -0,0 +1,16 @@
+var rt_slice: []const u8 = &.{ 1, 2, 3 };
+
+export fn foo() void {
+ inline for (rt_slice) |_| {}
+}
+
+export fn bar() void {
+ inline while (rt_slice.len == 0) {}
+}
+
+// error
+//
+// :4:17: error: unable to resolve comptime value
+// :4:17: note: inline loop condition must be comptime-known
+// :8:32: error: unable to resolve comptime value
+// :8:32: note: inline loop condition must be comptime-known