Merge pull request #22571 from mlugg/various-fixes-again

compiler: a few fixes
This commit is contained in:
Matthew Lugg 2025-01-22 16:47:45 +00:00 committed by GitHub
commit 61fe307d0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 15 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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",

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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