don't crash when can't evaluate comptime expression with inferred type

Closes #15911.
This commit is contained in:
yujiri8 2023-05-31 07:15:52 -04:00 committed by GitHub
parent 32e719e070
commit cd1417dbdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -6021,6 +6021,10 @@ pub fn errNoteNonLazy(
comptime format: []const u8,
args: anytype,
) error{OutOfMemory}!void {
if (src_loc.lazy == .unneeded) {
assert(parent.src_loc.lazy == .unneeded);
return;
}
const msg = try std.fmt.allocPrint(mod.gpa, format, args);
errdefer mod.gpa.free(msg);

View File

@ -882,7 +882,7 @@ fn analyzeBodyInner(
var dbg_block_begins: u32 = 0;
// We use a while(true) loop here to avoid a redundant way of breaking out of
// We use a while (true) loop here to avoid a redundant way of breaking out of
// the loop. The only way to break out of the loop is with a `noreturn`
// instruction.
var i: usize = 0;
@ -18073,7 +18073,7 @@ fn zirStructInitAnon(
return sema.addConstantMaybeRef(block, tuple_ty, tuple_val, is_ref);
};
sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {
sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
error.NeededSourceLocation => {
const decl = sema.mod.declPtr(block.src_decl);
const field_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);
@ -18179,7 +18179,7 @@ fn zirArrayInit(
return sema.addConstantMaybeRef(block, array_ty, array_val, is_ref);
};
sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {
sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
error.NeededSourceLocation => {
const decl = sema.mod.declPtr(block.src_decl);
const elem_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);

View File

@ -0,0 +1,17 @@
const A = struct {
a: u8,
};
var n: u8 = 5;
var a: A = .{ .a = n };
pub export fn entry() void {
_ = a;
}
// error
// backend=stage2
// target=native
//
// :6:13: error: unable to evaluate comptime expression
// :6:16: note: operation is runtime due to this operand