authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-13 00:35:09+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-21 12:21:30-07:00
log76e7959a90ae025a1934a4ad1c689663f523a4bc
tree7a6fb3a23b00fc79af6140c7ba90414bd2e13951
parent195c3cd89fe2b8e1b3aefe12de074878244d9e94

Sema: explain why comptime is needed


11 files changed, 359 insertions(+), 317 deletions(-)

src/Module.zig+2-14
......@@ -4065,18 +4065,6 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
40654065 var wip_captures = try WipCaptureScope.init(gpa, new_decl_arena_allocator, null);
40664066 defer wip_captures.deinit();
40674067
4068 var block_scope: Sema.Block = .{
4069 .parent = null,
4070 .sema = &sema,
4071 .src_decl = new_decl_index,
4072 .namespace = &struct_obj.namespace,
4073 .wip_capture_scope = wip_captures.scope,
4074 .instructions = .{},
4075 .inlining = null,
4076 .is_comptime = true,
4077 };
4078 defer block_scope.instructions.deinit(gpa);
4079
40804068 if (sema.analyzeStructDecl(new_decl, main_struct_inst, struct_obj)) |_| {
40814069 try wip_captures.finalize();
40824070 new_decl.analysis = .complete;
......@@ -4185,7 +4173,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
41854173 const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
41864174 try wip_captures.finalize();
41874175 const src = LazySrcLoc.nodeOffset(0);
4188 const decl_tv = try sema.resolveInstValue(&block_scope, src, result_ref);
4176 const decl_tv = try sema.resolveInstValue(&block_scope, .unneeded, result_ref, undefined);
41894177 const decl_align: u32 = blk: {
41904178 const align_ref = decl.zirAlignRef();
41914179 if (align_ref == .none) break :blk 0;
......@@ -4194,7 +4182,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
41944182 const decl_linksection: ?[*:0]const u8 = blk: {
41954183 const linksection_ref = decl.zirLinksectionRef();
41964184 if (linksection_ref == .none) break :blk null;
4197 const bytes = try sema.resolveConstString(&block_scope, src, linksection_ref);
4185 const bytes = try sema.resolveConstString(&block_scope, src, linksection_ref, "linksection must be comptime known");
41984186 break :blk (try decl_arena_allocator.dupeZ(u8, bytes)).ptr;
41994187 };
42004188 const target = sema.mod.getTarget();
src/Sema.zig+346-295
......@@ -892,7 +892,7 @@ fn analyzeBodyInner(
892892 .shl_sat => try sema.zirShl(block, inst, .shl_sat),
893893
894894 .ret_ptr => try sema.zirRetPtr(block, inst),
895 .ret_type => try sema.zirRetType(block, inst),
895 .ret_type => try sema.addType(sema.fn_ret_ty),
896896
897897 // Instructions that we know to *always* be noreturn based solely on their tag.
898898 // These functions match the return type of analyzeBody so that we can
......@@ -1173,7 +1173,7 @@ fn analyzeBodyInner(
11731173 } else {
11741174 const src_node = sema.code.instructions.items(.data)[inst].node;
11751175 const src = LazySrcLoc.nodeOffset(src_node);
1176 try sema.requireRuntimeBlock(block, src);
1176 try sema.requireFunctionBlock(block, src);
11771177 break always_noreturn;
11781178 }
11791179 },
......@@ -1303,7 +1303,7 @@ fn analyzeBodyInner(
13031303 const extra = sema.code.extraData(Zir.Inst.CondBr, inst_data.payload_index);
13041304 const then_body = sema.code.extra[extra.end..][0..extra.data.then_body_len];
13051305 const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
1306 const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition);
1306 const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition, "condition in comptime branch must be comptime known");
13071307 const inline_body = if (cond.val.toBool()) then_body else else_body;
13081308 const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
13091309 break always_noreturn;
......@@ -1319,7 +1319,7 @@ fn analyzeBodyInner(
13191319 const extra = sema.code.extraData(Zir.Inst.CondBr, inst_data.payload_index);
13201320 const then_body = sema.code.extra[extra.end..][0..extra.data.then_body_len];
13211321 const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
1322 const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition);
1322 const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition, "condition in comptime branch must be comptime known");
13231323 const inline_body = if (cond.val.toBool()) then_body else else_body;
13241324 const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
13251325 break always_noreturn;
......@@ -1339,7 +1339,7 @@ fn analyzeBodyInner(
13391339 const err_union = try sema.resolveInst(extra.data.operand);
13401340 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
13411341 assert(is_non_err != .none);
1342 const is_non_err_tv = try sema.resolveInstConst(block, operand_src, is_non_err);
1342 const is_non_err_tv = try sema.resolveInstConst(block, operand_src, is_non_err, "try operand inside comptime block must be comptime known");
13431343 if (is_non_err_tv.val.toBool()) {
13441344 const err_union_ty = sema.typeOf(err_union);
13451345 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
......@@ -1395,7 +1395,7 @@ fn analyzeBodyInner(
13951395 const err_union = try sema.analyzeLoad(block, src, operand, operand_src);
13961396 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
13971397 assert(is_non_err != .none);
1398 const is_non_err_tv = try sema.resolveInstConst(block, operand_src, is_non_err);
1398 const is_non_err_tv = try sema.resolveInstConst(block, operand_src, is_non_err, "try operand inside comptime block must be comptime known");
13991399 if (is_non_err_tv.val.toBool()) {
14001400 break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false);
14011401 }
......@@ -1478,11 +1478,12 @@ fn resolveConstBool(
14781478 block: *Block,
14791479 src: LazySrcLoc,
14801480 zir_ref: Zir.Inst.Ref,
1481 reason: []const u8,
14811482) !bool {
14821483 const air_inst = try sema.resolveInst(zir_ref);
14831484 const wanted_type = Type.bool;
14841485 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
1485 const val = try sema.resolveConstValue(block, src, coerced_inst);
1486 const val = try sema.resolveConstValue(block, src, coerced_inst, reason);
14861487 return val.toBool();
14871488}
14881489
......@@ -1491,11 +1492,12 @@ pub fn resolveConstString(
14911492 block: *Block,
14921493 src: LazySrcLoc,
14931494 zir_ref: Zir.Inst.Ref,
1495 reason: []const u8,
14941496) ![]u8 {
14951497 const air_inst = try sema.resolveInst(zir_ref);
14961498 const wanted_type = Type.initTag(.const_slice_u8);
14971499 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
1498 const val = try sema.resolveConstValue(block, src, coerced_inst);
1500 const val = try sema.resolveConstValue(block, src, coerced_inst, reason);
14991501 return val.toAllocatedBytes(wanted_type, sema.arena, sema.mod);
15001502}
15011503
......@@ -1514,7 +1516,7 @@ fn analyzeAsType(
15141516) !Type {
15151517 const wanted_type = Type.initTag(.@"type");
15161518 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
1517 const val = try sema.resolveConstValue(block, src, coerced_inst);
1519 const val = try sema.resolveConstValue(block, src, coerced_inst, "types must be comptime known");
15181520 var buffer: Value.ToTypeBuffer = undefined;
15191521 const ty = val.toType(&buffer);
15201522 return ty.copy(sema.arena);
......@@ -1567,12 +1569,13 @@ fn resolveValue(
15671569 block: *Block,
15681570 src: LazySrcLoc,
15691571 air_ref: Air.Inst.Ref,
1572 reason: []const u8,
15701573) CompileError!Value {
15711574 if (try sema.resolveMaybeUndefValAllowVariables(block, src, air_ref)) |val| {
15721575 if (val.tag() == .generic_poison) return error.GenericPoison;
15731576 return val;
15741577 }
1575 return sema.failWithNeededComptime(block, src);
1578 return sema.failWithNeededComptime(block, src, reason);
15761579}
15771580
15781581/// Value Tag `variable` will cause a compile error.
......@@ -1582,15 +1585,16 @@ fn resolveConstMaybeUndefVal(
15821585 block: *Block,
15831586 src: LazySrcLoc,
15841587 inst: Air.Inst.Ref,
1588 reason: []const u8,
15851589) CompileError!Value {
15861590 if (try sema.resolveMaybeUndefValAllowVariables(block, src, inst)) |val| {
15871591 switch (val.tag()) {
1588 .variable => return sema.failWithNeededComptime(block, src),
1592 .variable => return sema.failWithNeededComptime(block, src, reason),
15891593 .generic_poison => return error.GenericPoison,
15901594 else => return val,
15911595 }
15921596 }
1593 return sema.failWithNeededComptime(block, src);
1597 return sema.failWithNeededComptime(block, src, reason);
15941598}
15951599
15961600/// Will not return Value Tags: `variable`, `undef`. Instead they will emit compile errors.
......@@ -1600,16 +1604,17 @@ fn resolveConstValue(
16001604 block: *Block,
16011605 src: LazySrcLoc,
16021606 air_ref: Air.Inst.Ref,
1607 reason: []const u8,
16031608) CompileError!Value {
16041609 if (try sema.resolveMaybeUndefValAllowVariables(block, src, air_ref)) |val| {
16051610 switch (val.tag()) {
16061611 .undef => return sema.failWithUseOfUndef(block, src),
1607 .variable => return sema.failWithNeededComptime(block, src),
1612 .variable => return sema.failWithNeededComptime(block, src, reason),
16081613 .generic_poison => return error.GenericPoison,
16091614 else => return val,
16101615 }
16111616 }
1612 return sema.failWithNeededComptime(block, src);
1617 return sema.failWithNeededComptime(block, src, reason);
16131618}
16141619
16151620/// Value Tag `variable` causes this function to return `null`.
......@@ -1697,8 +1702,15 @@ fn resolveMaybeUndefValAllowVariables(
16971702 }
16981703}
16991704
1700fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
1701 return sema.fail(block, src, "unable to resolve comptime value", .{});
1705fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: []const u8) CompileError {
1706 const msg = msg: {
1707 const msg = try sema.errMsg(block, src, "unable to resolve comptime value", .{});
1708 errdefer msg.destroy(sema.gpa);
1709
1710 try sema.errNote(block, src, msg, "{s}", .{reason});
1711 break :msg msg;
1712 };
1713 return sema.failWithOwnedErrorMsg(block, msg);
17021714}
17031715
17041716fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
......@@ -1870,7 +1882,7 @@ fn analyzeAsAlign(
18701882 src: LazySrcLoc,
18711883 air_ref: Air.Inst.Ref,
18721884) !u32 {
1873 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty);
1885 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, "alignment must be comptime known");
18741886 const alignment = @intCast(u32, alignment_big); // We coerce to u16 in the prev line.
18751887 if (alignment == 0) return sema.fail(block, src, "alignment must be >= 1", .{});
18761888 if (!std.math.isPowerOfTwo(alignment)) {
......@@ -1897,9 +1909,10 @@ fn resolveInt(
18971909 src: LazySrcLoc,
18981910 zir_ref: Zir.Inst.Ref,
18991911 dest_ty: Type,
1912 reason: []const u8,
19001913) !u64 {
19011914 const air_ref = try sema.resolveInst(zir_ref);
1902 return analyzeAsInt(sema, block, src, air_ref, dest_ty);
1915 return analyzeAsInt(sema, block, src, air_ref, dest_ty, reason);
19031916}
19041917
19051918fn analyzeAsInt(
......@@ -1908,9 +1921,10 @@ fn analyzeAsInt(
19081921 src: LazySrcLoc,
19091922 air_ref: Air.Inst.Ref,
19101923 dest_ty: Type,
1924 reason: []const u8,
19111925) !u64 {
19121926 const coerced = try sema.coerce(block, dest_ty, air_ref, src);
1913 const val = try sema.resolveConstValue(block, src, coerced);
1927 const val = try sema.resolveConstValue(block, src, coerced, reason);
19141928 const target = sema.mod.getTarget();
19151929 return (try val.getUnsignedIntAdvanced(target, sema.kit(block, src))).?;
19161930}
......@@ -1922,9 +1936,10 @@ pub fn resolveInstConst(
19221936 block: *Block,
19231937 src: LazySrcLoc,
19241938 zir_ref: Zir.Inst.Ref,
1939 reason: []const u8,
19251940) CompileError!TypedValue {
19261941 const air_ref = try sema.resolveInst(zir_ref);
1927 const val = try sema.resolveConstValue(block, src, air_ref);
1942 const val = try sema.resolveConstValue(block, src, air_ref, reason);
19281943 return TypedValue{
19291944 .ty = sema.typeOf(air_ref),
19301945 .val = val,
......@@ -1938,9 +1953,10 @@ pub fn resolveInstValue(
19381953 block: *Block,
19391954 src: LazySrcLoc,
19401955 zir_ref: Zir.Inst.Ref,
1956 reason: []const u8,
19411957) CompileError!TypedValue {
19421958 const air_ref = try sema.resolveInst(zir_ref);
1943 const val = try sema.resolveValue(block, src, air_ref);
1959 const val = try sema.resolveValue(block, src, air_ref, reason);
19441960 return TypedValue{
19451961 .ty = sema.typeOf(air_ref),
19461962 .val = val,
......@@ -1974,7 +1990,7 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
19741990 defer trash_block.instructions.deinit(sema.gpa);
19751991 const operand = try trash_block.addBitCast(pointee_ty, .void_value);
19761992
1977 try sema.requireRuntimeBlock(block, src);
1993 try sema.requireFunctionBlock(block, src);
19781994 const ptr_ty = try Type.ptr(sema.arena, sema.mod, .{
19791995 .pointee_type = pointee_ty,
19801996 .@"align" = inferred_alloc.alignment,
......@@ -2259,7 +2275,7 @@ fn createAnonymousDeclTypeNamed(
22592275 const arg = sema.inst_map.get(zir_inst).?;
22602276 // The comptime call code in analyzeCall already did this, so we're
22612277 // just repeating it here and it's guaranteed to work.
2262 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg) catch unreachable;
2278 const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, undefined) catch unreachable;
22632279
22642280 if (arg_i != 0) try buf.appendSlice(",");
22652281 try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
......@@ -2515,7 +2531,7 @@ fn zirEnumDecl(
25152531 // TODO: if we need to report an error here, use a source location
25162532 // that points to this default value expression rather than the struct.
25172533 // But only resolve the source location if we need to emit a compile error.
2518 const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref)).val;
2534 const tag_val = (try sema.resolveInstConst(block, src, tag_val_ref, "enum tag value must be comptime known")).val;
25192535 last_tag_val = tag_val;
25202536 const copied_tag_val = try tag_val.copy(new_decl_arena_allocator);
25212537 enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{
......@@ -2738,7 +2754,6 @@ fn zirRetPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
27382754
27392755 const inst_data = sema.code.instructions.items(.data)[inst].node;
27402756 const src = LazySrcLoc.nodeOffset(inst_data);
2741 try sema.requireFunctionBlock(block, src);
27422757
27432758 if (block.is_comptime or try sema.typeRequiresComptime(block, src, sema.fn_ret_ty)) {
27442759 const fn_ret_ty = try sema.resolveTypeFields(block, src, sema.fn_ret_ty);
......@@ -2770,16 +2785,6 @@ fn zirRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
27702785 return sema.analyzeRef(block, inst_data.src(), operand);
27712786}
27722787
2773fn zirRetType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2774 const tracy = trace(@src());
2775 defer tracy.end();
2776
2777 const inst_data = sema.code.instructions.items(.data)[inst].node;
2778 const src = LazySrcLoc.nodeOffset(inst_data);
2779 try sema.requireFunctionBlock(block, src);
2780 return sema.addType(sema.fn_ret_ty);
2781}
2782
27832788fn zirEnsureResultUsed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
27842789 const tracy = trace(@src());
27852790 defer tracy.end();
......@@ -2925,7 +2930,7 @@ fn zirAllocExtended(
29252930 try sema.validateVarType(block, ty_src, var_ty, false);
29262931 }
29272932 const target = sema.mod.getTarget();
2928 try sema.requireRuntimeBlock(block, src);
2933 try sema.requireFunctionBlock(block, src);
29292934 try sema.resolveTypeLayout(block, src, var_ty);
29302935 const ptr_type = try Type.ptr(sema.arena, sema.mod, .{
29312936 .pointee_type = var_ty,
......@@ -3024,7 +3029,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
30243029 return sema.addConstant(const_ptr_ty, val);
30253030 }
30263031
3027 try sema.requireRuntimeBlock(block, src);
3032 try sema.requireFunctionBlock(block, src);
30283033 return block.addBitCast(const_ptr_ty, alloc);
30293034}
30303035
......@@ -3061,7 +3066,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
30613066 .pointee_type = var_ty,
30623067 .@"addrspace" = target_util.defaultAddressSpace(target, .local),
30633068 });
3064 try sema.requireRuntimeBlock(block, var_decl_src);
3069 try sema.requireFunctionBlock(block, var_decl_src);
30653070 try sema.queueFullTypeResolution(var_ty);
30663071 return block.addTy(.alloc, ptr_type);
30673072}
......@@ -3083,7 +3088,7 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
30833088 .pointee_type = var_ty,
30843089 .@"addrspace" = target_util.defaultAddressSpace(target, .local),
30853090 });
3086 try sema.requireRuntimeBlock(block, var_decl_src);
3091 try sema.requireFunctionBlock(block, var_decl_src);
30873092 try sema.queueFullTypeResolution(var_ty);
30883093 return block.addTy(.alloc, ptr_type);
30893094}
......@@ -3272,7 +3277,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
32723277 return;
32733278 }
32743279
3275 try sema.requireRuntimeBlock(block, src);
3280 try sema.requireFunctionBlock(block, src);
32763281 try sema.queueFullTypeResolution(final_elem_ty);
32773282
32783283 // Change it to a normal alloc.
......@@ -3593,7 +3598,7 @@ fn validateUnionInit(
35933598 return;
35943599 }
35953600
3596 try sema.requireRuntimeBlock(block, init_src);
3601 try sema.requireFunctionBlock(block, init_src);
35973602 const new_tag = try sema.addConstant(union_obj.tag_ty, tag_val);
35983603 _ = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
35993604}
......@@ -3859,7 +3864,7 @@ fn zirValidateArrayInit(
38593864 // any ZIR instructions at comptime; we need to do that here.
38603865 if (array_ty.sentinel()) |sentinel_val| {
38613866 const array_len_ref = try sema.addIntUnsigned(Type.usize, array_len);
3862 const sentinel_ptr = try sema.elemPtrArray(block, init_src, array_ptr, init_src, array_len_ref, true);
3867 const sentinel_ptr = try sema.elemPtrArray(block, init_src, init_src, array_ptr, init_src, array_len_ref, true);
38633868 const sentinel = try sema.addConstant(array_ty.childType(), sentinel_val);
38643869 try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store);
38653870 }
......@@ -4207,10 +4212,8 @@ fn storeToInferredAllocComptime(
42074212 const operand_ty = sema.typeOf(operand);
42084213 // There will be only one store_to_inferred_ptr because we are running at comptime.
42094214 // The alloc will turn into a Decl.
4210 if (try sema.resolveMaybeUndefValAllowVariables(block, src, operand)) |operand_val| {
4211 if (operand_val.tag() == .variable) {
4212 return sema.failWithNeededComptime(block, src);
4213 }
4215 if (try sema.resolveMaybeUndefValAllowVariables(block, src, operand)) |operand_val| store: {
4216 if (operand_val.tag() == .variable) break :store;
42144217 var anon_decl = try block.startAnonDecl(src);
42154218 defer anon_decl.deinit();
42164219 iac.data.decl_index = try anon_decl.finish(
......@@ -4219,15 +4222,15 @@ fn storeToInferredAllocComptime(
42194222 iac.data.alignment,
42204223 );
42214224 return;
4222 } else {
4223 return sema.failWithNeededComptime(block, src);
42244225 }
4226
4227 return sema.failWithNeededComptime(block, src, "value being stored to a comptime variable must be comptime known");
42254228}
42264229
42274230fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
42284231 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
42294232 const src = inst_data.src();
4230 const quota = @intCast(u32, try sema.resolveInt(block, src, inst_data.operand, Type.u32));
4233 const quota = @intCast(u32, try sema.resolveInt(block, src, inst_data.operand, Type.u32, "eval branch quota must be comptime known"));
42314234 sema.branch_quota = @maximum(sema.branch_quota, quota);
42324235}
42334236
......@@ -4282,7 +4285,7 @@ fn zirParamType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
42824285 var param_index = inst_data.param_index;
42834286
42844287 const fn_ty = if (callee_ty.tag() == .bound_fn) fn_ty: {
4285 const bound_fn_val = try sema.resolveConstValue(block, callee_src, callee);
4288 const bound_fn_val = try sema.resolveConstValue(block, .unneeded, callee, undefined);
42864289 const bound_fn = bound_fn_val.castTag(.bound_fn).?.data;
42874290 const fn_ty = sema.typeOf(bound_fn.func_inst);
42884291 param_index += 1;
......@@ -4417,7 +4420,7 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
44174420 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
44184421 const src = inst_data.src();
44194422 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
4420 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand);
4423 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, "compile error string must be comptime known");
44214424 return sema.fail(block, src, "{s}", .{msg});
44224425}
44234426
......@@ -4466,7 +4469,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index, force_comptime: bo
44664469 if (block.is_comptime or force_comptime) {
44674470 return sema.fail(block, src, "encountered @panic at comptime", .{});
44684471 }
4469 try sema.requireRuntimeBlock(block, src);
4472 try sema.requireFunctionBlock(block, src);
44704473 return sema.panicWithMsg(block, src, msg_inst);
44714474}
44724475
......@@ -4854,7 +4857,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
48544857 const src = inst_data.src();
48554858 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
48564859 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
4857 const operand = try sema.resolveInstConst(block, operand_src, extra.operand);
4860 const operand = try sema.resolveInstConst(block, operand_src, extra.operand, "export target must be comptime known");
48584861 const options = try sema.resolveExportOptions(block, options_src, extra.options);
48594862 const decl_index = switch (operand.val.tag()) {
48604863 .function => operand.val.castTag(.function).?.data.owner_decl,
......@@ -4989,7 +4992,7 @@ fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
49894992fn zirSetCold(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
49904993 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
49914994 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
4992 const is_cold = try sema.resolveConstBool(block, operand_src, inst_data.operand);
4995 const is_cold = try sema.resolveConstBool(block, operand_src, inst_data.operand, "operand to @setCold must be comptime known");
49934996 const func = sema.func orelse return; // does nothing outside a function
49944997 func.is_cold = is_cold;
49954998}
......@@ -4997,7 +5000,7 @@ fn zirSetCold(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
49975000fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
49985001 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
49995002 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
5000 const float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode");
5003 const float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", "operand to @setFloatMode must be comptime known");
50015004 switch (float_mode) {
50025005 .Strict => return,
50035006 .Optimized => {
......@@ -5009,7 +5012,7 @@ fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
50095012fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
50105013 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
50115014 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
5012 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);
5015 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, "operand to @setRuntimeSafety must be comptime known");
50135016}
50145017
50155018fn zirFence(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
......@@ -5017,7 +5020,7 @@ fn zirFence(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) Co
50175020
50185021 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
50195022 const order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
5020 const order = try sema.resolveAtomicOrder(block, order_src, extra.operand);
5023 const order = try sema.resolveAtomicOrder(block, order_src, extra.operand, "atomic order of @fence must be comptime known");
50215024
50225025 if (@enumToInt(order) < @enumToInt(std.builtin.AtomicOrder.Acquire)) {
50235026 return sema.fail(block, order_src, "atomic ordering must be Acquire or stricter", .{});
......@@ -5292,7 +5295,7 @@ fn zirCall(
52925295
52935296 // Desugar bound functions here
52945297 if (func_type.tag() == .bound_fn) {
5295 const bound_func = try sema.resolveValue(block, func_src, func);
5298 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
52965299 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
52975300 func = bound_data.func_inst;
52985301 resolved_args = try sema.arena.alloc(Air.Inst.Ref, args.len + 1);
......@@ -5489,7 +5492,8 @@ fn analyzeCall(
54895492 }
54905493
54915494 const result: Air.Inst.Ref = if (is_inline_call) res: {
5492 const func_val = try sema.resolveConstValue(block, func_src, func);
5495 // TODO explain why function is being called at comptime
5496 const func_val = try sema.resolveConstValue(block, func_src, func, "function being called at comptime must be comptime known");
54935497 const module_fn = switch (func_val.tag()) {
54945498 .decl_ref => mod.declPtr(func_val.castTag(.decl_ref).?.data).val.castTag(.function).?.data,
54955499 .function => func_val.castTag(.function).?.data,
......@@ -5606,7 +5610,8 @@ fn analyzeCall(
56065610 try sema.inst_map.putNoClobber(gpa, inst, casted_arg);
56075611
56085612 if (is_comptime_call) {
5609 const arg_val = try sema.resolveConstMaybeUndefVal(&child_block, arg_src, casted_arg);
5613 // TODO explain why function is being called at comptime
5614 const arg_val = try sema.resolveConstMaybeUndefVal(&child_block, arg_src, casted_arg, "argument to function being called at comptime must be comptime known");
56105615 switch (arg_val.tag()) {
56115616 .generic_poison, .generic_poison_type => {
56125617 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -5638,7 +5643,8 @@ fn analyzeCall(
56385643
56395644 if (is_comptime_call) {
56405645 const arg_src = call_src; // TODO: better source location
5641 const arg_val = try sema.resolveConstMaybeUndefVal(&child_block, arg_src, uncasted_arg);
5646 // TODO explain why function is being called at comptime
5647 const arg_val = try sema.resolveConstMaybeUndefVal(&child_block, arg_src, uncasted_arg, "argument to function being called at comptime must be comptime known");
56425648 switch (arg_val.tag()) {
56435649 .generic_poison, .generic_poison_type => {
56445650 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -5764,7 +5770,7 @@ fn analyzeCall(
57645770 }
57655771
57665772 if (should_memoize and is_comptime_call) {
5767 const result_val = try sema.resolveConstMaybeUndefVal(block, call_src, result);
5773 const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, undefined);
57685774
57695775 // TODO: check whether any external comptime memory was mutated by the
57705776 // comptime function call. If so, then do not memoize the call here.
......@@ -5795,7 +5801,7 @@ fn analyzeCall(
57955801 break :res res2;
57965802 } else res: {
57975803 assert(!func_ty_info.is_generic);
5798 try sema.requireRuntimeBlock(block, call_src);
5804 try sema.requireFunctionBlock(block, call_src);
57995805
58005806 const args = try sema.arena.alloc(Air.Inst.Ref, uncasted_args.len);
58015807 for (uncasted_args) |uncasted_arg, i| {
......@@ -5849,7 +5855,7 @@ fn instantiateGenericCall(
58495855 const mod = sema.mod;
58505856 const gpa = sema.gpa;
58515857
5852 const func_val = try sema.resolveConstValue(block, func_src, func);
5858 const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime known");
58535859 const module_fn = switch (func_val.tag()) {
58545860 .function => func_val.castTag(.function).?.data,
58555861 .decl_ref => mod.declPtr(func_val.castTag(.decl_ref).?.data).val.castTag(.function).?.data,
......@@ -5902,7 +5908,7 @@ fn instantiateGenericCall(
59025908 if (is_comptime) {
59035909 const arg_src = call_src; // TODO better source location
59045910 const arg_ty = sema.typeOf(uncasted_args[i]);
5905 const arg_val = try sema.resolveValue(block, arg_src, uncasted_args[i]);
5911 const arg_val = try sema.resolveValue(block, arg_src, uncasted_args[i], "parameter is comptime");
59065912 try sema.resolveLazyValue(block, arg_src, arg_val);
59075913 arg_val.hash(arg_ty, &hasher, mod);
59085914 if (is_anytype) {
......@@ -6066,12 +6072,12 @@ fn instantiateGenericCall(
60666072 const child_arg = try child_sema.addConstant(sema.typeOf(arg), arg_val);
60676073 child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
60686074 } else {
6069 return sema.failWithNeededComptime(block, arg_src);
6075 return sema.failWithNeededComptime(block, arg_src, "parameter is comptime");
60706076 }
60716077 } else if (is_anytype) {
60726078 const arg_ty = sema.typeOf(arg);
60736079 if (try sema.typeRequiresComptime(block, arg_src, arg_ty)) {
6074 const arg_val = try sema.resolveConstValue(block, arg_src, arg);
6080 const arg_val = try sema.resolveConstValue(block, arg_src, arg, "type of anytype parameter requires comptime");
60756081 const child_arg = try child_sema.addConstant(arg_ty, arg_val);
60766082 child_sema.inst_map.putAssumeCapacityNoClobber(inst, child_arg);
60776083 } else {
......@@ -6093,7 +6099,7 @@ fn instantiateGenericCall(
60936099 }
60946100 return err;
60956101 };
6096 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst) catch unreachable;
6102 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, undefined) catch unreachable;
60976103 const new_func = new_func_val.castTag(.function).?.data;
60986104 errdefer new_func.deinit(gpa);
60996105 assert(new_func == new_module_func);
......@@ -6195,7 +6201,7 @@ fn instantiateGenericCall(
61956201 const callee_inst = try sema.analyzeDeclVal(block, func_src, callee.owner_decl);
61966202
61976203 // Make a runtime call to the new function, making sure to omit the comptime args.
6198 try sema.requireRuntimeBlock(block, call_src);
6204 try sema.requireFunctionBlock(block, call_src);
61996205
62006206 const comptime_args = callee.comptime_args.?;
62016207 const new_fn_info = mod.declPtr(callee.owner_decl).ty.fnInfo();
......@@ -6314,7 +6320,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
63146320 const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
63156321 const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
63166322 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
6317 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.u32);
6323 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.u32, "vector length must be comptime known");
63186324 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
63196325 try sema.checkVectorElemType(block, elem_type_src, elem_type);
63206326 const vector_type = try Type.Tag.vector.create(sema.arena, .{
......@@ -6332,7 +6338,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
63326338 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
63336339 const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node };
63346340 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
6335 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize);
6341 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, "array length must be comptime known");
63366342 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
63376343 const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
63386344
......@@ -6348,11 +6354,11 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
63486354 const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node };
63496355 const sentinel_src: LazySrcLoc = .{ .node_offset_array_type_sentinel = inst_data.src_node };
63506356 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
6351 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize);
6357 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, "array length must be comptime known");
63526358 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
63536359 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
63546360 const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
6355 const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel);
6361 const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel, "array sentinel value must be comptime known");
63566362 const array_ty = try Type.array(sema.arena, len, sentinel_val, elem_type, sema.mod);
63576363
63586364 return sema.addType(array_ty);
......@@ -6452,7 +6458,7 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
64526458 }
64536459 }
64546460
6455 try sema.requireRuntimeBlock(block, src);
6461 try sema.requireRuntimeBlock(block, src, operand_src);
64566462 return block.addBitCast(result_ty, operand);
64576463}
64586464
......@@ -6478,7 +6484,7 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
64786484 };
64796485 return sema.addConstant(Type.anyerror, Value.initPayload(&payload.base));
64806486 }
6481 try sema.requireRuntimeBlock(block, src);
6487 try sema.requireRuntimeBlock(block, src, operand_src);
64826488 if (block.wantSafety()) {
64836489 const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
64846490 try sema.addSafetyCheck(block, is_lt_len, .invalid_error_code);
......@@ -6598,7 +6604,7 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
65986604 return sema.addConstant(int_tag_ty, try val.copy(sema.arena));
65996605 }
66006606
6601 try sema.requireRuntimeBlock(block, src);
6607 try sema.requireRuntimeBlock(block, src, operand_src);
66026608 return block.addBitCast(int_tag_ty, enum_tag);
66036609}
66046610
......@@ -6645,7 +6651,7 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
66456651 return sema.addConstant(dest_ty, int_val);
66466652 }
66476653
6648 try sema.requireRuntimeBlock(block, src);
6654 try sema.requireRuntimeBlock(block, src, operand_src);
66496655 // TODO insert safety check to make sure the value matches an enum value
66506656 return block.addTyOp(.intcast, dest_ty, operand);
66516657}
......@@ -6696,7 +6702,7 @@ fn analyzeOptionalPayloadPtr(
66966702 // If the pointer resulting from this function was stored at comptime,
66976703 // the optional non-null bit would be set that way. But in this case,
66986704 // we need to emit a runtime instruction to do it.
6699 try sema.requireRuntimeBlock(block, src);
6705 try sema.requireFunctionBlock(block, src);
67006706 _ = try block.addTyOp(.optional_payload_ptr_set, child_pointer, optional_ptr);
67016707 }
67026708 return sema.addConstant(
......@@ -6722,7 +6728,7 @@ fn analyzeOptionalPayloadPtr(
67226728 }
67236729 }
67246730
6725 try sema.requireRuntimeBlock(block, src);
6731 try sema.requireRuntimeBlock(block, src, null);
67266732 if (safety_check and block.wantSafety()) {
67276733 const is_non_null = try block.addUnOp(.is_non_null_ptr, optional_ptr);
67286734 try sema.addSafetyCheck(block, is_non_null, .unwrap_null);
......@@ -6778,7 +6784,7 @@ fn zirOptionalPayload(
67786784 return sema.addConstant(result_ty, val);
67796785 }
67806786
6781 try sema.requireRuntimeBlock(block, src);
6787 try sema.requireRuntimeBlock(block, src, null);
67826788 if (safety_check and block.wantSafety()) {
67836789 const is_non_null = try block.addUnOp(.is_non_null, operand);
67846790 try sema.addSafetyCheck(block, is_non_null, .unwrap_null);
......@@ -6827,7 +6833,7 @@ fn analyzeErrUnionPayload(
68276833 return sema.addConstant(payload_ty, data);
68286834 }
68296835
6830 try sema.requireRuntimeBlock(block, src);
6836 try sema.requireRuntimeBlock(block, src, null);
68316837
68326838 // If the error set has no fields then no safety check is needed.
68336839 if (safety_check and block.wantSafety() and
......@@ -6887,7 +6893,7 @@ fn analyzeErrUnionPayloadPtr(
68876893 // If the pointer resulting from this function was stored at comptime,
68886894 // the error union error code would be set that way. But in this case,
68896895 // we need to emit a runtime instruction to do it.
6890 try sema.requireRuntimeBlock(block, src);
6896 try sema.requireRuntimeBlock(block, src, null);
68916897 _ = try block.addTyOp(.errunion_payload_ptr_set, operand_pointer_ty, operand);
68926898 }
68936899 return sema.addConstant(
......@@ -6913,7 +6919,7 @@ fn analyzeErrUnionPayloadPtr(
69136919 }
69146920 }
69156921
6916 try sema.requireRuntimeBlock(block, src);
6922 try sema.requireRuntimeBlock(block, src, null);
69176923
69186924 // If the error set has no fields then no safety check is needed.
69196925 if (safety_check and block.wantSafety() and
......@@ -6951,7 +6957,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
69516957 return sema.addConstant(result_ty, val);
69526958 }
69536959
6954 try sema.requireRuntimeBlock(block, src);
6960 try sema.requireRuntimeBlock(block, src, null);
69556961 return block.addTyOp(.unwrap_errunion_err, result_ty, operand);
69566962}
69576963
......@@ -6981,7 +6987,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
69816987 }
69826988 }
69836989
6984 try sema.requireRuntimeBlock(block, src);
6990 try sema.requireRuntimeBlock(block, src, null);
69856991 return block.addTyOp(.unwrap_errunion_err_ptr, result_ty, operand);
69866992}
69876993
......@@ -7037,7 +7043,7 @@ fn zirFunc(
70377043 const ret_ty_body = sema.code.extra[extra_index..][0..extra.data.ret_body_len];
70387044 extra_index += ret_ty_body.len;
70397045
7040 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type);
7046 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, "return type must be comptime known");
70417047 var buffer: Value.ToTypeBuffer = undefined;
70427048 break :blk try ret_ty_val.toType(&buffer).copy(sema.arena);
70437049 },
......@@ -7085,6 +7091,7 @@ fn resolveGenericBody(
70857091 body: []const Zir.Inst.Index,
70867092 func_inst: Zir.Inst.Index,
70877093 dest_ty: Type,
7094 reason: []const u8,
70887095) !Value {
70897096 assert(body.len != 0);
70907097
......@@ -7098,7 +7105,7 @@ fn resolveGenericBody(
70987105 }
70997106 const uncasted = sema.resolveBody(block, body, func_inst) catch |err| break :err err;
71007107 const result = sema.coerce(block, dest_ty, uncasted, src) catch |err| break :err err;
7101 const val = sema.resolveConstValue(block, src, result) catch |err| break :err err;
7108 const val = sema.resolveConstValue(block, src, result, reason) catch |err| break :err err;
71027109 return val;
71037110 };
71047111 switch (err) {
......@@ -7629,7 +7636,7 @@ fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
76297636 if (try sema.resolveMaybeUndefVal(block, ptr_src, ptr)) |ptr_val| {
76307637 return sema.addConstant(Type.usize, ptr_val);
76317638 }
7632 try sema.requireRuntimeBlock(block, ptr_src);
7639 try sema.requireRuntimeBlock(block, ptr_src, ptr_src);
76337640 return block.addUnOp(.ptrtoint, ptr);
76347641}
76357642
......@@ -7681,7 +7688,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
76817688 const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
76827689 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
76837690 const object = try sema.resolveInst(extra.lhs);
7684 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name);
7691 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name, "field name must be comptime known");
76857692 return sema.fieldVal(block, src, object, field_name, field_name_src);
76867693}
76877694
......@@ -7694,7 +7701,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
76947701 const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
76957702 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
76967703 const object_ptr = try sema.resolveInst(extra.lhs);
7697 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name);
7704 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name, "field name must be comptime known");
76987705 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);
76997706}
77007707
......@@ -7706,7 +7713,7 @@ fn zirFieldCallBindNamed(sema: *Sema, block: *Block, extended: Zir.Inst.Extended
77067713 const src = LazySrcLoc.nodeOffset(extra.node);
77077714 const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
77087715 const object_ptr = try sema.resolveInst(extra.lhs);
7709 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name);
7716 const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name, "field name must be comptime known");
77107717 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);
77117718}
77127719
......@@ -7722,12 +7729,13 @@ fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
77227729 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
77237730 const operand = try sema.resolveInst(extra.rhs);
77247731
7725 return sema.intCast(block, dest_ty, dest_ty_src, operand, operand_src, true);
7732 return sema.intCast(block, inst_data.src(), dest_ty, dest_ty_src, operand, operand_src, true);
77267733}
77277734
77287735fn intCast(
77297736 sema: *Sema,
77307737 block: *Block,
7738 src: LazySrcLoc,
77317739 dest_ty: Type,
77327740 dest_ty_src: LazySrcLoc,
77337741 operand: Air.Inst.Ref,
......@@ -7750,7 +7758,7 @@ fn intCast(
77507758 if ((try sema.typeHasOnePossibleValue(block, dest_ty_src, dest_ty))) |opv| {
77517759 // requirement: intCast(u0, input) iff input == 0
77527760 if (runtime_safety and block.wantSafety()) {
7753 try sema.requireRuntimeBlock(block, operand_src);
7761 try sema.requireRuntimeBlock(block, src, operand_src);
77547762 const target = sema.mod.getTarget();
77557763 const wanted_info = dest_scalar_ty.intInfo(target);
77567764 const wanted_bits = wanted_info.bits;
......@@ -7765,7 +7773,7 @@ fn intCast(
77657773 return sema.addConstant(dest_ty, opv);
77667774 }
77677775
7768 try sema.requireRuntimeBlock(block, operand_src);
7776 try sema.requireRuntimeBlock(block, src, operand_src);
77697777 if (runtime_safety and block.wantSafety()) {
77707778 const target = sema.mod.getTarget();
77717779 const actual_info = operand_scalar_ty.intInfo(target);
......@@ -7972,7 +7980,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
79727980 if (dst_bits >= src_bits) {
79737981 return sema.coerce(block, dest_ty, operand, operand_src);
79747982 }
7975 try sema.requireRuntimeBlock(block, operand_src);
7983 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
79767984 return block.addTyOp(.fptrunc, dest_ty, operand);
79777985}
79787986
......@@ -8141,7 +8149,7 @@ fn zirSwitchCapture(
81418149
81428150 const first_item = try sema.resolveInst(items[0]);
81438151 // Previous switch validation ensured this will succeed
8144 const first_item_val = sema.resolveConstValue(block, .unneeded, first_item) catch unreachable;
8152 const first_item_val = sema.resolveConstValue(block, .unneeded, first_item, undefined) catch unreachable;
81458153
81468154 const first_field_index = @intCast(u32, enum_ty.enumTagFieldIndex(first_item_val, sema.mod).?);
81478155 const first_field = union_obj.fields.values()[first_field_index];
......@@ -8149,7 +8157,7 @@ fn zirSwitchCapture(
81498157 for (items[1..]) |item| {
81508158 const item_ref = try sema.resolveInst(item);
81518159 // Previous switch validation ensured this will succeed
8152 const item_val = sema.resolveConstValue(block, .unneeded, item_ref) catch unreachable;
8160 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
81538161
81548162 const field_index = enum_ty.enumTagFieldIndex(item_val, sema.mod).?;
81558163 const field = union_obj.fields.values()[field_index];
......@@ -8186,7 +8194,7 @@ fn zirSwitchCapture(
81868194 }),
81878195 );
81888196 }
8189 try sema.requireRuntimeBlock(block, operand_src);
8197 try sema.requireRuntimeBlock(block, operand_src, null);
81908198 return block.addStructFieldPtr(operand_ptr, first_field_index, field_ty_ptr);
81918199 }
81928200
......@@ -8196,7 +8204,7 @@ fn zirSwitchCapture(
81968204 operand_val.castTag(.@"union").?.data.val,
81978205 );
81988206 }
8199 try sema.requireRuntimeBlock(block, operand_src);
8207 try sema.requireRuntimeBlock(block, operand_src, null);
82008208 return block.addStructFieldVal(operand, first_field_index, first_field.ty);
82018209 },
82028210 .ErrorSet => {
......@@ -8206,7 +8214,7 @@ fn zirSwitchCapture(
82068214 for (items) |item| {
82078215 const item_ref = try sema.resolveInst(item);
82088216 // Previous switch validation ensured this will succeed
8209 const item_val = sema.resolveConstValue(block, .unneeded, item_ref) catch unreachable;
8217 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
82108218 names.putAssumeCapacityNoClobber(
82118219 item_val.getError().?,
82128220 {},
......@@ -8220,7 +8228,7 @@ fn zirSwitchCapture(
82208228 } else {
82218229 const item_ref = try sema.resolveInst(items[0]);
82228230 // Previous switch validation ensured this will succeed
8223 const item_val = sema.resolveConstValue(block, .unneeded, item_ref) catch unreachable;
8231 const item_val = sema.resolveConstValue(block, .unneeded, item_ref, undefined) catch unreachable;
82248232
82258233 const item_ty = try Type.Tag.error_set_single.create(sema.arena, item_val.getError().?);
82268234 return sema.bitCast(block, item_ty, operand, operand_src);
......@@ -8924,7 +8932,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89248932
89258933 const item = try sema.resolveInst(item_ref);
89268934 // Validation above ensured these will succeed.
8927 const item_val = sema.resolveConstValue(&child_block, .unneeded, item) catch unreachable;
8935 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable;
89288936 if (operand_val.eql(item_val, operand_ty, sema.mod)) {
89298937 return sema.resolveBlockBody(block, src, &child_block, body, inst, merges);
89308938 }
......@@ -8946,7 +8954,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89468954 for (items) |item_ref| {
89478955 const item = try sema.resolveInst(item_ref);
89488956 // Validation above ensured these will succeed.
8949 const item_val = sema.resolveConstValue(&child_block, .unneeded, item) catch unreachable;
8957 const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable;
89508958 if (operand_val.eql(item_val, operand_ty, sema.mod)) {
89518959 return sema.resolveBlockBody(block, src, &child_block, body, inst, merges);
89528960 }
......@@ -8960,8 +8968,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89608968 extra_index += 1;
89618969
89628970 // Validation above ensured these will succeed.
8963 const first_tv = sema.resolveInstConst(&child_block, .unneeded, item_first) catch unreachable;
8964 const last_tv = sema.resolveInstConst(&child_block, .unneeded, item_last) catch unreachable;
8971 const first_tv = sema.resolveInstConst(&child_block, .unneeded, item_first, undefined) catch unreachable;
8972 const last_tv = sema.resolveInstConst(&child_block, .unneeded, item_last, undefined) catch unreachable;
89658973 if ((try sema.compare(block, src, operand_val, .gte, first_tv.val, operand_ty)) and
89668974 (try sema.compare(block, src, operand_val, .lte, last_tv.val, operand_ty)))
89678975 {
......@@ -8982,7 +8990,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89828990 return sema.resolveBlockBody(block, src, &child_block, special.body, inst, merges);
89838991 }
89848992
8985 try sema.requireRuntimeBlock(block, src);
8993 try sema.requireRuntimeBlock(block, src, operand_src);
89868994
89878995 const estimated_cases_extra = (scalar_cases_len + multi_cases_len) *
89888996 @typeInfo(Air.SwitchBr.Case).Struct.fields.len + 2;
......@@ -9271,14 +9279,14 @@ fn resolveSwitchItemVal(
92719279 // Constructing a LazySrcLoc is costly because we only have the switch AST node.
92729280 // Only if we know for sure we need to report a compile error do we resolve the
92739281 // full source locations.
9274 if (sema.resolveConstValue(block, .unneeded, item)) |val| {
9282 if (sema.resolveConstValue(block, .unneeded, item, undefined)) |val| {
92759283 return TypedValue{ .ty = item_ty, .val = val };
92769284 } else |err| switch (err) {
92779285 error.NeededSourceLocation => {
92789286 const src = switch_prong_src.resolve(sema.gpa, sema.mod.declPtr(block.src_decl), switch_node_offset, range_expand);
92799287 return TypedValue{
92809288 .ty = item_ty,
9281 .val = try sema.resolveConstValue(block, src, item),
9289 .val = try sema.resolveConstValue(block, src, item, "switch prong values must be comptime known"),
92829290 };
92839291 },
92849292 else => |e| return e,
......@@ -9464,7 +9472,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
94649472 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
94659473 const name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
94669474 const unresolved_ty = try sema.resolveType(block, ty_src, extra.lhs);
9467 const field_name = try sema.resolveConstString(block, name_src, extra.rhs);
9475 const field_name = try sema.resolveConstString(block, name_src, extra.rhs, "field name must be comptime known");
94689476 const ty = try sema.resolveTypeFields(block, ty_src, unresolved_ty);
94699477
94709478 const has_field = hf: {
......@@ -9506,7 +9514,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
95069514 const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
95079515 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
95089516 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
9509 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
9517 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs, "decl name must be comptime known");
95109518
95119519 try checkNamespaceType(sema, block, lhs_src, container_type);
95129520
......@@ -9553,7 +9561,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
95539561 const mod = sema.mod;
95549562 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
95559563 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
9556 const name = try sema.resolveConstString(block, operand_src, inst_data.operand);
9564 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, "file path name must be comptime known");
95579565
95589566 const embed_file = mod.embedFile(block.getFileScope(), name) catch |err| switch (err) {
95599567 error.ImportOutsidePkgPath => {
......@@ -9707,13 +9715,13 @@ fn zirShl(
97079715 try lhs_ty.maxInt(sema.arena, target),
97089716 );
97099717 const rhs_limited = try sema.analyzeMinMax(block, rhs_src, rhs, max_int, .min, rhs_src, rhs_src);
9710 break :rhs try sema.intCast(block, lhs_ty, rhs_src, rhs_limited, rhs_src, false);
9718 break :rhs try sema.intCast(block, src, lhs_ty, rhs_src, rhs_limited, rhs_src, false);
97119719 } else {
97129720 break :rhs rhs;
97139721 }
97149722 } else rhs;
97159723
9716 try sema.requireRuntimeBlock(block, runtime_src);
9724 try sema.requireRuntimeBlock(block, src, runtime_src);
97179725 if (block.wantSafety()) {
97189726 const maybe_op_ov: ?Air.Inst.Tag = switch (air_tag) {
97199727 .shl_exact => .shl_with_overflow,
......@@ -9824,7 +9832,7 @@ fn zirShr(
98249832 }
98259833 } else rhs_src;
98269834
9827 try sema.requireRuntimeBlock(block, runtime_src);
9835 try sema.requireRuntimeBlock(block, src, runtime_src);
98289836 return block.addBinOp(air_tag, lhs, rhs);
98299837}
98309838
......@@ -9883,7 +9891,7 @@ fn zirBitwise(
98839891 }
98849892 };
98859893
9886 try sema.requireRuntimeBlock(block, runtime_src);
9894 try sema.requireRuntimeBlock(block, src, runtime_src);
98879895 return block.addBinOp(air_tag, casted_lhs, casted_rhs);
98889896}
98899897
......@@ -9927,7 +9935,7 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
99279935 }
99289936 }
99299937
9930 try sema.requireRuntimeBlock(block, src);
9938 try sema.requireRuntimeBlock(block, src, null);
99319939 return block.addTyOp(.not, operand_type, operand);
99329940}
99339941
......@@ -9940,6 +9948,7 @@ fn analyzeTupleCat(
99409948) CompileError!Air.Inst.Ref {
99419949 const lhs_ty = sema.typeOf(lhs);
99429950 const rhs_ty = sema.typeOf(rhs);
9951 const src = LazySrcLoc.nodeOffset(src_node);
99439952 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = src_node };
99449953 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = src_node };
99459954
......@@ -9987,7 +9996,7 @@ fn analyzeTupleCat(
99879996 return sema.addConstant(tuple_ty, tuple_val);
99889997 };
99899998
9990 try sema.requireRuntimeBlock(block, runtime_src);
9999 try sema.requireRuntimeBlock(block, src, runtime_src);
999110000
999210001 const element_refs = try sema.arena.alloc(Air.Inst.Ref, final_len);
999310002 for (lhs_tuple.types) |_, i| {
......@@ -10050,8 +10059,8 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1005010059 const rhs_sent = try sema.addConstant(rhs_info.elem_type, rhs_sent_val);
1005110060 const lhs_sent_casted = try sema.coerce(block, resolved_elem_ty, lhs_sent, lhs_src);
1005210061 const rhs_sent_casted = try sema.coerce(block, resolved_elem_ty, rhs_sent, rhs_src);
10053 const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted);
10054 const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted);
10062 const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, "array sentinel value must be comptime known");
10063 const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, "array sentinel value must be comptime known");
1005510064 if (try sema.valuesEqual(block, src, lhs_sent_casted_val, rhs_sent_casted_val, resolved_elem_ty)) {
1005610065 break :s lhs_sent_casted_val;
1005710066 } else {
......@@ -10059,14 +10068,14 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1005910068 }
1006010069 } else {
1006110070 const lhs_sent_casted = try sema.coerce(block, resolved_elem_ty, lhs_sent, lhs_src);
10062 const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted);
10071 const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, "array sentinel value must be comptime known");
1006310072 break :s lhs_sent_casted_val;
1006410073 }
1006510074 } else {
1006610075 if (rhs_info.sentinel) |rhs_sent_val| {
1006710076 const rhs_sent = try sema.addConstant(rhs_info.elem_type, rhs_sent_val);
1006810077 const rhs_sent_casted = try sema.coerce(block, resolved_elem_ty, rhs_sent, rhs_src);
10069 const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted);
10078 const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, "array sentinel value must be comptime known");
1007010079 break :s rhs_sent_casted_val;
1007110080 } else {
1007210081 break :s null;
......@@ -10121,7 +10130,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1012110130 } else break :rs rhs_src;
1012210131 } else lhs_src;
1012310132
10124 try sema.requireRuntimeBlock(block, runtime_src);
10133 try sema.requireRuntimeBlock(block, src, runtime_src);
1012510134
1012610135 if (ptr_addrspace) |ptr_as| {
1012710136 const alloc_ty = try Type.ptr(sema.arena, sema.mod, .{
......@@ -10187,7 +10196,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins
1018710196 // has a sentinel, and this code should compute the length based
1018810197 // on the sentinel value.
1018910198 .Slice, .Many => {
10190 const val = try sema.resolveConstValue(block, src, operand);
10199 const val = try sema.resolveConstValue(block, src, operand, "slice value being concatenated must be comptime known");
1019110200 return Type.ArrayInfo{
1019210201 .elem_type = ptr_info.pointee_type,
1019310202 .sentinel = ptr_info.sentinel,
......@@ -10216,6 +10225,7 @@ fn analyzeTupleMul(
1021610225) CompileError!Air.Inst.Ref {
1021710226 const operand_ty = sema.typeOf(operand);
1021810227 const operand_tuple = operand_ty.tupleFields();
10228 const src = LazySrcLoc.nodeOffset(src_node);
1021910229 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = src_node };
1022010230 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = src_node };
1022110231
......@@ -10259,7 +10269,7 @@ fn analyzeTupleMul(
1025910269 return sema.addConstant(tuple_ty, tuple_val);
1026010270 };
1026110271
10262 try sema.requireRuntimeBlock(block, runtime_src);
10272 try sema.requireRuntimeBlock(block, src, runtime_src);
1026310273
1026410274 const element_refs = try sema.arena.alloc(Air.Inst.Ref, final_len);
1026510275 for (operand_tuple.types) |_, i| {
......@@ -10287,7 +10297,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1028710297 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1028810298
1028910299 // In `**` rhs must be comptime-known, but lhs can be runtime-known
10290 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize);
10300 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, "array multiplication factor must be comptime known");
1029110301
1029210302 if (lhs_ty.isTuple()) {
1029310303 return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor);
......@@ -10338,7 +10348,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1033810348 return sema.addConstantMaybeRef(block, src, result_ty, val, ptr_addrspace != null);
1033910349 }
1034010350
10341 try sema.requireRuntimeBlock(block, lhs_src);
10351 try sema.requireRuntimeBlock(block, src, lhs_src);
1034210352
1034310353 if (ptr_addrspace) |ptr_as| {
1034410354 const alloc_ty = try Type.ptr(sema.arena, sema.mod, .{
......@@ -10412,7 +10422,7 @@ fn zirNegate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1041210422 const target = sema.mod.getTarget();
1041310423 return sema.addConstant(rhs_ty, try rhs_val.floatNeg(rhs_ty, sema.arena, target));
1041410424 }
10415 try sema.requireRuntimeBlock(block, rhs_src);
10425 try sema.requireRuntimeBlock(block, src, null);
1041610426 return block.addUnOp(.neg, rhs);
1041710427 }
1041810428
......@@ -10636,7 +10646,8 @@ fn zirOverflowArithmetic(
1063610646 else => unreachable,
1063710647 };
1063810648
10639 try sema.requireRuntimeBlock(block, src);
10649 const runtime_src = if (maybe_lhs_val == null) lhs_src else rhs_src;
10650 try sema.requireRuntimeBlock(block, src, runtime_src);
1064010651
1064110652 const tuple = try block.addInst(.{
1064210653 .tag = air_tag,
......@@ -11543,7 +11554,7 @@ fn analyzeArithmetic(
1154311554 }
1154411555 };
1154511556
11546 try sema.requireRuntimeBlock(block, rs.src);
11557 try sema.requireRuntimeBlock(block, src, rs.src);
1154711558 if (block.wantSafety()) {
1154811559 if (scalar_tag == .Int) {
1154911560 const maybe_op_ov: ?Air.Inst.Tag = switch (rs.air_tag) {
......@@ -11667,7 +11678,7 @@ fn analyzePtrArithmetic(
1166711678 } else break :rs ptr_src;
1166811679 };
1166911680
11670 try sema.requireRuntimeBlock(block, runtime_src);
11681 try sema.requireRuntimeBlock(block, op_src, runtime_src);
1167111682 return block.addInst(.{
1167211683 .tag = air_tag,
1167311684 .data = .{ .ty_pl = .{
......@@ -11686,7 +11697,7 @@ fn zirLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.In
1168611697
1168711698 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1168811699 const src = inst_data.src();
11689 const ptr_src: LazySrcLoc = .{ .node_offset_deref_ptr = inst_data.src_node };
11700 const ptr_src = src; // TODO better source location
1169011701 const ptr = try sema.resolveInst(inst_data.operand);
1169111702 return sema.analyzeLoad(block, src, ptr, ptr_src);
1169211703}
......@@ -11734,7 +11745,7 @@ fn zirAsm(
1173411745 }
1173511746
1173611747 if (block.is_comptime) {
11737 try sema.requireRuntimeBlock(block, src);
11748 try sema.requireRuntimeBlock(block, src, null);
1173811749 }
1173911750
1174011751 var extra_i = extra.end;
......@@ -11896,10 +11907,10 @@ fn zirCmpEq(
1189611907 }
1189711908
1189811909 if (lhs_ty_tag == .Union and (rhs_ty_tag == .EnumLiteral or rhs_ty_tag == .Enum)) {
11899 return sema.analyzeCmpUnionTag(block, lhs, lhs_src, rhs, rhs_src, op);
11910 return sema.analyzeCmpUnionTag(block, src, lhs, lhs_src, rhs, rhs_src, op);
1190011911 }
1190111912 if (rhs_ty_tag == .Union and (lhs_ty_tag == .EnumLiteral or lhs_ty_tag == .Enum)) {
11902 return sema.analyzeCmpUnionTag(block, rhs, rhs_src, lhs, lhs_src, op);
11913 return sema.analyzeCmpUnionTag(block, src, rhs, rhs_src, lhs, lhs_src, op);
1190311914 }
1190411915
1190511916 if (lhs_ty_tag == .ErrorSet and rhs_ty_tag == .ErrorSet) {
......@@ -11926,7 +11937,7 @@ fn zirCmpEq(
1192611937 break :src lhs_src;
1192711938 }
1192811939 };
11929 try sema.requireRuntimeBlock(block, runtime_src);
11940 try sema.requireRuntimeBlock(block, src, runtime_src);
1193011941 return block.addBinOp(air_tag, lhs, rhs);
1193111942 }
1193211943 if (lhs_ty_tag == .Type and rhs_ty_tag == .Type) {
......@@ -11944,6 +11955,7 @@ fn zirCmpEq(
1194411955fn analyzeCmpUnionTag(
1194511956 sema: *Sema,
1194611957 block: *Block,
11958 src: LazySrcLoc,
1194711959 un: Air.Inst.Ref,
1194811960 un_src: LazySrcLoc,
1194911961 tag: Air.Inst.Ref,
......@@ -11965,7 +11977,7 @@ fn analyzeCmpUnionTag(
1196511977 const coerced_tag = try sema.coerce(block, union_tag_ty, tag, tag_src);
1196611978 const coerced_union = try sema.coerce(block, union_tag_ty, un, un_src);
1196711979
11968 return sema.cmpSelf(block, coerced_union, coerced_tag, op, un_src, tag_src);
11980 return sema.cmpSelf(block, src, coerced_union, coerced_tag, op, un_src, tag_src);
1196911981}
1197011982
1197111983/// Only called for non-equality operators. See also `zirCmpEq`.
......@@ -12021,7 +12033,7 @@ fn analyzeCmp(
1202112033 }
1202212034 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);
1202312035 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);
12024 return sema.cmpSelf(block, casted_lhs, casted_rhs, op, lhs_src, rhs_src);
12036 return sema.cmpSelf(block, src, casted_lhs, casted_rhs, op, lhs_src, rhs_src);
1202512037}
1202612038
1202712039fn compareOperatorName(comp: std.math.CompareOperator) []const u8 {
......@@ -12038,6 +12050,7 @@ fn compareOperatorName(comp: std.math.CompareOperator) []const u8 {
1203812050fn cmpSelf(
1203912051 sema: *Sema,
1204012052 block: *Block,
12053 src: LazySrcLoc,
1204112054 casted_lhs: Air.Inst.Ref,
1204212055 casted_rhs: Air.Inst.Ref,
1204312056 op: std.math.CompareOperator,
......@@ -12065,7 +12078,7 @@ fn cmpSelf(
1206512078 } else {
1206612079 if (resolved_type.zigTypeTag() == .Bool) {
1206712080 // We can lower bool eq/neq more efficiently.
12068 return sema.runtimeBoolCmp(block, op, casted_rhs, lhs_val.toBool(), rhs_src);
12081 return sema.runtimeBoolCmp(block, src, op, casted_rhs, lhs_val.toBool(), rhs_src);
1206912082 }
1207012083 break :src rhs_src;
1207112084 }
......@@ -12075,13 +12088,13 @@ fn cmpSelf(
1207512088 if (resolved_type.zigTypeTag() == .Bool) {
1207612089 if (try sema.resolveMaybeUndefVal(block, rhs_src, casted_rhs)) |rhs_val| {
1207712090 if (rhs_val.isUndef()) return sema.addConstUndef(Type.bool);
12078 return sema.runtimeBoolCmp(block, op, casted_lhs, rhs_val.toBool(), lhs_src);
12091 return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src);
1207912092 }
1208012093 }
1208112094 break :src lhs_src;
1208212095 }
1208312096 };
12084 try sema.requireRuntimeBlock(block, runtime_src);
12097 try sema.requireRuntimeBlock(block, src, runtime_src);
1208512098 if (resolved_type.zigTypeTag() == .Vector) {
1208612099 const result_ty = try Type.vector(sema.arena, resolved_type.vectorLen(), Type.@"bool");
1208712100 const result_ty_ref = try sema.addType(result_ty);
......@@ -12098,13 +12111,14 @@ fn cmpSelf(
1209812111fn runtimeBoolCmp(
1209912112 sema: *Sema,
1210012113 block: *Block,
12114 src: LazySrcLoc,
1210112115 op: std.math.CompareOperator,
1210212116 lhs: Air.Inst.Ref,
1210312117 rhs: bool,
1210412118 runtime_src: LazySrcLoc,
1210512119) CompileError!Air.Inst.Ref {
1210612120 if ((op == .neq) == rhs) {
12107 try sema.requireRuntimeBlock(block, runtime_src);
12121 try sema.requireRuntimeBlock(block, src, runtime_src);
1210812122 return block.addTyOp(.not, Type.bool, lhs);
1210912123 } else {
1211012124 return lhs;
......@@ -12226,7 +12240,7 @@ fn zirRetAddr(
1222612240 extended: Zir.Inst.Extended.InstData,
1222712241) CompileError!Air.Inst.Ref {
1222812242 const src = LazySrcLoc.nodeOffset(@bitCast(i32, extended.operand));
12229 try sema.requireRuntimeBlock(block, src);
12243 try sema.requireRuntimeBlock(block, src, null);
1223012244 return try block.addNoOp(.ret_addr);
1223112245}
1223212246
......@@ -12236,7 +12250,7 @@ fn zirFrameAddress(
1223612250 extended: Zir.Inst.Extended.InstData,
1223712251) CompileError!Air.Inst.Ref {
1223812252 const src = LazySrcLoc.nodeOffset(@bitCast(i32, extended.operand));
12239 try sema.requireRuntimeBlock(block, src);
12253 try sema.requireRuntimeBlock(block, src, null);
1224012254 return try block.addNoOp(.frame_addr);
1224112255}
1224212256
......@@ -13305,7 +13319,7 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1330513319 else
1330613320 Air.Inst.Ref.bool_true;
1330713321 }
13308 try sema.requireRuntimeBlock(block, src);
13322 try sema.requireRuntimeBlock(block, src, null);
1330913323 return block.addTyOp(.not, Type.bool, operand);
1331013324}
1331113325
......@@ -13690,7 +13704,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1369013704 if (block.is_comptime or inst_data.force_comptime) {
1369113705 return sema.fail(block, src, "reached unreachable code", .{});
1369213706 }
13693 try sema.requireRuntimeBlock(block, src);
13707 try sema.requireFunctionBlock(block, src);
1369413708 // TODO Add compile error for @optimizeFor occurring too late in a scope.
1369513709 try block.addUnreachable(src, true);
1369613710 return always_noreturn;
......@@ -13752,7 +13766,6 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
1375213766 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
1375313767 return sema.analyzeRet(block, operand, src);
1375413768 }
13755 try sema.requireRuntimeBlock(block, src);
1375613769 _ = try block.addUnOp(.ret_load, ret_ptr);
1375713770 return always_noreturn;
1375813771}
......@@ -13874,14 +13887,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1387413887 const sentinel = if (inst_data.flags.has_sentinel) blk: {
1387513888 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1387613889 extra_i += 1;
13877 break :blk (try sema.resolveInstConst(block, sentinel_src, ref)).val;
13890 break :blk (try sema.resolveInstConst(block, sentinel_src, ref, "pointer sentinel value must be comptime known")).val;
1387813891 } else null;
1387913892
1388013893 const abi_align: u32 = if (inst_data.flags.has_align) blk: {
1388113894 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1388213895 extra_i += 1;
1388313896 const coerced = try sema.coerce(block, Type.u32, try sema.resolveInst(ref), align_src);
13884 const val = try sema.resolveConstValue(block, align_src, coerced);
13897 const val = try sema.resolveConstValue(block, align_src, coerced, "pointer alignment must be comptime known");
1388513898 // Check if this happens to be the lazy alignment of our element type, in
1388613899 // which case we can make this 0 without resolving it.
1388713900 if (val.castTag(.lazy_align)) |payload| {
......@@ -13902,14 +13915,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1390213915 const bit_offset = if (inst_data.flags.has_bit_range) blk: {
1390313916 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1390413917 extra_i += 1;
13905 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16);
13918 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, "pointer bit-offset must be comptime known");
1390613919 break :blk @intCast(u16, bit_offset);
1390713920 } else 0;
1390813921
1390913922 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
1391013923 const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
1391113924 extra_i += 1;
13912 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16);
13925 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, "pointer host size must be comptime known");
1391313926 break :blk @intCast(u16, host_size);
1391413927 } else 0;
1391513928
......@@ -14014,7 +14027,7 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1401414027 const init_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
1401514028 const extra = sema.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data;
1401614029 const union_ty = try sema.resolveType(block, ty_src, extra.union_type);
14017 const field_name = try sema.resolveConstString(block, field_src, extra.field_name);
14030 const field_name = try sema.resolveConstString(block, field_src, extra.field_name, "name of field being initialized must be comptime known");
1401814031 const init = try sema.resolveInst(extra.init);
1401914032 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);
1402014033}
......@@ -14041,7 +14054,7 @@ fn unionInit(
1404114054 }));
1404214055 }
1404314056
14044 try sema.requireRuntimeBlock(block, init_src);
14057 try sema.requireRuntimeBlock(block, init_src, null);
1404514058 _ = union_ty_src;
1404614059 try sema.queueFullTypeResolution(union_ty);
1404714060 return block.addUnionInit(union_ty, field_index, init);
......@@ -14146,7 +14159,7 @@ fn zirStructInit(
1414614159 return alloc;
1414714160 }
1414814161
14149 try sema.requireRuntimeBlock(block, src);
14162 try sema.requireRuntimeBlock(block, src, null);
1415014163 try sema.queueFullTypeResolution(resolved_ty);
1415114164 return block.addUnionInit(resolved_ty, field_index, init_inst);
1415214165 } else if (resolved_ty.isAnonStruct()) {
......@@ -14251,7 +14264,7 @@ fn finishStructInit(
1425114264 return alloc;
1425214265 }
1425314266
14254 try sema.requireRuntimeBlock(block, dest_src);
14267 try sema.requireRuntimeBlock(block, dest_src, null);
1425514268 try sema.queueFullTypeResolution(struct_ty);
1425614269 return block.addAggregateInit(struct_ty, field_inits);
1425714270}
......@@ -14301,7 +14314,7 @@ fn zirStructInitAnon(
1430114314 return sema.addConstantMaybeRef(block, src, tuple_ty, tuple_val, is_ref);
1430214315 };
1430314316
14304 try sema.requireRuntimeBlock(block, runtime_src);
14317 try sema.requireRuntimeBlock(block, src, runtime_src);
1430514318
1430614319 if (is_ref) {
1430714320 const target = sema.mod.getTarget();
......@@ -14393,7 +14406,7 @@ fn zirArrayInit(
1439314406 return sema.addConstantMaybeRef(block, src, array_ty, array_val, is_ref);
1439414407 };
1439514408
14396 try sema.requireRuntimeBlock(block, runtime_src);
14409 try sema.requireRuntimeBlock(block, src, runtime_src);
1439714410 try sema.queueFullTypeResolution(array_ty);
1439814411
1439914412 if (is_ref) {
......@@ -14479,7 +14492,7 @@ fn zirArrayInitAnon(
1447914492 return sema.addConstantMaybeRef(block, src, tuple_ty, tuple_val, is_ref);
1448014493 };
1448114494
14482 try sema.requireRuntimeBlock(block, runtime_src);
14495 try sema.requireRuntimeBlock(block, src, runtime_src);
1448314496
1448414497 if (is_ref) {
1448514498 const target = sema.mod.getTarget();
......@@ -14538,7 +14551,7 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
1453814551 const ty_src = inst_data.src();
1453914552 const field_src = inst_data.src();
1454014553 const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type);
14541 const field_name = try sema.resolveConstString(block, field_src, extra.field_name);
14554 const field_name = try sema.resolveConstString(block, field_src, extra.field_name, "field name must be comptime known");
1454214555 return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);
1454314556}
1454414557
......@@ -14728,7 +14741,7 @@ fn zirUnaryMath(
1472814741 );
1472914742 }
1473014743
14731 try sema.requireRuntimeBlock(block, operand_src);
14744 try sema.requireRuntimeBlock(block, operand_src, null);
1473214745 return block.addUnOp(air_tag, operand);
1473314746 },
1473414747 .ComptimeFloat, .Float => {
......@@ -14739,7 +14752,7 @@ fn zirUnaryMath(
1473914752 return sema.addConstant(operand_ty, result_val);
1474014753 }
1474114754
14742 try sema.requireRuntimeBlock(block, operand_src);
14755 try sema.requireRuntimeBlock(block, operand_src, null);
1474314756 return block.addUnOp(air_tag, operand);
1474414757 },
1474514758 else => unreachable,
......@@ -14757,7 +14770,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1475714770 try sema.resolveTypeLayout(block, operand_src, operand_ty);
1475814771 const enum_ty = switch (operand_ty.zigTypeTag()) {
1475914772 .EnumLiteral => {
14760 const val = try sema.resolveConstValue(block, operand_src, operand);
14773 const val = try sema.resolveConstValue(block, .unneeded, operand, undefined);
1476114774 const bytes = val.castTag(.enum_literal).?.data;
1476214775 return sema.addStrLit(block, bytes);
1476314776 },
......@@ -14809,7 +14822,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1480914822 const uncasted_operand = try sema.resolveInst(inst_data.operand);
1481014823 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1481114824 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
14812 const val = try sema.resolveConstValue(block, operand_src, type_info);
14825 const val = try sema.resolveConstValue(block, operand_src, type_info, "operand to @Type must be comptime known");
1481314826 const union_val = val.cast(Value.Payload.Union).?.data;
1481414827 const tag_ty = type_info_ty.unionTagType().?;
1481514828 const target = mod.getTarget();
......@@ -15493,10 +15506,10 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1549315506 const result_val = try sema.floatToInt(block, operand_src, val, operand_ty, dest_ty);
1549415507 return sema.addConstant(dest_ty, result_val);
1549515508 } else if (dest_ty.zigTypeTag() == .ComptimeInt) {
15496 return sema.failWithNeededComptime(block, operand_src);
15509 return sema.failWithNeededComptime(block, operand_src, "value being casted to 'comptime_int' must be comptime known");
1549715510 }
1549815511
15499 try sema.requireRuntimeBlock(block, operand_src);
15512 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
1550015513 return block.addTyOp(.float_to_int, dest_ty, operand);
1550115514}
1550215515
......@@ -15517,10 +15530,10 @@ fn zirIntToFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1551715530 const result_val = try val.intToFloat(sema.arena, operand_ty, dest_ty, target);
1551815531 return sema.addConstant(dest_ty, result_val);
1551915532 } else if (dest_ty.zigTypeTag() == .ComptimeFloat) {
15520 return sema.failWithNeededComptime(block, operand_src);
15533 return sema.failWithNeededComptime(block, operand_src, "value being casted to 'comptime_float' must be comptime known");
1552115534 }
1552215535
15523 try sema.requireRuntimeBlock(block, operand_src);
15536 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
1552415537 return block.addTyOp(.int_to_float, dest_ty, operand);
1552515538}
1552615539
......@@ -15556,7 +15569,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1555615569 return sema.addConstant(type_res, Value.initPayload(&val_payload.base));
1555715570 }
1555815571
15559 try sema.requireRuntimeBlock(block, src);
15572 try sema.requireRuntimeBlock(block, src, operand_src);
1556015573 if (block.wantSafety()) {
1556115574 if (!type_res.isAllowzeroPtr()) {
1556215575 const is_non_zero = try block.addBinOp(.cmp_neq, operand_coerced, .zero_usize);
......@@ -15653,7 +15666,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1565315666 return sema.addConstant(dest_ty, val);
1565415667 }
1565515668
15656 try sema.requireRuntimeBlock(block, src);
15669 try sema.requireRuntimeBlock(block, src, operand_src);
1565715670 if (block.wantSafety() and !dest_ty.isAnyError()) {
1565815671 const err_int_inst = try block.addBitCast(Type.u16, operand);
1565915672 // TODO: Output a switch instead of chained OR's.
......@@ -15808,7 +15821,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1580815821 );
1580915822 }
1581015823
15811 try sema.requireRuntimeBlock(block, src);
15824 try sema.requireRuntimeBlock(block, src, operand_src);
1581215825 return block.addTyOp(.trunc, dest_ty, operand);
1581315826}
1581415827
......@@ -15851,6 +15864,7 @@ fn zirBitCount(
1585115864 comptimeOp: fn (val: Value, ty: Type, target: std.Target) u64,
1585215865) CompileError!Air.Inst.Ref {
1585315866 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15867 const src = inst_data.src();
1585415868 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
1585515869 const operand = try sema.resolveInst(inst_data.operand);
1585615870 const operand_ty = sema.typeOf(operand);
......@@ -15883,7 +15897,7 @@ fn zirBitCount(
1588315897 try Value.Tag.aggregate.create(sema.arena, elems),
1588415898 );
1588515899 } else {
15886 try sema.requireRuntimeBlock(block, operand_src);
15900 try sema.requireRuntimeBlock(block, src, operand_src);
1588715901 return block.addTyOp(air_tag, result_ty, operand);
1588815902 }
1588915903 },
......@@ -15892,7 +15906,7 @@ fn zirBitCount(
1589215906 if (val.isUndef()) return sema.addConstUndef(result_scalar_ty);
1589315907 return sema.addIntUnsigned(result_scalar_ty, comptimeOp(val, operand_ty, target));
1589415908 } else {
15895 try sema.requireRuntimeBlock(block, operand_src);
15909 try sema.requireRuntimeBlock(block, src, operand_src);
1589615910 return block.addTyOp(air_tag, result_scalar_ty, operand);
1589715911 }
1589815912 },
......@@ -15902,6 +15916,7 @@ fn zirBitCount(
1590215916
1590315917fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1590415918 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15919 const src = inst_data.src();
1590515920 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1590615921 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
1590715922 const operand = try sema.resolveInst(inst_data.operand);
......@@ -15930,7 +15945,7 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1593015945 return sema.addConstant(operand_ty, result_val);
1593115946 } else operand_src;
1593215947
15933 try sema.requireRuntimeBlock(block, runtime_src);
15948 try sema.requireRuntimeBlock(block, src, runtime_src);
1593415949 return block.addTyOp(.byte_swap, operand_ty, operand);
1593515950 },
1593615951 .Vector => {
......@@ -15951,7 +15966,7 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1595115966 );
1595215967 } else operand_src;
1595315968
15954 try sema.requireRuntimeBlock(block, runtime_src);
15969 try sema.requireRuntimeBlock(block, src, runtime_src);
1595515970 return block.addTyOp(.byte_swap, operand_ty, operand);
1595615971 },
1595715972 else => unreachable,
......@@ -15960,6 +15975,7 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1596015975
1596115976fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1596215977 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15978 const src = inst_data.src();
1596315979 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
1596415980 const operand = try sema.resolveInst(inst_data.operand);
1596515981 const operand_ty = sema.typeOf(operand);
......@@ -15978,7 +15994,7 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1597815994 return sema.addConstant(operand_ty, result_val);
1597915995 } else operand_src;
1598015996
15981 try sema.requireRuntimeBlock(block, runtime_src);
15997 try sema.requireRuntimeBlock(block, src, runtime_src);
1598215998 return block.addTyOp(.bit_reverse, operand_ty, operand);
1598315999 },
1598416000 .Vector => {
......@@ -15999,7 +16015,7 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1599916015 );
1600016016 } else operand_src;
1600116017
16002 try sema.requireRuntimeBlock(block, runtime_src);
16018 try sema.requireRuntimeBlock(block, src, runtime_src);
1600316019 return block.addTyOp(.bit_reverse, operand_ty, operand);
1600416020 },
1600516021 else => unreachable,
......@@ -16026,7 +16042,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
1602616042 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1602716043
1602816044 const ty = try sema.resolveType(block, lhs_src, extra.lhs);
16029 const field_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
16045 const field_name = try sema.resolveConstString(block, rhs_src, extra.rhs, "name of field must be comptime known");
1603016046 const target = sema.mod.getTarget();
1603116047
1603216048 try sema.resolveTypeLayout(block, lhs_src, ty);
......@@ -16447,19 +16463,19 @@ fn resolveExportOptions(
1644716463 const options = try sema.coerce(block, export_options_ty, air_ref, src);
1644816464
1644916465 const name_operand = try sema.fieldVal(block, src, options, "name", src);
16450 const name_val = try sema.resolveConstValue(block, src, name_operand);
16466 const name_val = try sema.resolveConstValue(block, src, name_operand, "name of exported value must be comptime known");
1645116467 const name_ty = Type.initTag(.const_slice_u8);
1645216468 const name = try name_val.toAllocatedBytes(name_ty, sema.arena, sema.mod);
1645316469
1645416470 const linkage_operand = try sema.fieldVal(block, src, options, "linkage", src);
16455 const linkage_val = try sema.resolveConstValue(block, src, linkage_operand);
16471 const linkage_val = try sema.resolveConstValue(block, src, linkage_operand, "linkage of exported value must be comptime known");
1645616472 const linkage = linkage_val.toEnum(std.builtin.GlobalLinkage);
1645716473
1645816474 const section = try sema.fieldVal(block, src, options, "section", src);
16459 const section_val = try sema.resolveConstValue(block, src, section);
16475 const section_val = try sema.resolveConstValue(block, src, section, "linksection of exported value must be comptime known");
1646016476
1646116477 const visibility_operand = try sema.fieldVal(block, src, options, "visibility", src);
16462 const visibility_val = try sema.resolveConstValue(block, src, visibility_operand);
16478 const visibility_val = try sema.resolveConstValue(block, src, visibility_operand, "visibility of exported value must be comptime known");
1646316479 const visibility = visibility_val.toEnum(std.builtin.SymbolVisibility);
1646416480
1646516481 if (name.len < 1) {
......@@ -16490,11 +16506,12 @@ fn resolveBuiltinEnum(
1649016506 src: LazySrcLoc,
1649116507 zir_ref: Zir.Inst.Ref,
1649216508 comptime name: []const u8,
16509 reason: []const u8,
1649316510) CompileError!@field(std.builtin, name) {
1649416511 const ty = try sema.getBuiltinType(block, src, name);
1649516512 const air_ref = try sema.resolveInst(zir_ref);
1649616513 const coerced = try sema.coerce(block, ty, air_ref, src);
16497 const val = try sema.resolveConstValue(block, src, coerced);
16514 const val = try sema.resolveConstValue(block, src, coerced, reason);
1649816515 return val.toEnum(@field(std.builtin, name));
1649916516}
1650016517
......@@ -16503,8 +16520,9 @@ fn resolveAtomicOrder(
1650316520 block: *Block,
1650416521 src: LazySrcLoc,
1650516522 zir_ref: Zir.Inst.Ref,
16523 reason: []const u8,
1650616524) CompileError!std.builtin.AtomicOrder {
16507 return resolveBuiltinEnum(sema, block, src, zir_ref, "AtomicOrder");
16525 return resolveBuiltinEnum(sema, block, src, zir_ref, "AtomicOrder", reason);
1650816526}
1650916527
1651016528fn resolveAtomicRmwOp(
......@@ -16513,7 +16531,7 @@ fn resolveAtomicRmwOp(
1651316531 src: LazySrcLoc,
1651416532 zir_ref: Zir.Inst.Ref,
1651516533) CompileError!std.builtin.AtomicRmwOp {
16516 return resolveBuiltinEnum(sema, block, src, zir_ref, "AtomicRmwOp");
16534 return resolveBuiltinEnum(sema, block, src, zir_ref, "AtomicRmwOp", "@atomicRmW operation must be comptime known");
1651716535}
1651816536
1651916537fn zirCmpxchg(
......@@ -16546,8 +16564,8 @@ fn zirCmpxchg(
1654616564 const uncasted_ptr = try sema.resolveInst(extra.ptr);
1654716565 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
1654816566 const new_value = try sema.coerce(block, elem_ty, try sema.resolveInst(extra.new_value), new_value_src);
16549 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order);
16550 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order);
16567 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, "atomic order of cmpxchg success must be comptime known");
16568 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, "atomic order of cmpxchg failure must be comptime known");
1655116569
1655216570 if (@enumToInt(success_order) < @enumToInt(std.builtin.AtomicOrder.Monotonic)) {
1655316571 return sema.fail(block, success_order_src, "success atomic ordering must be Monotonic or stricter", .{});
......@@ -16592,7 +16610,7 @@ fn zirCmpxchg(
1659216610 const flags: u32 = @as(u32, @enumToInt(success_order)) |
1659316611 (@as(u32, @enumToInt(failure_order)) << 3);
1659416612
16595 try sema.requireRuntimeBlock(block, runtime_src);
16613 try sema.requireRuntimeBlock(block, src, runtime_src);
1659616614 return block.addInst(.{
1659716615 .tag = air_tag,
1659816616 .data = .{ .ty_pl = .{
......@@ -16612,7 +16630,7 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1661216630 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1661316631 const len_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1661416632 const scalar_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
16615 const len = @intCast(u32, try sema.resolveInt(block, len_src, extra.lhs, Type.u32));
16633 const len = @intCast(u32, try sema.resolveInt(block, len_src, extra.lhs, Type.u32, "vector splat destination length must be comptime known"));
1661616634 const scalar = try sema.resolveInst(extra.rhs);
1661716635 const scalar_ty = sema.typeOf(scalar);
1661816636 try sema.checkVectorElemType(block, scalar_src, scalar_ty);
......@@ -16629,7 +16647,7 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1662916647 );
1663016648 }
1663116649
16632 try sema.requireRuntimeBlock(block, scalar_src);
16650 try sema.requireRuntimeBlock(block, inst_data.src(), scalar_src);
1663316651 return block.addTyOp(.splat, vector_ty, scalar);
1663416652}
1663516653
......@@ -16638,7 +16656,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1663816656 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1663916657 const op_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1664016658 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
16641 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp");
16659 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", "@reduce operation must be comptime known");
1664216660 const operand = try sema.resolveInst(extra.rhs);
1664316661 const operand_ty = sema.typeOf(operand);
1664416662 const target = sema.mod.getTarget();
......@@ -16693,7 +16711,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1669316711 return sema.addConstant(scalar_ty, accum);
1669416712 }
1669516713
16696 try sema.requireRuntimeBlock(block, operand_src);
16714 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
1669716715 return block.addInst(.{
1669816716 .tag = .reduce,
1669916717 .data = .{ .reduce = .{
......@@ -16725,7 +16743,7 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1672516743 .elem_type = Type.@"i32",
1672616744 });
1672716745 mask = try sema.coerce(block, mask_ty, mask, mask_src);
16728 const mask_val = try sema.resolveConstMaybeUndefVal(block, mask_src, mask);
16746 const mask_val = try sema.resolveConstMaybeUndefVal(block, mask_src, mask, "shuffle mask must be comptime known");
1672916747 return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(u32, mask_len));
1673016748}
1673116749
......@@ -16897,6 +16915,7 @@ fn analyzeShuffle(
1689716915fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
1689816916 const extra = sema.code.extraData(Zir.Inst.Select, extended.operand).data;
1689916917
16918 const src = LazySrcLoc.nodeOffset(extra.node);
1690016919 const elem_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1690116920 const pred_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
1690216921 const a_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
......@@ -16968,7 +16987,7 @@ fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C
1696816987 break :rs pred_src;
1696916988 };
1697016989
16971 try sema.requireRuntimeBlock(block, runtime_src);
16990 try sema.requireRuntimeBlock(block, src, runtime_src);
1697216991 return block.addInst(.{
1697316992 .tag = .select,
1697416993 .data = .{ .pl_op = .{
......@@ -16992,7 +17011,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1699217011 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
1699317012 const uncasted_ptr = try sema.resolveInst(extra.ptr);
1699417013 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, true);
16995 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);
17014 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicLoad must be comptime known");
1699617015
1699717016 switch (order) {
1699817017 .Release, .AcqRel => {
......@@ -17016,7 +17035,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1701617035 }
1701717036 }
1701817037
17019 try sema.requireRuntimeBlock(block, ptr_src);
17038 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src);
1702017039 return block.addInst(.{
1702117040 .tag = .atomic_load,
1702217041 .data = .{ .atomic_load = .{
......@@ -17056,7 +17075,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1705617075 },
1705717076 else => {},
1705817077 }
17059 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);
17078 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicRmW must be comptime known");
1706017079
1706117080 if (order == .Unordered) {
1706217081 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be Unordered", .{});
......@@ -17097,7 +17116,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1709717116
1709817117 const flags: u32 = @as(u32, @enumToInt(order)) | (@as(u32, @enumToInt(op)) << 3);
1709917118
17100 try sema.requireRuntimeBlock(block, runtime_src);
17119 try sema.requireRuntimeBlock(block, src, runtime_src);
1710117120 return block.addInst(.{
1710217121 .tag = .atomic_rmw,
1710317122 .data = .{ .pl_op = .{
......@@ -17124,7 +17143,7 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1712417143 const elem_ty = sema.typeOf(operand);
1712517144 const uncasted_ptr = try sema.resolveInst(extra.ptr);
1712617145 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
17127 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);
17146 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicStore must be comptime known");
1712817147
1712917148 const air_tag: Air.Inst.Tag = switch (order) {
1713017149 .Acquire, .AcqRel => {
......@@ -17196,7 +17215,7 @@ fn zirMulAdd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1719617215 break :rs mulend1_src;
1719717216 };
1719817217
17199 try sema.requireRuntimeBlock(block, runtime_src);
17218 try sema.requireRuntimeBlock(block, src, runtime_src);
1720017219 return block.addInst(.{
1720117220 .tag = .mul_add,
1720217221 .data = .{ .pl_op = .{
......@@ -17229,10 +17248,10 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1722917248 const coerced_options = try sema.coerce(block, call_options_ty, options, options_src);
1723017249
1723117250 const modifier = try sema.fieldVal(block, options_src, coerced_options, "modifier", options_src);
17232 const modifier_val = try sema.resolveConstValue(block, options_src, modifier);
17251 const modifier_val = try sema.resolveConstValue(block, options_src, modifier, "call modifier must be comptime known");
1723317252
1723417253 const stack = try sema.fieldVal(block, options_src, coerced_options, "stack", options_src);
17235 const stack_val = try sema.resolveConstValue(block, options_src, stack);
17254 const stack_val = try sema.resolveConstValue(block, options_src, stack, "call stack value must be comptime known");
1723617255
1723717256 if (!stack_val.isNull()) {
1723817257 return sema.fail(block, options_src, "TODO: implement @call with stack", .{});
......@@ -17293,7 +17312,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1729317312
1729417313 // Desugar bound functions here
1729517314 if (sema.typeOf(func).tag() == .bound_fn) {
17296 const bound_func = try sema.resolveValue(block, func_src, func);
17315 const bound_func = try sema.resolveValue(block, .unneeded, func, undefined);
1729717316 const bound_data = &bound_func.cast(Value.Payload.BoundFn).?.data;
1729817317 func = bound_data.func_inst;
1729917318 resolved_args = try sema.arena.alloc(Air.Inst.Ref, args_ty.structFieldCount() + 1);
......@@ -17320,7 +17339,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
1732017339 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
1732117340
1732217341 const struct_ty = try sema.resolveType(block, ty_src, extra.parent_type);
17323 const field_name = try sema.resolveConstString(block, name_src, extra.field_name);
17342 const field_name = try sema.resolveConstString(block, name_src, extra.field_name, "field name must be comptime known");
1732417343 const field_ptr = try sema.resolveInst(extra.field_ptr);
1732517344 const field_ptr_ty = sema.typeOf(field_ptr);
1732617345
......@@ -17385,7 +17404,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
1738517404 return sema.addConstant(result_ptr, payload.data.container_ptr);
1738617405 }
1738717406
17388 try sema.requireRuntimeBlock(block, src);
17407 try sema.requireRuntimeBlock(block, src, ptr_src);
1738917408 return block.addInst(.{
1739017409 .tag = .field_parent_ptr,
1739117410 .data = .{ .ty_pl = .{
......@@ -17466,7 +17485,7 @@ fn analyzeMinMax(
1746617485 break :rs lhs_src;
1746717486 };
1746817487
17469 try sema.requireRuntimeBlock(block, runtime_src);
17488 try sema.requireRuntimeBlock(block, src, runtime_src);
1747017489 return block.addBinOp(air_tag, simd_op.lhs, simd_op.rhs);
1747117490}
1747217491
......@@ -17514,7 +17533,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1751417533 } else break :rs src_src;
1751517534 } else dest_src;
1751617535
17517 try sema.requireRuntimeBlock(block, runtime_src);
17536 try sema.requireRuntimeBlock(block, src, runtime_src);
1751817537 _ = try block.addInst(.{
1751917538 .tag = .memcpy,
1752017539 .data = .{ .pl_op = .{
......@@ -17556,7 +17575,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1755617575 } else break :rs len_src;
1755717576 } else dest_src;
1755817577
17559 try sema.requireRuntimeBlock(block, runtime_src);
17578 try sema.requireRuntimeBlock(block, src, runtime_src);
1756017579 _ = try block.addInst(.{
1756117580 .tag = .memset,
1756217581 .data = .{ .pl_op = .{
......@@ -17652,7 +17671,7 @@ fn zirVarExtended(
1765217671 uncasted_init;
1765317672
1765417673 break :blk (try sema.resolveMaybeUndefVal(block, init_src, init)) orelse
17655 return sema.failWithNeededComptime(block, init_src);
17674 return sema.failWithNeededComptime(block, init_src, "container level variable initializers must be comptime known");
1765617675 } else Value.initTag(.unreachable_value);
1765717676
1765817677 try sema.validateVarType(block, name_src, var_ty, small.is_extern);
......@@ -17718,7 +17737,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1771817737 const body = sema.code.extra[extra_index..][0..body_len];
1771917738 extra_index += body.len;
1772017739
17721 const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29);
17740 const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29, "alignment must be comptime known");
1772217741 if (val.tag() == .generic_poison) {
1772317742 break :blk null;
1772417743 }
......@@ -17731,7 +17750,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1773117750 } else if (extra.data.bits.has_align_ref) blk: {
1773217751 const align_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
1773317752 extra_index += 1;
17734 const align_tv = sema.resolveInstConst(block, align_src, align_ref) catch |err| switch (err) {
17753 const align_tv = sema.resolveInstConst(block, align_src, align_ref, "alignment must be comptime known") catch |err| switch (err) {
1773517754 error.GenericPoison => {
1773617755 break :blk null;
1773717756 },
......@@ -17752,7 +17771,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1775217771 extra_index += body.len;
1775317772
1775417773 const addrspace_ty = try sema.getBuiltinType(block, addrspace_src, "AddressSpace");
17755 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty);
17774 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, "addrespace must be comptime known");
1775617775 if (val.tag() == .generic_poison) {
1775717776 break :blk null;
1775817777 }
......@@ -17760,7 +17779,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1776017779 } else if (extra.data.bits.has_addrspace_ref) blk: {
1776117780 const addrspace_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
1776217781 extra_index += 1;
17763 const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref) catch |err| switch (err) {
17782 const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref, "addrespace must be comptime known") catch |err| switch (err) {
1776417783 error.GenericPoison => {
1776517784 break :blk null;
1776617785 },
......@@ -17775,7 +17794,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1777517794 const body = sema.code.extra[extra_index..][0..body_len];
1777617795 extra_index += body.len;
1777717796
17778 const val = try sema.resolveGenericBody(block, section_src, body, inst, Type.initTag(.const_slice_u8));
17797 const val = try sema.resolveGenericBody(block, section_src, body, inst, Type.initTag(.const_slice_u8), "linksection must be comptime known");
1777917798 if (val.tag() == .generic_poison) {
1778017799 break :blk FuncLinkSection{ .generic = {} };
1778117800 }
......@@ -17784,7 +17803,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1778417803 } else if (extra.data.bits.has_section_ref) blk: {
1778517804 const section_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
1778617805 extra_index += 1;
17787 const section_tv = sema.resolveInstConst(block, section_src, section_ref) catch |err| switch (err) {
17806 const section_tv = sema.resolveInstConst(block, section_src, section_ref, "linksection must be comptime known") catch |err| switch (err) {
1778817807 error.GenericPoison => {
1778917808 break :blk FuncLinkSection{ .generic = {} };
1779017809 },
......@@ -17801,7 +17820,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1780117820 extra_index += body.len;
1780217821
1780317822 const cc_ty = try sema.getBuiltinType(block, addrspace_src, "CallingConvention");
17804 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty);
17823 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, "calling convention must be comptime known");
1780517824 if (val.tag() == .generic_poison) {
1780617825 break :blk null;
1780717826 }
......@@ -17809,7 +17828,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1780917828 } else if (extra.data.bits.has_cc_ref) blk: {
1781017829 const cc_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
1781117830 extra_index += 1;
17812 const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref) catch |err| switch (err) {
17831 const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref, "calling convention must be comptime known") catch |err| switch (err) {
1781317832 error.GenericPoison => {
1781417833 break :blk null;
1781517834 },
......@@ -17824,14 +17843,14 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1782417843 const body = sema.code.extra[extra_index..][0..body_len];
1782517844 extra_index += body.len;
1782617845
17827 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type);
17846 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, "return type must be comptime known");
1782817847 var buffer: Value.ToTypeBuffer = undefined;
1782917848 const ty = try val.toType(&buffer).copy(sema.arena);
1783017849 break :blk ty;
1783117850 } else if (extra.data.bits.has_ret_ty_ref) blk: {
1783217851 const ret_ty_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
1783317852 extra_index += 1;
17834 const ret_ty_tv = sema.resolveInstConst(block, ret_src, ret_ty_ref) catch |err| switch (err) {
17853 const ret_ty_tv = sema.resolveInstConst(block, ret_src, ret_ty_ref, "return type must be comptime known") catch |err| switch (err) {
1783517854 error.GenericPoison => {
1783617855 break :blk Type.initTag(.generic_poison);
1783717856 },
......@@ -17886,7 +17905,7 @@ fn zirCUndef(
1788617905 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
1788717906 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1788817907
17889 const name = try sema.resolveConstString(block, src, extra.operand);
17908 const name = try sema.resolveConstString(block, src, extra.operand, "name of macro being undefined must be comptime known");
1789017909 try block.c_import_buf.?.writer().print("#undefine {s}\n", .{name});
1789117910 return Air.Inst.Ref.void_value;
1789217911}
......@@ -17899,7 +17918,7 @@ fn zirCInclude(
1789917918 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
1790017919 const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1790117920
17902 const name = try sema.resolveConstString(block, src, extra.operand);
17921 const name = try sema.resolveConstString(block, src, extra.operand, "path being included must be comptime known");
1790317922 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});
1790417923 return Air.Inst.Ref.void_value;
1790517924}
......@@ -17913,10 +17932,10 @@ fn zirCDefine(
1791317932 const name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1791417933 const val_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
1791517934
17916 const name = try sema.resolveConstString(block, name_src, extra.lhs);
17935 const name = try sema.resolveConstString(block, name_src, extra.lhs, "name of macro being undefined must be comptime known");
1791717936 const rhs = try sema.resolveInst(extra.rhs);
1791817937 if (sema.typeOf(rhs).zigTypeTag() != .Void) {
17919 const value = try sema.resolveConstString(block, val_src, extra.rhs);
17938 const value = try sema.resolveConstString(block, val_src, extra.rhs, "value of macro being undefined must be comptime known");
1792017939 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
1792117940 } else {
1792217941 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});
......@@ -17937,8 +17956,8 @@ fn zirWasmMemorySize(
1793717956 return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
1793817957 }
1793917958
17940 const index = @intCast(u32, try sema.resolveInt(block, index_src, extra.operand, Type.u32));
17941 try sema.requireRuntimeBlock(block, builtin_src);
17959 const index = @intCast(u32, try sema.resolveInt(block, index_src, extra.operand, Type.u32, "wasm memory size index must be comptime known"));
17960 try sema.requireRuntimeBlock(block, builtin_src, null);
1794217961 return block.addInst(.{
1794317962 .tag = .wasm_memory_size,
1794417963 .data = .{ .pl_op = .{
......@@ -17962,10 +17981,10 @@ fn zirWasmMemoryGrow(
1796217981 return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
1796317982 }
1796417983
17965 const index = @intCast(u32, try sema.resolveInt(block, index_src, extra.lhs, Type.u32));
17984 const index = @intCast(u32, try sema.resolveInt(block, index_src, extra.lhs, Type.u32, "wasm memory size index must be comptime known"));
1796617985 const delta = try sema.coerce(block, Type.u32, try sema.resolveInst(extra.rhs), delta_src);
1796717986
17968 try sema.requireRuntimeBlock(block, builtin_src);
17987 try sema.requireRuntimeBlock(block, builtin_src, null);
1796917988 return block.addInst(.{
1797017989 .tag = .wasm_memory_grow,
1797117990 .data = .{ .pl_op = .{
......@@ -17990,15 +18009,15 @@ fn zirPrefetch(
1799018009 const target = sema.mod.getTarget();
1799118010
1799218011 const rw = try sema.fieldVal(block, opts_src, options, "rw", opts_src);
17993 const rw_val = try sema.resolveConstValue(block, opts_src, rw);
18012 const rw_val = try sema.resolveConstValue(block, opts_src, rw, "prefetch read/write must be comptime known");
1799418013 const rw_tag = rw_val.toEnum(std.builtin.PrefetchOptions.Rw);
1799518014
1799618015 const locality = try sema.fieldVal(block, opts_src, options, "locality", opts_src);
17997 const locality_val = try sema.resolveConstValue(block, opts_src, locality);
18016 const locality_val = try sema.resolveConstValue(block, opts_src, locality, "prefetch locality must be comptime known");
1799818017 const locality_int = @intCast(u2, locality_val.toUnsignedInt(target));
1799918018
1800018019 const cache = try sema.fieldVal(block, opts_src, options, "cache", opts_src);
18001 const cache_val = try sema.resolveConstValue(block, opts_src, cache);
18020 const cache_val = try sema.resolveConstValue(block, opts_src, cache, "prefetch cache must be comptime known");
1800218021 const cache_tag = cache_val.toEnum(std.builtin.PrefetchOptions.Cache);
1800318022
1800418023 if (!block.is_comptime) {
......@@ -18035,16 +18054,16 @@ fn zirBuiltinExtern(
1803518054 const options = try sema.coerce(block, extern_options_ty, options_inst, options_src);
1803618055
1803718056 const name = try sema.fieldVal(block, options_src, options, "name", options_src);
18038 const name_val = try sema.resolveConstValue(block, options_src, name);
18057 const name_val = try sema.resolveConstValue(block, options_src, name, "name of the extern symbol must be comptime known");
1803918058
1804018059 const library_name_inst = try sema.fieldVal(block, options_src, options, "library_name", options_src);
18041 const library_name_val = try sema.resolveConstValue(block, options_src, library_name_inst);
18060 const library_name_val = try sema.resolveConstValue(block, options_src, library_name_inst, "library in which extern symbol is must be comptime known");
1804218061
1804318062 const linkage = try sema.fieldVal(block, options_src, options, "linkage", options_src);
18044 const linkage_val = try sema.resolveConstValue(block, options_src, linkage);
18063 const linkage_val = try sema.resolveConstValue(block, options_src, linkage, "linkage of the extern symbol must be comptime known");
1804518064
1804618065 const is_thread_local = try sema.fieldVal(block, options_src, options, "is_thread_local", options_src);
18047 const is_thread_local_val = try sema.resolveConstValue(block, options_src, is_thread_local);
18066 const is_thread_local_val = try sema.resolveConstValue(block, options_src, is_thread_local, "threadlocality of the extern symbol must be comptime known");
1804818067
1804918068 var library_name: ?[]const u8 = null;
1805018069 if (!library_name_val.isNull()) {
......@@ -18121,19 +18140,30 @@ fn zirBuiltinExtern(
1812118140 new_decl.value_arena = arena_state;
1812218141
1812318142 const ref = try sema.analyzeDeclRef(new_decl_index);
18124 try sema.requireRuntimeBlock(block, src);
18143 try sema.requireRuntimeBlock(block, src, null);
1812518144 return block.addBitCast(ty, ref);
1812618145}
1812718146
18147/// Asserts that the block is not comptime.
1812818148fn requireFunctionBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
18149 assert(!block.is_comptime);
1812918150 if (sema.func == null and !block.is_typeof and !block.is_coerce_result_ptr) {
1813018151 return sema.fail(block, src, "instruction illegal outside function body", .{});
1813118152 }
1813218153}
1813318154
18134fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
18155fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
1813518156 if (block.is_comptime) {
18136 return sema.failWithNeededComptime(block, src);
18157 const msg = msg: {
18158 const msg = try sema.errMsg(block, src, "unable to evalutate comptime expression", .{});
18159 errdefer msg.destroy(sema.gpa);
18160
18161 if (runtime_src) |some| {
18162 try sema.errNote(block, some, msg, "operation is runtime due to this operand", .{});
18163 }
18164 break :msg msg;
18165 };
18166 return sema.failWithOwnedErrorMsg(block, msg);
1813718167 }
1813818168 try sema.requireFunctionBlock(block, src);
1813918169}
......@@ -18972,7 +19002,7 @@ fn fieldPtr(
1897219002 }),
1897319003 );
1897419004 }
18975 try sema.requireRuntimeBlock(block, src);
19005 try sema.requireRuntimeBlock(block, src, null);
1897619006
1897719007 return block.addTyOp(.ptr_slice_ptr_ptr, result_ty, inner_ptr);
1897819008 } else if (mem.eql(u8, field_name, "len")) {
......@@ -18992,7 +19022,7 @@ fn fieldPtr(
1899219022 }),
1899319023 );
1899419024 }
18995 try sema.requireRuntimeBlock(block, src);
19025 try sema.requireRuntimeBlock(block, src, null);
1899619026
1899719027 return block.addTyOp(.ptr_slice_len_ptr, result_ty, inner_ptr);
1899819028 } else {
......@@ -19005,7 +19035,7 @@ fn fieldPtr(
1900519035 }
1900619036 },
1900719037 .Type => {
19008 _ = try sema.resolveConstValue(block, object_ptr_src, object_ptr);
19038 _ = try sema.resolveConstValue(block, .unneeded, object_ptr, undefined);
1900919039 const result = try sema.analyzeLoad(block, src, object_ptr, object_ptr_src);
1901019040 const inner = if (is_pointer_to)
1901119041 try sema.analyzeLoad(block, src, result, object_ptr_src)
......@@ -19238,7 +19268,7 @@ fn finishFieldCallBind(
1923819268 return sema.analyzeLoad(block, src, pointer, src);
1923919269 }
1924019270
19241 try sema.requireRuntimeBlock(block, src);
19271 try sema.requireRuntimeBlock(block, src, null);
1924219272 const ptr_inst = try block.addStructFieldPtr(object_ptr, field_index, ptr_field_ty);
1924319273 return sema.analyzeLoad(block, src, ptr_inst, src);
1924419274}
......@@ -19425,7 +19455,7 @@ fn structFieldPtrByIndex(
1942519455 );
1942619456 }
1942719457
19428 try sema.requireRuntimeBlock(block, src);
19458 try sema.requireRuntimeBlock(block, src, null);
1942919459 return block.addStructFieldPtr(struct_ptr, field_index, ptr_field_ty);
1943019460}
1943119461
......@@ -19469,7 +19499,7 @@ fn structFieldVal(
1946919499 return sema.addConstant(field.ty, field_values[field_index]);
1947019500 }
1947119501
19472 try sema.requireRuntimeBlock(block, src);
19502 try sema.requireRuntimeBlock(block, src, null);
1947319503 return block.addStructFieldVal(struct_byval, field_index, field.ty);
1947419504 },
1947519505 else => unreachable,
......@@ -19533,7 +19563,7 @@ fn tupleFieldValByIndex(
1953319563 return sema.addConstant(field_ty, field_values[field_index]);
1953419564 }
1953519565
19536 try sema.requireRuntimeBlock(block, src);
19566 try sema.requireRuntimeBlock(block, src, null);
1953719567 return block.addStructFieldVal(tuple_byval, field_index, field_ty);
1953819568}
1953919569
......@@ -19597,7 +19627,7 @@ fn unionFieldPtr(
1959719627 );
1959819628 }
1959919629
19600 try sema.requireRuntimeBlock(block, src);
19630 try sema.requireRuntimeBlock(block, src, null);
1960119631 return block.addStructFieldPtr(union_ptr, field_index, ptr_field_ty);
1960219632}
1960319633
......@@ -19655,7 +19685,7 @@ fn unionFieldVal(
1965519685 }
1965619686 }
1965719687
19658 try sema.requireRuntimeBlock(block, src);
19688 try sema.requireRuntimeBlock(block, src, null);
1965919689 return block.addStructFieldVal(union_byval, field_index, field.ty);
1966019690}
1966119691
......@@ -19684,7 +19714,7 @@ fn elemPtr(
1968419714 // In all below cases, we have to deref the ptr operand to get the actual indexable pointer.
1968519715 const indexable = try sema.analyzeLoad(block, indexable_ptr_src, indexable_ptr, indexable_ptr_src);
1968619716 switch (indexable_ty.ptrSize()) {
19687 .Slice => return sema.elemPtrSlice(block, indexable_ptr_src, indexable, elem_index_src, elem_index),
19717 .Slice => return sema.elemPtrSlice(block, src, indexable_ptr_src, indexable, elem_index_src, elem_index),
1968819718 .Many, .C => {
1968919719 const maybe_ptr_val = try sema.resolveDefinedValue(block, indexable_ptr_src, indexable);
1969019720 const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
......@@ -19698,19 +19728,19 @@ fn elemPtr(
1969819728 };
1969919729 const result_ty = try sema.elemPtrType(indexable_ty, null);
1970019730
19701 try sema.requireRuntimeBlock(block, runtime_src);
19731 try sema.requireRuntimeBlock(block, src, runtime_src);
1970219732 return block.addPtrElemPtr(indexable, elem_index, result_ty);
1970319733 },
1970419734 .One => {
1970519735 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable
19706 return sema.elemPtrArray(block, indexable_ptr_src, indexable, elem_index_src, elem_index, init);
19736 return sema.elemPtrArray(block, src, indexable_ptr_src, indexable, elem_index_src, elem_index, init);
1970719737 },
1970819738 }
1970919739 },
19710 .Array, .Vector => return sema.elemPtrArray(block, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init),
19740 .Array, .Vector => return sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init),
1971119741 .Struct => {
1971219742 // Tuple field access.
19713 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index);
19743 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, "tuple field access index must be comptime known");
1971419744 const index = @intCast(u32, index_val.toUnsignedInt(target));
1971519745 return sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index);
1971619746 },
......@@ -19740,7 +19770,7 @@ fn elemVal(
1974019770
1974119771 switch (indexable_ty.zigTypeTag()) {
1974219772 .Pointer => switch (indexable_ty.ptrSize()) {
19743 .Slice => return sema.elemValSlice(block, indexable_src, indexable, elem_index_src, elem_index),
19773 .Slice => return sema.elemValSlice(block, src, indexable_src, indexable, elem_index_src, elem_index),
1974419774 .Many, .C => {
1974519775 const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
1974619776 const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
......@@ -19756,7 +19786,7 @@ fn elemVal(
1975619786 break :rs indexable_src;
1975719787 };
1975819788
19759 try sema.requireRuntimeBlock(block, runtime_src);
19789 try sema.requireRuntimeBlock(block, src, runtime_src);
1976019790 return block.addBinOp(.ptr_elem_val, indexable, elem_index);
1976119791 },
1976219792 .One => {
......@@ -19765,14 +19795,14 @@ fn elemVal(
1976519795 return sema.analyzeLoad(block, indexable_src, elem_ptr, elem_index_src);
1976619796 },
1976719797 },
19768 .Array => return elemValArray(sema, block, indexable_src, indexable, elem_index_src, elem_index),
19798 .Array => return sema.elemValArray(block, src, indexable_src, indexable, elem_index_src, elem_index),
1976919799 .Vector => {
1977019800 // TODO: If the index is a vector, the result should be a vector.
19771 return elemValArray(sema, block, indexable_src, indexable, elem_index_src, elem_index);
19801 return sema.elemValArray(block, src, indexable_src, indexable, elem_index_src, elem_index);
1977219802 },
1977319803 .Struct => {
1977419804 // Tuple field access.
19775 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index);
19805 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, "tuple field access index must be comptime known");
1977619806 const index = @intCast(u32, index_val.toUnsignedInt(target));
1977719807 return tupleField(sema, block, indexable_src, indexable, elem_index_src, index);
1977819808 },
......@@ -19850,7 +19880,7 @@ fn tupleFieldPtr(
1985019880
1985119881 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
1985219882
19853 try sema.requireRuntimeBlock(block, tuple_ptr_src);
19883 try sema.requireRuntimeBlock(block, tuple_ptr_src, null);
1985419884 return block.addStructFieldPtr(tuple_ptr, field_index, ptr_field_ty);
1985519885}
1985619886
......@@ -19890,13 +19920,14 @@ fn tupleField(
1989019920
1989119921 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_src);
1989219922
19893 try sema.requireRuntimeBlock(block, tuple_src);
19923 try sema.requireRuntimeBlock(block, tuple_src, null);
1989419924 return block.addStructFieldVal(tuple, field_index, field_ty);
1989519925}
1989619926
1989719927fn elemValArray(
1989819928 sema: *Sema,
1989919929 block: *Block,
19930 src: LazySrcLoc,
1990019931 array_src: LazySrcLoc,
1990119932 array: Air.Inst.Ref,
1990219933 elem_index_src: LazySrcLoc,
......@@ -19943,7 +19974,7 @@ fn elemValArray(
1994319974 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, array_ty, array_src);
1994419975
1994519976 const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src;
19946 try sema.requireRuntimeBlock(block, runtime_src);
19977 try sema.requireRuntimeBlock(block, src, runtime_src);
1994719978 if (block.wantSafety()) {
1994819979 // Runtime check is only needed if unable to comptime check
1994919980 if (maybe_index_val == null) {
......@@ -19958,6 +19989,7 @@ fn elemValArray(
1995819989fn elemPtrArray(
1995919990 sema: *Sema,
1996019991 block: *Block,
19992 src: LazySrcLoc,
1996119993 array_ptr_src: LazySrcLoc,
1996219994 array_ptr: Air.Inst.Ref,
1996319995 elem_index_src: LazySrcLoc,
......@@ -20003,7 +20035,7 @@ fn elemPtrArray(
2000320035 }
2000420036
2000520037 const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src;
20006 try sema.requireRuntimeBlock(block, runtime_src);
20038 try sema.requireRuntimeBlock(block, src, runtime_src);
2000720039
2000820040 // Runtime check is only needed if unable to comptime check.
2000920041 if (block.wantSafety() and offset == null) {
......@@ -20018,6 +20050,7 @@ fn elemPtrArray(
2001820050fn elemValSlice(
2001920051 sema: *Sema,
2002020052 block: *Block,
20053 src: LazySrcLoc,
2002120054 slice_src: LazySrcLoc,
2002220055 slice: Air.Inst.Ref,
2002320056 elem_index_src: LazySrcLoc,
......@@ -20057,7 +20090,7 @@ fn elemValSlice(
2005720090
2005820091 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
2005920092
20060 try sema.requireRuntimeBlock(block, runtime_src);
20093 try sema.requireRuntimeBlock(block, src, runtime_src);
2006120094 if (block.wantSafety()) {
2006220095 const len_inst = if (maybe_slice_val) |slice_val|
2006320096 try sema.addIntUnsigned(Type.usize, slice_val.sliceLen(sema.mod))
......@@ -20073,6 +20106,7 @@ fn elemValSlice(
2007320106fn elemPtrSlice(
2007420107 sema: *Sema,
2007520108 block: *Block,
20109 src: LazySrcLoc,
2007620110 slice_src: LazySrcLoc,
2007720111 slice: Air.Inst.Ref,
2007820112 elem_index_src: LazySrcLoc,
......@@ -20113,7 +20147,7 @@ fn elemPtrSlice(
2011320147 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ptr_ty, slice_ty, slice_src);
2011420148
2011520149 const runtime_src = if (maybe_undef_slice_val != null) elem_index_src else slice_src;
20116 try sema.requireRuntimeBlock(block, runtime_src);
20150 try sema.requireRuntimeBlock(block, src, runtime_src);
2011720151 if (block.wantSafety()) {
2011820152 const len_inst = len: {
2011920153 if (maybe_undef_slice_val) |slice_val|
......@@ -20177,7 +20211,7 @@ fn coerceExtra(
2017720211 // Keep the comptime Value representation; take the new type.
2017820212 return sema.addConstant(dest_ty, val);
2017920213 }
20180 try sema.requireRuntimeBlock(block, inst_src);
20214 try sema.requireRuntimeBlock(block, inst_src, null);
2018120215 return block.addBitCast(dest_ty, inst);
2018220216 }
2018320217
......@@ -20222,7 +20256,7 @@ fn coerceExtra(
2022220256
2022320257 // Function body to function pointer.
2022420258 if (inst_ty.zigTypeTag() == .Fn) {
20225 const fn_val = try sema.resolveConstValue(block, inst_src, inst);
20259 const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
2022620260 const fn_decl = fn_val.castTag(.function).?.data.owner_decl;
2022720261 const inst_as_ptr = try sema.analyzeDeclRef(fn_decl);
2022820262 return sema.coerce(block, dest_ty, inst_as_ptr, inst_src);
......@@ -20489,7 +20523,7 @@ fn coerceExtra(
2048920523 // small enough unsigned ints can get casted to large enough signed ints
2049020524 (dst_info.signedness == .signed and dst_info.bits > src_info.bits))
2049120525 {
20492 try sema.requireRuntimeBlock(block, inst_src);
20526 try sema.requireRuntimeBlock(block, inst_src, null);
2049320527 return block.addTyOp(.intcast, dest_ty, inst);
2049420528 }
2049520529 },
......@@ -20500,7 +20534,7 @@ fn coerceExtra(
2050020534 },
2050120535 .Float, .ComptimeFloat => switch (inst_ty.zigTypeTag()) {
2050220536 .ComptimeFloat => {
20503 const val = try sema.resolveConstValue(block, inst_src, inst);
20537 const val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
2050420538 const result_val = try val.floatCast(sema.arena, dest_ty, target);
2050520539 return try sema.addConstant(dest_ty, result_val);
2050620540 },
......@@ -20516,13 +20550,15 @@ fn coerceExtra(
2051620550 );
2051720551 }
2051820552 return try sema.addConstant(dest_ty, result_val);
20553 } else if (dest_ty.zigTypeTag() == .ComptimeFloat) {
20554 return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime known");
2051920555 }
2052020556
2052120557 // float widening
2052220558 const src_bits = inst_ty.floatBits(target);
2052320559 const dst_bits = dest_ty.floatBits(target);
2052420560 if (dst_bits >= src_bits) {
20525 try sema.requireRuntimeBlock(block, inst_src);
20561 try sema.requireRuntimeBlock(block, inst_src, null);
2052620562 return block.addTyOp(.fpext, dest_ty, inst);
2052720563 }
2052820564 },
......@@ -20549,7 +20585,7 @@ fn coerceExtra(
2054920585 .Enum => switch (inst_ty.zigTypeTag()) {
2055020586 .EnumLiteral => {
2055120587 // enum literal to enum
20552 const val = try sema.resolveConstValue(block, inst_src, inst);
20588 const val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
2055320589 const bytes = val.castTag(.enum_literal).?.data;
2055420590 const field_index = dest_ty.enumFieldIndex(bytes) orelse {
2055520591 const msg = msg: {
......@@ -21712,7 +21748,12 @@ fn storePtr2(
2171221748 return;
2171321749 }
2171421750
21715 try sema.requireRuntimeBlock(block, runtime_src);
21751 if (block.is_comptime) {
21752 // TODO ideally this would tell why the block is comptime
21753 return sema.fail(block, ptr_src, "cannot store to runtime value in comptime block", .{});
21754 }
21755
21756 try sema.requireRuntimeBlock(block, src, runtime_src);
2171621757 try sema.queueFullTypeResolution(elem_ty);
2171721758 if (is_ret) {
2171821759 _ = try block.addBinOp(.store, ptr, operand);
......@@ -22644,7 +22685,7 @@ fn bitCast(
2264422685 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0);
2264522686 return sema.addConstant(dest_ty, result_val);
2264622687 }
22647 try sema.requireRuntimeBlock(block, inst_src);
22688 try sema.requireRuntimeBlock(block, inst_src, null);
2264822689 return block.addBitCast(dest_ty, inst);
2264922690}
2265022691
......@@ -22727,7 +22768,7 @@ fn coerceArrayPtrToSlice(
2272722768 });
2272822769 return sema.addConstant(dest_ty, slice_val);
2272922770 }
22730 try sema.requireRuntimeBlock(block, inst_src);
22771 try sema.requireRuntimeBlock(block, inst_src, null);
2273122772 return block.addTyOp(.array_to_slice, dest_ty, inst);
2273222773}
2273322774
......@@ -22743,7 +22784,7 @@ fn coerceCompatiblePtrs(
2274322784 // The comptime Value representation is compatible with both types.
2274422785 return sema.addConstant(dest_ty, val);
2274522786 }
22746 try sema.requireRuntimeBlock(block, inst_src);
22787 try sema.requireRuntimeBlock(block, inst_src, null);
2274722788 return sema.bitCast(block, dest_ty, inst, inst_src);
2274822789}
2274922790
......@@ -22807,7 +22848,7 @@ fn coerceEnumToUnion(
2280722848 }));
2280822849 }
2280922850
22810 try sema.requireRuntimeBlock(block, inst_src);
22851 try sema.requireRuntimeBlock(block, inst_src, null);
2281122852
2281222853 if (tag_ty.isNonexhaustiveEnum()) {
2281322854 const msg = msg: {
......@@ -22947,7 +22988,7 @@ fn coerceArrayLike(
2294722988 // These types share the same comptime value representation.
2294822989 return sema.addConstant(dest_ty, inst_val);
2294922990 }
22950 try sema.requireRuntimeBlock(block, inst_src);
22991 try sema.requireRuntimeBlock(block, inst_src, null);
2295122992 return block.addBitCast(dest_ty, inst);
2295222993 }
2295322994
......@@ -22960,8 +23001,9 @@ fn coerceArrayLike(
2296023001 Type.usize,
2296123002 try Value.Tag.int_u64.create(sema.arena, i),
2296223003 );
23004 const src = inst_src; // TODO better source location
2296323005 const elem_src = inst_src; // TODO better source location
22964 const elem_ref = try elemValArray(sema, block, inst_src, inst, elem_src, index_ref);
23006 const elem_ref = try sema.elemValArray(block, src, inst_src, inst, elem_src, index_ref);
2296523007 const coerced = try sema.coerce(block, dest_elem_ty, elem_ref, elem_src);
2296623008 element_refs[i] = coerced;
2296723009 if (runtime_src == null) {
......@@ -22974,7 +23016,7 @@ fn coerceArrayLike(
2297423016 }
2297523017
2297623018 if (runtime_src) |rs| {
22977 try sema.requireRuntimeBlock(block, rs);
23019 try sema.requireRuntimeBlock(block, inst_src, rs);
2297823020 return block.addAggregateInit(dest_ty, element_refs);
2297923021 }
2298023022
......@@ -23037,7 +23079,7 @@ fn coerceTupleToArray(
2303723079 }
2303823080
2303923081 if (runtime_src) |rs| {
23040 try sema.requireRuntimeBlock(block, rs);
23082 try sema.requireRuntimeBlock(block, inst_src, rs);
2304123083 return block.addAggregateInit(dest_ty, element_refs);
2304223084 }
2304323085
......@@ -23168,7 +23210,7 @@ fn coerceTupleToStruct(
2316823210 }
2316923211
2317023212 if (runtime_src) |rs| {
23171 try sema.requireRuntimeBlock(block, rs);
23213 try sema.requireRuntimeBlock(block, inst_src, rs);
2317223214 return block.addAggregateInit(struct_ty, field_refs);
2317323215 }
2317423216
......@@ -23282,7 +23324,7 @@ fn analyzeRef(
2328223324 ));
2328323325 }
2328423326
23285 try sema.requireRuntimeBlock(block, src);
23327 try sema.requireRuntimeBlock(block, src, null);
2328623328 const address_space = target_util.defaultAddressSpace(sema.mod.getTarget(), .local);
2328723329 const ptr_type = try Type.ptr(sema.arena, sema.mod, .{
2328823330 .pointee_type = operand_ty,
......@@ -23326,10 +23368,12 @@ fn analyzeLoad(
2332623368 }
2332723369 }
2332823370
23329 const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);
23330 if (!valid_rt) return sema.failWithNeededComptime(block, src);
23371 if (block.is_comptime) {
23372 // TODO ideally this would tell why the block is comptime
23373 return sema.fail(block, ptr_src, "cannot load runtime value in comptime block", .{});
23374 }
2333123375
23332 try sema.requireRuntimeBlock(block, src);
23376 try sema.requireFunctionBlock(block, src);
2333323377 return block.addTyOp(.load, elem_ty, ptr);
2333423378}
2333523379
......@@ -23346,7 +23390,7 @@ fn analyzeSlicePtr(
2334623390 if (val.isUndef()) return sema.addConstUndef(result_ty);
2334723391 return sema.addConstant(result_ty, val.slicePtr());
2334823392 }
23349 try sema.requireRuntimeBlock(block, slice_src);
23393 try sema.requireRuntimeBlock(block, slice_src, null);
2335023394 return block.addTyOp(.slice_ptr, result_ty, slice);
2335123395}
2335223396
......@@ -23362,7 +23406,7 @@ fn analyzeSliceLen(
2336223406 }
2336323407 return sema.addIntUnsigned(Type.usize, slice_val.sliceLen(sema.mod));
2336423408 }
23365 try sema.requireRuntimeBlock(block, src);
23409 try sema.requireRuntimeBlock(block, src, null);
2336623410 return block.addTyOp(.slice_len, Type.usize, slice_inst);
2336723411}
2336823412
......@@ -23386,7 +23430,7 @@ fn analyzeIsNull(
2338623430 return Air.Inst.Ref.bool_false;
2338723431 }
2338823432 }
23389 try sema.requireRuntimeBlock(block, src);
23433 try sema.requireRuntimeBlock(block, src, null);
2339023434 const air_tag: Air.Inst.Tag = if (invert_logic) .is_non_null else .is_null;
2339123435 return block.addUnOp(air_tag, operand);
2339223436}
......@@ -23476,7 +23520,7 @@ fn analyzeIsNonErr(
2347623520) CompileError!Air.Inst.Ref {
2347723521 const result = try sema.analyzeIsNonErrComptimeOnly(block, src, operand);
2347823522 if (result == .none) {
23479 try sema.requireRuntimeBlock(block, src);
23523 try sema.requireRuntimeBlock(block, src, null);
2348023524 return block.addUnOp(.is_non_err, operand);
2348123525 } else {
2348223526 return result;
......@@ -23661,7 +23705,7 @@ fn analyzeSlice(
2366123705 const sentinel = s: {
2366223706 if (sentinel_opt != .none) {
2366323707 const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);
23664 break :s try sema.resolveConstValue(block, sentinel_src, casted);
23708 break :s try sema.resolveConstValue(block, sentinel_src, casted, "slice sentinel must be comptime known");
2366523709 }
2366623710 // If we are slicing to the end of something that is sentinel-terminated
2366723711 // then the resulting slice type is also sentinel-terminated.
......@@ -23738,7 +23782,14 @@ fn analyzeSlice(
2373823782 .size = .Slice,
2373923783 });
2374023784
23741 try sema.requireRuntimeBlock(block, src);
23785 const runtime_src = if ((try sema.resolveMaybeUndefVal(block, ptr_src, ptr_or_slice)) == null)
23786 ptr_src
23787 else if ((try sema.resolveMaybeUndefVal(block, src, start)) == null)
23788 start_src
23789 else
23790 end_src;
23791
23792 try sema.requireRuntimeBlock(block, src, runtime_src);
2374223793 if (block.wantSafety()) {
2374323794 // requirement: slicing C ptr is non-null
2374423795 if (ptr_ptr_child_ty.isCPtr()) {
......@@ -23846,7 +23897,7 @@ fn cmpNumeric(
2384623897 // a full resolution of their value, for example `@sizeOf(@Frame(function))` is known to
2384723898 // always be nonzero, and we benefit from not forcing the full evaluation and stack frame layout
2384823899 // of this function if we don't need to.
23849 try sema.requireRuntimeBlock(block, runtime_src);
23900 try sema.requireRuntimeBlock(block, src, runtime_src);
2385023901
2385123902 // For floats, emit a float comparison instruction.
2385223903 const lhs_is_float = switch (lhs_ty_tag) {
......@@ -24034,7 +24085,7 @@ fn cmpVector(
2403424085 }
2403524086 };
2403624087
24037 try sema.requireRuntimeBlock(block, runtime_src);
24088 try sema.requireRuntimeBlock(block, src, runtime_src);
2403824089 const result_ty_inst = try sema.addType(result_ty);
2403924090 return block.addCmpVector(lhs, rhs, op, result_ty_inst);
2404024091}
......@@ -24050,7 +24101,7 @@ fn wrapOptional(
2405024101 return sema.addConstant(dest_ty, try Value.Tag.opt_payload.create(sema.arena, val));
2405124102 }
2405224103
24053 try sema.requireRuntimeBlock(block, inst_src);
24104 try sema.requireRuntimeBlock(block, inst_src, null);
2405424105 return block.addTyOp(.wrap_optional, dest_ty, inst);
2405524106}
2405624107
......@@ -24066,7 +24117,7 @@ fn wrapErrorUnionPayload(
2406624117 if (try sema.resolveMaybeUndefVal(block, inst_src, coerced)) |val| {
2406724118 return sema.addConstant(dest_ty, try Value.Tag.eu_payload.create(sema.arena, val));
2406824119 }
24069 try sema.requireRuntimeBlock(block, inst_src);
24120 try sema.requireRuntimeBlock(block, inst_src, null);
2407024121 try sema.queueFullTypeResolution(dest_payload_ty);
2407124122 return block.addTyOp(.wrap_errunion_payload, dest_ty, coerced);
2407224123}
......@@ -24122,7 +24173,7 @@ fn wrapErrorUnionSet(
2412224173 return sema.addConstant(dest_ty, val);
2412324174 }
2412424175
24125 try sema.requireRuntimeBlock(block, inst_src);
24176 try sema.requireRuntimeBlock(block, inst_src, null);
2412624177 const coerced = try sema.coerce(block, dest_err_set_ty, inst, inst_src);
2412724178 return block.addTyOp(.wrap_errunion_err, dest_ty, coerced);
2412824179}
......@@ -24140,7 +24191,7 @@ fn unionToTag(
2414024191 if (try sema.resolveMaybeUndefVal(block, un_src, un)) |un_val| {
2414124192 return sema.addConstant(enum_ty, un_val.unionTag());
2414224193 }
24143 try sema.requireRuntimeBlock(block, un_src);
24194 try sema.requireRuntimeBlock(block, un_src, null);
2414424195 return block.addTyOp(.get_union_tag, enum_ty, un);
2414524196}
2414624197
......@@ -25311,7 +25362,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void
2531125362 const field = &struct_obj.fields.values()[i];
2531225363 const coerced = try sema.coerce(&block_scope, field.ty, init, src);
2531325364 const default_val = (try sema.resolveMaybeUndefVal(&block_scope, src, coerced)) orelse
25314 return sema.failWithNeededComptime(&block_scope, src);
25365 return sema.failWithNeededComptime(&block_scope, src, "struct field default value must be comptime known");
2531525366 field.default_val = try default_val.copy(decl_arena_allocator);
2531625367 }
2531725368 }
......@@ -25504,7 +25555,7 @@ fn semaUnionFields(block: *Block, mod: *Module, union_obj: *Module.Union) Compil
2550425555 if (tag_ref != .none) {
2550525556 const tag_src = src; // TODO better source location
2550625557 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, tag_src);
25507 const val = try sema.resolveConstValue(&block_scope, tag_src, coerced);
25558 const val = try sema.resolveConstValue(&block_scope, tag_src, coerced, "enum tag value must be comptime known");
2550825559 last_tag_val = val;
2550925560
2551025561 // This puts the memory into the union arena, not the enum arena, but
......@@ -26247,7 +26298,7 @@ pub fn analyzeAddrspace(
2624726298 zir_ref: Zir.Inst.Ref,
2624826299 ctx: AddressSpaceContext,
2624926300) !std.builtin.AddressSpace {
26250 const addrspace_tv = try sema.resolveInstConst(block, src, zir_ref);
26301 const addrspace_tv = try sema.resolveInstConst(block, src, zir_ref, "addresspace must be comptime known");
2625126302 const address_space = addrspace_tv.val.toEnum(std.builtin.AddressSpace);
2625226303 const target = sema.mod.getTarget();
2625326304 const arch = target.cpu.arch;
test/cases/compile_errors/asm_at_compile_time.zig+1-1
......@@ -14,5 +14,5 @@ fn doSomeAsm() void {
1414// backend=llvm
1515// target=native
1616//
17// :6:5: error: unable to resolve comptime value
17// :6:5: error: unable to evalutate comptime expression
1818// :2:14: note: called from here
test/cases/compile_errors/call method on bound fn referring to var instance.zig +1-1
......@@ -17,4 +17,4 @@ fn bad(ok: bool) void {
1717// target=native
1818// backend=stage2
1919//
20// :12:18: error: unable to resolve comptime value
20// :12:18: error: cannot load runtime value in comptime block
test/cases/compile_errors/int-float_conversion_to_comptime_int-float.zig+2
......@@ -12,4 +12,6 @@ export fn bar() void {
1212// target=native
1313//
1414// :3:35: error: unable to resolve comptime value
15// :3:35: note: value being casted to 'comptime_int' must be comptime known
1516// :7:37: error: unable to resolve comptime value
17// :7:37: note: value being casted to 'comptime_float' must be comptime known
test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig+1-1
......@@ -14,5 +14,5 @@ export fn entry() usize { return @sizeOf(@TypeOf(a)); }
1414// backend=stage2
1515// target=native
1616//
17// :6:26: error: unable to resolve comptime value
17// :6:26: error: cannot store to runtime value in comptime block
1818// :4:17: note: called from here
test/cases/compile_errors/non-pure_function_returns_type.zig+1-1
......@@ -21,5 +21,5 @@ export fn function_with_return_type_type() void {
2121// backend=stage2
2222// target=native
2323//
24// :3:7: error: unable to resolve comptime value
24// :3:7: error: cannot load runtime value in comptime block
2525// :16:19: note: called from here
test/cases/compile_errors/non_constant_expression_in_array_size.zig+1-1
......@@ -10,5 +10,5 @@ export fn entry() usize { return @offsetOf(Foo, "y"); }
1010// backend=stage2
1111// target=native
1212//
13// :5:25: error: unable to resolve comptime value
13// :5:25: error: cannot load runtime value in comptime block
1414// :2:15: note: called from here
test/cases/extern_variable_has_no_type.0.zig+1-1
......@@ -6,4 +6,4 @@ extern var foo: i32;
66
77// error
88//
9// :2:15: error: unable to resolve comptime value
9// :2:15: error: cannot load runtime value in comptime block
test/cases/x86_64-linux/assert_function.8.zig+1
......@@ -22,3 +22,4 @@ pub fn assert(ok: bool) void {
2222// error
2323//
2424// :3:21: error: unable to resolve comptime value
25// :3:21: note: condition in comptime branch must be comptime known
test/stage2/cbe.zig+2-2
......@@ -51,8 +51,8 @@ pub fn addCases(ctx: *TestContext) !void {
5151 \\}
5252 \\var y: @import("std").builtin.CallingConvention = .C;
5353 , &.{
54 ":2:22: error: unable to resolve comptime value",
55 ":5:26: error: unable to resolve comptime value",
54 ":2:22: error: cannot load runtime value in comptime block",
55 ":5:26: error: cannot load runtime value in comptime block",
5656 });
5757 }
5858