diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-08-29 16:13:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-29 16:13:39 -0400 |
| commit | 49075d20557994da4eb341e7431de38a6df2088b (patch) | |
| tree | 6c56a36b2d6e96612dd8beff36f5852816c6782f /src | |
| parent | 4635179857999a64ce8350c9b3cbe90cede9ea8c (diff) | |
| parent | 7a251c4cb8082d080e23fb86fe20be6bf4c745a4 (diff) | |
| download | zig-49075d20557994da4eb341e7431de38a6df2088b.tar.gz zig-49075d20557994da4eb341e7431de38a6df2088b.zip | |
Merge pull request #16969 from jacobly0/no-clear-ref-trace
Sema: refactor to use fewer catch expressions
Diffstat (limited to 'src')
| -rw-r--r-- | src/Compilation.zig | 47 | ||||
| -rw-r--r-- | src/Module.zig | 12 | ||||
| -rw-r--r-- | src/Sema.zig | 1802 |
3 files changed, 1025 insertions, 836 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index 069cc8a2c7..6cc8502461 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -2858,51 +2858,52 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod var ref_traces: std.ArrayListUnmanaged(ErrorBundle.ReferenceTrace) = .{}; defer ref_traces.deinit(gpa); - for (module_err_msg.reference_trace) |module_reference| { - if (module_reference.hidden != 0) { - try ref_traces.append(gpa, .{ - .decl_name = module_reference.hidden, - .src_loc = .none, - }); - break; - } else if (module_reference.decl == .none) { - try ref_traces.append(gpa, .{ - .decl_name = 0, - .src_loc = .none, - }); - break; + const remaining_references: ?u32 = remaining: { + if (mod.comp.reference_trace) |_| { + if (module_err_msg.hidden_references > 0) break :remaining module_err_msg.hidden_references; + } else { + if (module_err_msg.reference_trace.len > 0) break :remaining 0; } + break :remaining null; + }; + try ref_traces.ensureTotalCapacityPrecise(gpa, module_err_msg.reference_trace.len + + @intFromBool(remaining_references != null)); + + for (module_err_msg.reference_trace) |module_reference| { const source = try module_reference.src_loc.file_scope.getSource(gpa); const span = try module_reference.src_loc.span(gpa); const loc = std.zig.findLineColumn(source.bytes, span.main); const rt_file_path = try module_reference.src_loc.file_scope.fullPath(gpa); defer gpa.free(rt_file_path); - try ref_traces.append(gpa, .{ - .decl_name = try eb.addString(ip.stringToSliceUnwrap(module_reference.decl).?), + ref_traces.appendAssumeCapacity(.{ + .decl_name = try eb.addString(ip.stringToSlice(module_reference.decl)), .src_loc = try eb.addSourceLocation(.{ .src_path = try eb.addString(rt_file_path), .span_start = span.start, .span_main = span.main, .span_end = span.end, - .line = @as(u32, @intCast(loc.line)), - .column = @as(u32, @intCast(loc.column)), + .line = @intCast(loc.line), + .column = @intCast(loc.column), .source_line = 0, }), }); } + if (remaining_references) |remaining| ref_traces.appendAssumeCapacity( + .{ .decl_name = remaining, .src_loc = .none }, + ); const src_loc = try eb.addSourceLocation(.{ .src_path = try eb.addString(file_path), .span_start = err_span.start, .span_main = err_span.main, .span_end = err_span.end, - .line = @as(u32, @intCast(err_loc.line)), - .column = @as(u32, @intCast(err_loc.column)), + .line = @intCast(err_loc.line), + .column = @intCast(err_loc.column), .source_line = if (module_err_msg.src_loc.lazy == .entire_file) 0 else try eb.addString(err_loc.source_line), - .reference_trace_len = @as(u32, @intCast(ref_traces.items.len)), + .reference_trace_len = @intCast(ref_traces.items.len), }); for (ref_traces.items) |rt| { @@ -2928,8 +2929,8 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod .span_start = span.start, .span_main = span.main, .span_end = span.end, - .line = @as(u32, @intCast(loc.line)), - .column = @as(u32, @intCast(loc.column)), + .line = @intCast(loc.line), + .column = @intCast(loc.column), .source_line = if (err_loc.eql(loc)) 0 else try eb.addString(loc.source_line), }), }, .{ .eb = eb }); @@ -2938,7 +2939,7 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod } } - const notes_len = @as(u32, @intCast(notes.entries.len)); + const notes_len: u32 = @intCast(notes.entries.len); try eb.addRootErrorMessage(.{ .msg = try eb.addString(module_err_msg.msg), diff --git a/src/Module.zig b/src/Module.zig index c13be489bb..ba76c16faf 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1519,11 +1519,11 @@ pub const ErrorMsg = struct { msg: []const u8, notes: []ErrorMsg = &.{}, reference_trace: []Trace = &.{}, + hidden_references: u32 = 0, pub const Trace = struct { - decl: InternPool.OptionalNullTerminatedString, + decl: InternPool.NullTerminatedString, src_loc: SrcLoc, - hidden: u32 = 0, }; pub fn create( @@ -4147,7 +4147,9 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 }; const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 }; const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 }; - const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, "global variable initializer must be comptime-known"); + const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, .{ + .needed_comptime_reason = "global variable initializer must be comptime-known", + }); // Note this resolves the type of the Decl, not the value; if this Decl // is a struct, for example, this resolves `type` (which needs no resolution), @@ -4257,7 +4259,9 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { decl.@"linksection" = blk: { const linksection_ref = decl.zirLinksectionRef(mod); if (linksection_ref == .none) break :blk .none; - const bytes = try sema.resolveConstString(&block_scope, section_src, linksection_ref, "linksection must be comptime-known"); + const bytes = try sema.resolveConstString(&block_scope, section_src, linksection_ref, .{ + .needed_comptime_reason = "linksection must be comptime-known", + }); if (mem.indexOfScalar(u8, bytes, 0) != null) { return sema.fail(&block_scope, section_src, "linksection cannot contain null bytes", .{}); } else if (bytes.len == 0) { diff --git a/src/Sema.zig b/src/Sema.zig index a0af64fc83..b843081a6a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -269,7 +269,7 @@ pub const InstMap = struct { while (true) { const extra_capacity = better_capacity / 2 + 16; better_capacity += extra_capacity; - better_start -|= @as(Zir.Inst.Index, @intCast(extra_capacity / 2)); + better_start -|= @intCast(extra_capacity / 2); if (better_start <= start and end < better_capacity + better_start) break; } @@ -282,7 +282,7 @@ pub const InstMap = struct { allocator.free(map.items); map.items = new_items; - map.start = @as(Zir.Inst.Index, @intCast(better_start)); + map.start = @intCast(better_start); } }; @@ -405,7 +405,9 @@ pub const Block = struct { /// It is shared among all the blocks in an inline or comptime called /// function. pub const Inlining = struct { - /// Might be `none`. + call_block: *Block, + call_src: LazySrcLoc, + has_comptime_args: bool, func: InternPool.Index, comptime_result: Air.Inst.Ref, merges: Merges, @@ -681,7 +683,7 @@ pub const Block = struct { const sema = block.sema; const ty_ref = Air.internedToRef(aggregate_ty.toIntern()); try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements.len); - const extra_index = @as(u32, @intCast(sema.air_extra.items.len)); + const extra_index: u32 = @intCast(sema.air_extra.items.len); sema.appendRefsAssumeCapacity(elements); return block.addInst(.{ @@ -722,7 +724,7 @@ pub const Block = struct { try sema.air_instructions.ensureUnusedCapacity(gpa, 1); try block.instructions.ensureUnusedCapacity(gpa, 1); - const result_index = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const result_index: Air.Inst.Index = @intCast(sema.air_instructions.len); sema.air_instructions.appendAssumeCapacity(inst); block.instructions.appendAssumeCapacity(result_index); return result_index; @@ -740,7 +742,7 @@ pub const Block = struct { try sema.air_instructions.ensureUnusedCapacity(gpa, 1); - const result_index = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const result_index: Air.Inst.Index = @intCast(sema.air_instructions.len); sema.air_instructions.appendAssumeCapacity(inst); try block.instructions.insert(gpa, index, result_index); @@ -818,6 +820,11 @@ const InferredAlloc = struct { }) = .{}, }; +const NeededComptimeReason = struct { + needed_comptime_reason: []const u8, + block_comptime_reason: ?*const Block.ComptimeReason = null, +}; + pub fn deinit(sema: *Sema) void { const gpa = sema.gpa; sema.air_instructions.deinit(gpa); @@ -849,7 +856,7 @@ fn resolveBody( body_inst: Zir.Inst.Index, ) CompileError!Air.Inst.Ref { const break_data = (try sema.analyzeBodyBreak(block, body)) orelse - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; // For comptime control flow, we need to detect when `analyzeBody` reports // that we need to break from an outer block. In such case we // use Zig's error mechanism to send control flow up the stack until @@ -1455,7 +1462,7 @@ fn analyzeBodyInner( try sema.errNote(block, runtime_src, msg, "runtime control flow here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } i += 1; @@ -1654,10 +1661,10 @@ fn analyzeBodyInner( const extra = sema.code.extraData(Zir.Inst.CondBr, inst_data.payload_index); const then_body = sema.code.extra[extra.end..][0..extra.data.then_body_len]; const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; - const cond = sema.resolveInstConst(block, cond_src, extra.data.condition, "condition in comptime branch must be comptime-known") catch |err| { - if (err == error.AnalysisFail and block.comptime_reason != null) try block.comptime_reason.?.explain(sema, sema.err); - return err; - }; + const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition, .{ + .needed_comptime_reason = "condition in comptime branch must be comptime-known", + .block_comptime_reason = block.comptime_reason, + }); const inline_body = if (cond.val.toBool()) then_body else else_body; try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src); @@ -1675,10 +1682,10 @@ fn analyzeBodyInner( const extra = sema.code.extraData(Zir.Inst.CondBr, inst_data.payload_index); const then_body = sema.code.extra[extra.end..][0..extra.data.then_body_len]; const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; - const cond = sema.resolveInstConst(block, cond_src, extra.data.condition, "condition in comptime branch must be comptime-known") catch |err| { - if (err == error.AnalysisFail and block.comptime_reason != null) try block.comptime_reason.?.explain(sema, sema.err); - return err; - }; + const cond = try sema.resolveInstConst(block, cond_src, extra.data.condition, .{ + .needed_comptime_reason = "condition in comptime branch must be comptime-known", + .block_comptime_reason = block.comptime_reason, + }); const inline_body = if (cond.val.toBool()) then_body else else_body; try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src); @@ -1708,10 +1715,10 @@ fn analyzeBodyInner( } const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union); assert(is_non_err != .none); - const is_non_err_val = sema.resolveConstValue(block, operand_src, is_non_err, "try operand inside comptime block must be comptime-known") catch |err| { - if (err == error.AnalysisFail and block.comptime_reason != null) try block.comptime_reason.?.explain(sema, sema.err); - return err; - }; + const is_non_err_val = try sema.resolveConstValue(block, operand_src, is_non_err, .{ + .needed_comptime_reason = "try operand inside comptime block must be comptime-known", + .block_comptime_reason = block.comptime_reason, + }); if (is_non_err_val.toBool()) { break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false); } @@ -1734,10 +1741,10 @@ fn analyzeBodyInner( const err_union = try sema.analyzeLoad(block, src, operand, operand_src); const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union); assert(is_non_err != .none); - const is_non_err_val = sema.resolveConstValue(block, operand_src, is_non_err, "try operand inside comptime block must be comptime-known") catch |err| { - if (err == error.AnalysisFail and block.comptime_reason != null) try block.comptime_reason.?.explain(sema, sema.err); - return err; - }; + const is_non_err_val = try sema.resolveConstValue(block, operand_src, is_non_err, .{ + .needed_comptime_reason = "try operand inside comptime block must be comptime-known", + .block_comptime_reason = block.comptime_reason, + }); if (is_non_err_val.toBool()) { break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false); } @@ -1757,7 +1764,7 @@ fn analyzeBodyInner( else => |e| return e, }; if (break_inst != defer_body[defer_body.len - 1]) break always_noreturn; - break :blk Air.Inst.Ref.void_value; + break :blk .void_value; }, .defer_err_code => blk: { const inst_data = sema.code.instructions.items(.data)[inst].defer_err_code; @@ -1770,7 +1777,7 @@ fn analyzeBodyInner( else => |e| return e, }; if (break_inst != defer_body[defer_body.len - 1]) break always_noreturn; - break :blk Air.Inst.Ref.void_value; + break :blk .void_value; }, }; if (sema.isNoReturn(air_inst)) { @@ -1819,7 +1826,7 @@ pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) !Air.Inst.Ref { const i = @intFromEnum(zir_ref); // First section of indexes correspond to a set number of constant values. // We intentionally map the same indexes to the same values between ZIR and AIR. - if (i < InternPool.static_len) return @as(Air.Inst.Ref, @enumFromInt(i)); + if (i < InternPool.static_len) return @enumFromInt(i); // The last section of indexes refers to the map of ZIR => AIR. const inst = sema.inst_map.get(i - InternPool.static_len).?; if (inst == .generic_poison) return error.GenericPoison; @@ -1831,7 +1838,7 @@ fn resolveConstBool( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) !bool { const air_inst = try sema.resolveInst(zir_ref); const wanted_type = Type.bool; @@ -1845,7 +1852,7 @@ pub fn resolveConstString( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) ![]u8 { const air_inst = try sema.resolveInst(zir_ref); const wanted_type = Type.slice_const_u8; @@ -1859,7 +1866,7 @@ pub fn resolveConstStringIntern( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) !InternPool.NullTerminatedString { const air_inst = try sema.resolveInst(zir_ref); const wanted_type = Type.slice_const_u8; @@ -1905,7 +1912,7 @@ fn resolveDestType( try sema.errNote(block, src, msg, "use @as to provide explicit result type", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, else => |e| return e, }; @@ -1931,7 +1938,9 @@ fn analyzeAsType( ) !Type { const wanted_type = Type.type; const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src); - const val = try sema.resolveConstValue(block, src, coerced_inst, "types must be comptime-known"); + const val = try sema.resolveConstValue(block, src, coerced_inst, .{ + .needed_comptime_reason = "types must be comptime-known", + }); return val.toType(); } @@ -1984,7 +1993,7 @@ fn resolveValue( block: *Block, src: LazySrcLoc, air_ref: Air.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!Value { if (try sema.resolveMaybeUndefValAllowVariables(air_ref)) |val| { if (val.isGenericPoison()) return error.GenericPoison; @@ -2000,7 +2009,7 @@ fn resolveConstMaybeUndefVal( block: *Block, src: LazySrcLoc, inst: Air.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!Value { if (try sema.resolveMaybeUndefValAllowVariables(inst)) |val| { if (val.isGenericPoison()) return error.GenericPoison; @@ -2018,7 +2027,7 @@ fn resolveConstValue( block: *Block, src: LazySrcLoc, air_ref: Air.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!Value { if (try sema.resolveMaybeUndefValAllowVariables(air_ref)) |val| { if (val.isGenericPoison()) return error.GenericPoison; @@ -2037,7 +2046,7 @@ fn resolveConstLazyValue( block: *Block, src: LazySrcLoc, air_ref: Air.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!Value { return sema.resolveLazyValue(try sema.resolveConstValue(block, src, air_ref, reason)); } @@ -2140,15 +2149,18 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime( return val; } -fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: []const u8) CompileError { +fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError { const msg = msg: { const msg = try sema.errMsg(block, src, "unable to resolve comptime value", .{}); errdefer msg.destroy(sema.gpa); + try sema.errNote(block, src, msg, "{s}", .{reason.needed_comptime_reason}); - try sema.errNote(block, src, msg, "{s}", .{reason}); + if (reason.block_comptime_reason) |block_comptime_reason| { + try block_comptime_reason.explain(sema, msg); + } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError { @@ -2181,7 +2193,7 @@ fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithStructInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError { @@ -2213,7 +2225,7 @@ fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty: try sema.errNote(block, src, msg, "when computing vector element at index '{d}'", .{vector_index}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return sema.fail(block, src, "overflow of integer type '{}' with value '{}'", .{ int_ty.fmt(sema.mod), val.fmtValue(int_ty, sema.mod), @@ -2234,7 +2246,7 @@ fn failWithInvalidComptimeFieldStore(sema: *Sema, block: *Block, init_src: LazyS try mod.errNoteNonLazy(default_value_src, msg, "default value set here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithUseOfAsync(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError { @@ -2243,7 +2255,7 @@ fn failWithUseOfAsync(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError errdefer msg.destroy(sema.gpa); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithInvalidFieldAccess( @@ -2265,7 +2277,7 @@ fn failWithInvalidFieldAccess( try sema.errNote(block, src, msg, "consider using '.?', 'orelse', or 'if'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } else if (inner_ty.zigTypeTag(mod) == .ErrorUnion) err: { const child_ty = inner_ty.errorUnionPayload(mod); if (!typeSupportsFieldAccess(mod, child_ty, field_name)) break :err; @@ -2275,7 +2287,7 @@ fn failWithInvalidFieldAccess( try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return sema.fail(block, src, "type '{}' does not support field access", .{object_ty.fmt(sema.mod)}); } @@ -2376,78 +2388,77 @@ pub fn fail( args: anytype, ) CompileError { const err_msg = try sema.errMsg(block, src, format, args); - return sema.failWithOwnedErrorMsg(err_msg); + return sema.failWithOwnedErrorMsg(block, err_msg); } -fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError { +fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Module.ErrorMsg) CompileError { @setCold(true); const gpa = sema.gpa; const mod = sema.mod; - if (crash_report.is_enabled and mod.comp.debug_compile_errors) { - if (err_msg.src_loc.lazy == .unneeded) return error.NeededSourceLocation; - var wip_errors: std.zig.ErrorBundle.Wip = undefined; - wip_errors.init(gpa) catch unreachable; - Compilation.addModuleErrorMsg(mod, &wip_errors, err_msg.*) catch unreachable; - std.debug.print("compile error during Sema:\n", .{}); - var error_bundle = wip_errors.toOwnedBundle("") catch unreachable; - error_bundle.renderToStdErr(.{ .ttyconf = .no_color }); - crash_report.compilerPanic("unexpected compile error occurred", null, null); - } - ref: { errdefer err_msg.destroy(gpa); - if (err_msg.src_loc.lazy == .unneeded) { - return error.NeededSourceLocation; + if (err_msg.src_loc.lazy == .unneeded) return error.NeededSourceLocation; + + if (crash_report.is_enabled and mod.comp.debug_compile_errors) { + var wip_errors: std.zig.ErrorBundle.Wip = undefined; + wip_errors.init(gpa) catch unreachable; + Compilation.addModuleErrorMsg(mod, &wip_errors, err_msg.*) catch unreachable; + std.debug.print("compile error during Sema:\n", .{}); + var error_bundle = wip_errors.toOwnedBundle("") catch unreachable; + error_bundle.renderToStdErr(.{ .ttyconf = .no_color }); + crash_report.compilerPanic("unexpected compile error occurred", null, null); } + try mod.failed_decls.ensureUnusedCapacity(gpa, 1); try mod.failed_files.ensureUnusedCapacity(gpa, 1); - const max_references = blk: { - if (mod.comp.reference_trace) |num| break :blk num; - // Do not add multiple traces without explicit request. - if (mod.failed_decls.count() != 0) break :ref; - break :blk default_reference_trace_len; - }; + if (block) |start_block| { + var block_it = start_block; + while (block_it.inlining) |inlining| { + try sema.errNote( + inlining.call_block, + inlining.call_src, + err_msg, + "called from here", + .{}, + ); + block_it = inlining.call_block; + } - var referenced_by = if (sema.owner_func_index != .none) - mod.funcOwnerDeclIndex(sema.owner_func_index) - else - sema.owner_decl_index; - var reference_stack = std.ArrayList(Module.ErrorMsg.Trace).init(gpa); - defer reference_stack.deinit(); - - // Avoid infinite loops. - var seen = std.AutoHashMap(Decl.Index, void).init(gpa); - defer seen.deinit(); - - var cur_reference_trace: u32 = 0; - while (sema.mod.reference_table.get(referenced_by)) |ref| : (cur_reference_trace += 1) { - const gop = try seen.getOrPut(ref.referencer); - if (gop.found_existing) break; - if (cur_reference_trace < max_references) { - const decl = sema.mod.declPtr(ref.referencer); - try reference_stack.append(.{ - .decl = decl.name.toOptional(), - .src_loc = ref.src.toSrcLoc(decl, mod), - }); + const max_references = refs: { + if (mod.comp.reference_trace) |num| break :refs num; + // Do not add multiple traces without explicit request. + if (mod.failed_decls.count() > 0) break :ref; + break :refs default_reference_trace_len; + }; + + var referenced_by = if (sema.owner_func_index != .none) + mod.funcOwnerDeclIndex(sema.owner_func_index) + else + sema.owner_decl_index; + var reference_stack = std.ArrayList(Module.ErrorMsg.Trace).init(gpa); + defer reference_stack.deinit(); + + // Avoid infinite loops. + var seen = std.AutoHashMap(Decl.Index, void).init(gpa); + defer seen.deinit(); + + while (mod.reference_table.get(referenced_by)) |ref| { + const gop = try seen.getOrPut(ref.referencer); + if (gop.found_existing) break; + if (reference_stack.items.len < max_references) { + const decl = mod.declPtr(ref.referencer); + try reference_stack.append(.{ + .decl = decl.name, + .src_loc = ref.src.toSrcLoc(decl, mod), + }); + } + referenced_by = ref.referencer; } - referenced_by = ref.referencer; - } - if (sema.mod.comp.reference_trace == null and cur_reference_trace > 0) { - try reference_stack.append(.{ - .decl = .none, - .src_loc = undefined, - .hidden = 0, - }); - } else if (cur_reference_trace > max_references) { - try reference_stack.append(.{ - .decl = undefined, - .src_loc = undefined, - .hidden = cur_reference_trace - max_references, - }); + err_msg.reference_trace = try reference_stack.toOwnedSlice(); + err_msg.hidden_references = @intCast(seen.count() -| max_references); } - err_msg.reference_trace = try reference_stack.toOwnedSlice(); } const ip = &mod.intern_pool; if (sema.owner_func_index != .none) { @@ -2507,8 +2518,10 @@ fn analyzeAsAlign( src: LazySrcLoc, air_ref: Air.Inst.Ref, ) !Alignment { - const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, "alignment must be comptime-known"); - const alignment = @as(u32, @intCast(alignment_big)); // We coerce to u29 in the prev line. + const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{ + .needed_comptime_reason = "alignment must be comptime-known", + }); + const alignment: u32 = @intCast(alignment_big); // We coerce to u29 in the prev line. try sema.validateAlign(block, src, alignment); return Alignment.fromNonzeroByteUnits(alignment); } @@ -2543,7 +2556,7 @@ fn resolveInt( src: LazySrcLoc, zir_ref: Zir.Inst.Ref, dest_ty: Type, - reason: []const u8, + reason: NeededComptimeReason, ) !u64 { const air_ref = try sema.resolveInst(zir_ref); return sema.analyzeAsInt(block, src, air_ref, dest_ty, reason); @@ -2555,7 +2568,7 @@ fn analyzeAsInt( src: LazySrcLoc, air_ref: Air.Inst.Ref, dest_ty: Type, - reason: []const u8, + reason: NeededComptimeReason, ) !u64 { const mod = sema.mod; const coerced = try sema.coerce(block, dest_ty, air_ref, src); @@ -2570,7 +2583,7 @@ pub fn resolveInstConst( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!TypedValue { const air_ref = try sema.resolveInst(zir_ref); const val = try sema.resolveConstValue(block, src, air_ref, reason); @@ -2587,7 +2600,7 @@ pub fn resolveInstValue( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!TypedValue { const air_ref = try sema.resolveInst(zir_ref); const val = try sema.resolveValue(block, src, air_ref, reason); @@ -2742,7 +2755,7 @@ fn coerceResultPtr( if (pointee_ty.eql(Type.null, sema.mod)) { const null_inst = Air.internedToRef(Value.null.toIntern()); _ = try block.addBinOp(.store, new_ptr, null_inst); - return Air.Inst.Ref.void_value; + return .void_value; } return sema.bitCast(block, ptr_ty, new_ptr, src, null); } @@ -2815,7 +2828,7 @@ pub fn analyzeStructDecl( const struct_obj = mod.structPtr(struct_index); const extended = sema.code.instructions.items(.data)[inst].extended; assert(extended.opcode == .struct_decl); - const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); struct_obj.known_non_opv = small.known_non_opv; if (small.known_comptime_only) { @@ -2852,9 +2865,9 @@ fn zirStructDecl( ) CompileError!Air.Inst.Ref { const mod = sema.mod; const gpa = sema.gpa; - const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); const src: LazySrcLoc = if (small.has_src_node) blk: { - const node_offset = @as(i32, @bitCast(sema.code.extra[extended.operand])); + const node_offset: i32 = @bitCast(sema.code.extra[extended.operand]); break :blk LazySrcLoc.nodeOffset(node_offset); } else sema.src; @@ -2972,7 +2985,7 @@ fn createAnonymousDeclTypeNamed( // If not then this is a struct type being returned from a non-generic // function and the name doesn't matter since it will later // result in a compile error. - const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, "") catch + const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg, undefined) catch return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null); if (arg_i != 0) try writer.writeByte(','); @@ -3214,20 +3227,22 @@ fn zirEnumDecl( try sema.errNote(block, other_field_src, msg, "other field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const tag_overflow = if (has_tag_value) overflow: { - const tag_val_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const tag_val_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; const tag_inst = try sema.resolveInst(tag_val_ref); - last_tag_val = sema.resolveConstValue(block, .unneeded, tag_inst, "") catch |err| switch (err) { + last_tag_val = sema.resolveConstValue(block, .unneeded, tag_inst, undefined) catch |err| switch (err) { error.NeededSourceLocation => { const value_src = mod.fieldSrcLoc(new_decl_index, .{ .index = field_i, .range = .value, }).lazy; - _ = try sema.resolveConstValue(block, value_src, tag_inst, "enum tag value must be comptime-known"); + _ = try sema.resolveConstValue(block, value_src, tag_inst, .{ + .needed_comptime_reason = "enum tag value must be comptime-known", + }); unreachable; }, else => |e| return e, @@ -3246,7 +3261,7 @@ fn zirEnumDecl( try sema.errNote(block, other_field_src, msg, "other occurrence here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } break :overflow false; } else if (any_values) overflow: { @@ -3265,7 +3280,7 @@ fn zirEnumDecl( try sema.errNote(block, other_field_src, msg, "other occurrence here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } break :overflow false; } else overflow: { @@ -3283,7 +3298,7 @@ fn zirEnumDecl( const msg = try sema.errMsg(block, value_src, "enumeration value '{}' too large for type '{}'", .{ last_tag_val.?.fmtValue(int_tag_ty, mod), int_tag_ty.fmt(mod), }); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } return decl_val; @@ -3300,11 +3315,11 @@ fn zirUnionDecl( const mod = sema.mod; const gpa = sema.gpa; - const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small); var extra_index: usize = extended.operand; const src: LazySrcLoc = if (small.has_src_node) blk: { - const node_offset = @as(i32, @bitCast(sema.code.extra[extra_index])); + const node_offset: i32 = @bitCast(sema.code.extra[extra_index]); extra_index += 1; break :blk LazySrcLoc.nodeOffset(node_offset); } else sema.src; @@ -3398,11 +3413,11 @@ fn zirOpaqueDecl( defer tracy.end(); const mod = sema.mod; - const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small); var extra_index: usize = extended.operand; const src: LazySrcLoc = if (small.has_src_node) blk: { - const node_offset = @as(i32, @bitCast(sema.code.extra[extra_index])); + const node_offset: i32 = @bitCast(sema.code.extra[extra_index]); extra_index += 1; break :blk LazySrcLoc.nodeOffset(node_offset); } else sema.src; @@ -3469,7 +3484,7 @@ fn zirErrorSetDecl( var names: InferredErrorSet.NameMap = .{}; try names.ensureUnusedCapacity(sema.arena, extra.data.fields_len); - var extra_index = @as(u32, @intCast(extra.end)); + var extra_index: u32 = @intCast(extra.end); const extra_index_end = extra_index + (extra.data.fields_len * 2); while (extra_index < extra_index_end) : (extra_index += 2) { // +2 to skip over doc_string const str_index = sema.code.extra[extra_index]; @@ -3556,7 +3571,7 @@ fn ensureResultUsed( try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, else => { const msg = msg: { @@ -3566,7 +3581,7 @@ fn ensureResultUsed( try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } } @@ -3588,7 +3603,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com try sema.errNote(block, src, msg, "consider using 'try', 'catch', or 'if'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, else => return, } @@ -3616,7 +3631,7 @@ fn zirEnsureErrUnionPayloadVoid(sema: *Sema, block: *Block, inst: Zir.Inst.Index try sema.errNote(block, src, msg, "payload value can be explicitly ignored with '|_|'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -3669,18 +3684,18 @@ fn zirAllocExtended( const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand); const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node }; const align_src: LazySrcLoc = .{ .node_offset_var_decl_align = extra.data.src_node }; - const small = @as(Zir.Inst.AllocExtended.Small, @bitCast(extended.small)); + const small: Zir.Inst.AllocExtended.Small = @bitCast(extended.small); var extra_index: usize = extra.end; const var_ty: Type = if (small.has_type) blk: { - const type_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const type_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; break :blk try sema.resolveType(block, ty_src, type_ref); } else undefined; const alignment = if (small.has_align) blk: { - const align_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; const alignment = try sema.resolveAlign(block, align_src, align_ref); break :blk alignment; @@ -3698,7 +3713,7 @@ fn zirAllocExtended( .is_const = small.is_const, } }, }); - return Air.indexToRef(@as(u32, @intCast(sema.air_instructions.len - 1))); + return Air.indexToRef(@intCast(sema.air_instructions.len - 1)); } } @@ -3830,7 +3845,7 @@ fn zirAllocInferredComptime( .is_const = is_const, } }, }); - return Air.indexToRef(@as(u32, @intCast(sema.air_instructions.len - 1))); + return Air.indexToRef(@intCast(sema.air_instructions.len - 1)); } fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { @@ -3895,7 +3910,7 @@ fn zirAllocInferred( .is_const = is_const, } }, }); - return Air.indexToRef(@as(u32, @intCast(sema.air_instructions.len - 1))); + return Air.indexToRef(@intCast(sema.air_instructions.len - 1)); } const result_index = try block.addInstAsIndex(.{ @@ -4147,7 +4162,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com .data = .{ .ty_pl = .{ .ty = ty_inst, .payload = sema.addExtraAssumeCapacity(Air.Block{ - .body_len = @as(u32, @intCast(replacement_block.instructions.items.len)), + .body_len = @intCast(replacement_block.instructions.items.len), }), } }, }); @@ -4231,7 +4246,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. // First pass to look for comptime values. for (args, 0..) |zir_arg, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); runtime_arg_lens[i] = .none; if (zir_arg == .none) continue; const object = try sema.resolveInst(zir_arg); @@ -4255,7 +4270,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. try sema.errNote(block, arg_src, msg, "for loop operand must be a range, array, slice, tuple, or vector", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (!object_ty.indexableHasLen(mod)) continue; @@ -4284,7 +4299,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. }); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } else { len = arg_len; @@ -4302,7 +4317,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. const msg = try sema.errMsg(block, src, "unbounded for loop", .{}); errdefer msg.destroy(gpa); for (args, 0..) |zir_arg, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); if (zir_arg == .none) continue; const object = try sema.resolveInst(zir_arg); const object_ty = sema.typeOf(object); @@ -4322,7 +4337,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // Now for the runtime checks. @@ -4497,7 +4512,7 @@ fn validateUnionInit( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (block.is_comptime and @@ -4611,7 +4626,9 @@ fn validateUnionInit( try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store); return; } else if (try sema.typeRequiresComptime(union_ty)) { - return sema.failWithNeededComptime(block, field_ptr_data.src(), "initializer of comptime only union must be comptime-known"); + return sema.failWithNeededComptime(block, field_ptr_data.src(), .{ + .needed_comptime_reason = "initializer of comptime only union must be comptime-known", + }); } const new_tag = Air.internedToRef(tag_val.toIntern()); @@ -4662,7 +4679,7 @@ fn validateStructInit( try sema.errNote(block, other_field_src, msg, "other field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } found_fields[field_index.*] = field_ptr; } @@ -4705,9 +4722,9 @@ fn validateStructInit( const field_src = init_src; // TODO better source location const default_field_ptr = if (struct_ty.isTuple(mod)) - try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @as(u32, @intCast(i)), true) + try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @intCast(i), true) else - try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @as(u32, @intCast(i)), field_src, struct_ty, true); + try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(i), field_src, struct_ty, true); const init = Air.internedToRef(default_val.toIntern()); try sema.storePtr2(block, init_src, default_field_ptr, init_src, init, field_src, .store); } @@ -4723,7 +4740,7 @@ fn validateStructInit( ); } root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return; @@ -4806,7 +4823,9 @@ fn validateStructInit( field_values[i] = val.toIntern(); } else if (require_comptime) { const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node; - return sema.failWithNeededComptime(block, field_ptr_data.src(), "initializer of comptime only struct must be comptime-known"); + return sema.failWithNeededComptime(block, field_ptr_data.src(), .{ + .needed_comptime_reason = "initializer of comptime only struct must be comptime-known", + }); } else { struct_is_comptime = false; } @@ -4851,7 +4870,7 @@ fn validateStructInit( ); } root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (struct_is_comptime) { @@ -4911,9 +4930,9 @@ fn validateStructInit( const field_src = init_src; // TODO better source location const default_field_ptr = if (struct_ty.isTuple(mod)) - try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @as(u32, @intCast(i)), true) + try sema.tupleFieldPtr(block, init_src, struct_ptr, field_src, @intCast(i), true) else - try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @as(u32, @intCast(i)), field_src, struct_ty, true); + try sema.structFieldPtrByIndex(block, init_src, struct_ptr, @intCast(i), field_src, struct_ty, true); const init = Air.internedToRef(field_values[i]); try sema.storePtr2(block, init_src, default_field_ptr, init_src, init, field_src, .store); } @@ -4955,7 +4974,7 @@ fn zirValidateArrayInit( if (root_msg) |msg| { root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } }, .Array => { @@ -5168,7 +5187,7 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr try sema.explainWhyTypeIsComptime(msg, src.toSrcLoc(src_decl, mod), elem_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -5200,7 +5219,7 @@ fn failWithBadMemberAccess( try sema.addDeclaredHereNote(msg, agg_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithBadStructFieldAccess( @@ -5226,7 +5245,7 @@ fn failWithBadStructFieldAccess( try mod.errNoteNonLazy(struct_obj.srcLoc(mod), msg, "struct declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn failWithBadUnionFieldAccess( @@ -5253,7 +5272,7 @@ fn failWithBadUnionFieldAccess( try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "union declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn addDeclaredHereNote(sema: *Sema, parent: *Module.ErrorMsg, decl_ty: Type) !void { @@ -5331,13 +5350,17 @@ fn storeToInferredAllocComptime( return; } - return sema.failWithNeededComptime(block, src, "value being stored to a comptime variable must be comptime-known"); + return sema.failWithNeededComptime(block, src, .{ + .needed_comptime_reason = "value being stored to a comptime variable must be comptime-known", + }); } fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { const inst_data = sema.code.instructions.items(.data)[inst].un_node; const src = inst_data.src(); - const quota = @as(u32, @intCast(try sema.resolveInt(block, src, inst_data.operand, Type.u32, "eval branch quota must be comptime-known"))); + const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, Type.u32, .{ + .needed_comptime_reason = "eval branch quota must be comptime-known", + })); sema.branch_quota = @max(sema.branch_quota, quota); } @@ -5479,7 +5502,9 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const inst_data = sema.code.instructions.items(.data)[inst].un_node; const src = inst_data.src(); const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; - const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, "compile error string must be comptime-known"); + const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ + .needed_comptime_reason = "compile error string must be comptime-known", + }); return sema.fail(block, src, "{s}", .{msg}); } @@ -5520,7 +5545,7 @@ fn zirCompileLog( if (!gop.found_existing) { gop.value_ptr.* = src_node; } - return Air.Inst.Ref.void_value; + return .void_value; } fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index { @@ -5558,7 +5583,7 @@ fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError // Reserve space for a Loop instruction so that generated Break instructions can // point to it, even if it doesn't end up getting used because the code ends up being // comptime evaluated. - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); const loop_inst = block_inst + 1; try sema.air_instructions.ensureUnusedCapacity(gpa, 2); sema.air_instructions.appendAssumeCapacity(.{ @@ -5606,7 +5631,7 @@ fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len + loop_block_len); sema.air_instructions.items(.data)[loop_inst].ty_pl.payload = sema.addExtraAssumeCapacity( - Air.Block{ .body_len = @as(u32, @intCast(loop_block_len)) }, + Air.Block{ .body_len = @intCast(loop_block_len) }, ); sema.air_extra.appendSliceAssumeCapacity(loop_block.instructions.items); } @@ -5713,7 +5738,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&child_block, msg); } const c_import_pkg = Package.create( sema.gpa, @@ -5756,7 +5781,7 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt // Reserve space for a Block instruction so that generated Break instructions can // point to it, even if it doesn't end up getting used because the code ends up being // comptime evaluated or is an unlabeled block. - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ .tag = .block, .data = undefined, @@ -5894,7 +5919,7 @@ fn analyzeBlockBody( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(child_block, msg); } const ty_inst = Air.internedToRef(resolved_ty.toIntern()); try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len + @@ -5902,7 +5927,7 @@ fn analyzeBlockBody( sema.air_instructions.items(.data)[merges.block_inst] = .{ .ty_pl = .{ .ty = ty_inst, .payload = sema.addExtraAssumeCapacity(Air.Block{ - .body_len = @as(u32, @intCast(child_block.instructions.items.len)), + .body_len = @intCast(child_block.instructions.items.len), }), } }; sema.air_extra.appendSliceAssumeCapacity(child_block.instructions.items); @@ -5929,15 +5954,15 @@ fn analyzeBlockBody( // Convert the br instruction to a block instruction that has the coercion // and then a new br inside that returns the coerced instruction. - const sub_block_len = @as(u32, @intCast(coerce_block.instructions.items.len + 1)); + const sub_block_len: u32 = @intCast(coerce_block.instructions.items.len + 1); try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len + sub_block_len); try sema.air_instructions.ensureUnusedCapacity(gpa, 1); - const sub_br_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const sub_br_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); sema.air_instructions.items(.tag)[br] = .block; sema.air_instructions.items(.data)[br] = .{ .ty_pl = .{ - .ty = Air.Inst.Ref.noreturn_type, + .ty = .noreturn_type, .payload = sema.addExtraAssumeCapacity(Air.Block{ .body_len = sub_block_len, }), @@ -6001,7 +6026,9 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError const src = inst_data.src(); const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; - const operand = try sema.resolveInstConst(block, operand_src, extra.operand, "export target must be comptime-known"); + const operand = try sema.resolveInstConst(block, operand_src, extra.operand, .{ + .needed_comptime_reason = "export target must be comptime-known", + }); const options = sema.resolveExportOptions(block, .unneeded, extra.options) catch |err| switch (err) { error.NeededSourceLocation => { _ = try sema.resolveExportOptions(block, options_src, extra.options); @@ -6045,7 +6072,7 @@ pub fn analyzeExport( try sema.addDeclaredHereNote(msg, exported_decl.ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // TODO: some backends might support re-exporting extern decls @@ -6122,7 +6149,7 @@ fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst try sema.errNote(block, prev_src, msg, "other instance here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const ip = &mod.intern_pool; @@ -6140,7 +6167,9 @@ fn zirSetCold(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) const ip = &mod.intern_pool; const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; - const is_cold = try sema.resolveConstBool(block, operand_src, extra.operand, "operand to @setCold must be comptime-known"); + const is_cold = try sema.resolveConstBool(block, operand_src, extra.operand, .{ + .needed_comptime_reason = "operand to @setCold must be comptime-known", + }); if (sema.func_index == .none) return; // does nothing outside a function ip.funcAnalysis(sema.func_index).is_cold = is_cold; } @@ -6148,13 +6177,17 @@ fn zirSetCold(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; - block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", "operand to @setFloatMode must be comptime-known"); + block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{ + .needed_comptime_reason = "operand to @setFloatMode must be comptime-known", + }); } fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { const inst_data = sema.code.instructions.items(.data)[inst].un_node; const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; - block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, "operand to @setRuntimeSafety must be comptime-known"); + block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{ + .needed_comptime_reason = "operand to @setRuntimeSafety must be comptime-known", + }); } fn zirFence(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { @@ -6162,7 +6195,9 @@ fn zirFence(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) Co const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; - const order = try sema.resolveAtomicOrder(block, order_src, extra.operand, "atomic order of @fence must be comptime-known"); + const order = try sema.resolveAtomicOrder(block, order_src, extra.operand, .{ + .needed_comptime_reason = "atomic order of @fence must be comptime-known", + }); if (@intFromEnum(order) < @intFromEnum(std.builtin.AtomicOrder.Acquire)) { return sema.fail(block, order_src, "atomic ordering must be Acquire or stricter", .{}); @@ -6291,7 +6326,7 @@ fn addDbgVar( try sema.queueFullTypeResolution(operand_ty); // Add the name to the AIR. - const name_extra_index = @as(u32, @intCast(sema.air_extra.items.len)); + const name_extra_index: u32 = @intCast(sema.air_extra.items.len); const elements_used = name.len / 4 + 1; try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used); const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); @@ -6431,7 +6466,7 @@ fn lookupInNamespace( } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } } else if (namespace.decls.getKeyAdapted(ident_name, Module.DeclAdapter{ .mod = mod })) |decl_index| { @@ -6491,7 +6526,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref .tag = .save_err_return_trace_index, .data = .{ .ty_pl = .{ .ty = Air.internedToRef(stack_trace_ty.toIntern()), - .payload = @as(u32, @intCast(field_index)), + .payload = @intCast(field_index), } }, }); } @@ -6535,7 +6570,7 @@ fn popErrorReturnTrace( .tag = .block, .data = .{ .ty_pl = .{ - .ty = Air.Inst.Ref.void_type, + .ty = .void_type, .payload = undefined, // updated below }, }, @@ -6552,23 +6587,23 @@ fn popErrorReturnTrace( const field_name = try mod.intern_pool.getOrPutString(gpa, "index"); const field_ptr = try sema.structFieldPtr(&then_block, src, err_return_trace, field_name, src, stack_trace_ty, true); try sema.storePtr2(&then_block, src, field_ptr, src, saved_error_trace_index, src, .store); - _ = try then_block.addBr(cond_block_inst, Air.Inst.Ref.void_value); + _ = try then_block.addBr(cond_block_inst, .void_value); // Otherwise, do nothing var else_block = block.makeSubBlock(); defer else_block.instructions.deinit(gpa); - _ = try else_block.addBr(cond_block_inst, Air.Inst.Ref.void_value); + _ = try else_block.addBr(cond_block_inst, .void_value); try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.CondBr).Struct.fields.len + then_block.instructions.items.len + else_block.instructions.items.len + @typeInfo(Air.Block).Struct.fields.len + 1); // +1 for the sole .cond_br instruction in the .block - const cond_br_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const cond_br_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ .tag = .cond_br, .data = .{ .pl_op = .{ .operand = is_non_error_inst, .payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(then_block.instructions.items.len)), - .else_body_len = @as(u32, @intCast(else_block.instructions.items.len)), + .then_body_len = @intCast(then_block.instructions.items.len), + .else_body_len = @intCast(else_block.instructions.items.len), }), } } }); sema.air_extra.appendSliceAssumeCapacity(then_block.instructions.items); @@ -6599,7 +6634,7 @@ fn zirCall( const extra = sema.code.extraData(ExtraType, inst_data.payload_index); const args_len = extra.data.flags.args_len; - const modifier = @as(std.builtin.CallModifier, @enumFromInt(extra.data.flags.packed_modifier)); + const modifier: std.builtin.CallModifier = @enumFromInt(extra.data.flags.packed_modifier); const ensure_result_used = extra.data.flags.ensure_result_used; const pop_error_return_trace = extra.data.flags.pop_error_return_trace; @@ -6678,7 +6713,7 @@ fn zirCall( .tag = .save_err_return_trace_index, .data = .{ .ty_pl = .{ .ty = Air.internedToRef(stack_trace_ty.toIntern()), - .payload = @as(u32, @intCast(field_index)), + .payload = @intCast(field_index), } }, }); @@ -6725,7 +6760,7 @@ fn checkCallArgumentCount( try sema.errNote(block, func_src, msg, "consider using '.?', 'orelse' or 'if'", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } }, else => {}, @@ -6763,7 +6798,7 @@ fn checkCallArgumentCount( if (maybe_decl) |fn_decl| try mod.errNoteNonLazy(fn_decl.srcLoc(mod), msg, "function declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn callBuiltin( @@ -7097,7 +7132,7 @@ fn analyzeCall( if (maybe_decl) |fn_decl| try mod.errNoteNonLazy(fn_decl.srcLoc(mod), msg, "function declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const call_tag: Air.Inst.Tag = switch (modifier) { @@ -7156,7 +7191,7 @@ fn analyzeCall( } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (!is_inline_call and is_generic_call) { @@ -7194,10 +7229,10 @@ fn analyzeCall( } const result: Air.Inst.Ref = if (is_inline_call) res: { - const func_val = sema.resolveConstValue(block, func_src, func, "function being called at comptime must be comptime-known") catch |err| { - if (err == error.AnalysisFail and comptime_reason != null) try comptime_reason.?.explain(sema, sema.err); - return err; - }; + const func_val = try sema.resolveConstValue(block, func_src, func, .{ + .needed_comptime_reason = "function being called at comptime must be comptime-known", + .block_comptime_reason = comptime_reason, + }); const module_fn_index = switch (mod.intern_pool.indexToKey(func_val.toIntern())) { .extern_func => return sema.fail(block, call_src, "{s} call of extern function", .{ @as([]const u8, if (is_comptime_call) "comptime" else "inline"), @@ -7225,7 +7260,7 @@ fn analyzeCall( // set to in the `Block`. // This block instruction will be used to capture the return value from the // inlined function. - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ .tag = .block, .data = undefined, @@ -7233,7 +7268,10 @@ fn analyzeCall( // This one is shared among sub-blocks within the same callee, but not // shared among the entire inline/comptime call stack. var inlining: Block.Inlining = .{ - .func = .none, + .call_block = block, + .call_src = call_src, + .has_comptime_args = false, + .func = module_fn_index, .comptime_result = undefined, .merges = .{ .src_locs = .{}, @@ -7317,7 +7355,6 @@ fn analyzeCall( const fn_info = ics.callee().code.getFnInfo(module_fn.zir_body_inst); try ics.callee().inst_map.ensureSpaceForInstructions(gpa, fn_info.param_body); - var has_comptime_args = false; var arg_i: u32 = 0; for (fn_info.param_body) |inst| { const opt_noreturn_ref = try analyzeInlineCallArg( @@ -7333,7 +7370,6 @@ fn analyzeCall( memoized_arg_values, func_ty_info, func, - &has_comptime_args, ); if (opt_noreturn_ref) |ref| { // Analyzing this argument gave a ref of a noreturn type. Terminate argument analysis here. @@ -7345,19 +7381,19 @@ fn analyzeCall( // can just use `sema` directly. _ = ics.callee(); - if (!has_comptime_args and module_fn.analysis(ip).state == .sema_failure) - return error.AnalysisFail; + if (!inlining.has_comptime_args) { + if (module_fn.analysis(ip).state == .sema_failure) + return error.AnalysisFail; - const recursive_msg = "inline call is recursive"; - var head = if (!has_comptime_args) block else null; - while (head) |some| { - const parent_inlining = some.inlining orelse break; - if (parent_inlining.func == module_fn_index) { - return sema.fail(block, call_src, recursive_msg, .{}); + var block_it = block; + while (block_it.inlining) |parent_inlining| { + if (!parent_inlining.has_comptime_args and parent_inlining.func == module_fn_index) { + const err_msg = try sema.errMsg(block, call_src, "inline call is recursive", .{}); + return sema.failWithOwnedErrorMsg(null, err_msg); + } + block_it = parent_inlining.call_block; } - head = some.parent; } - if (!has_comptime_args) inlining.func = module_fn_index; // In case it is a generic function with an expression for the return type that depends // on parameters, we must now do the same for the return type as we just did with @@ -7427,12 +7463,6 @@ fn analyzeCall( const result = result: { sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) { error.ComptimeReturn => break :result inlining.comptime_result, - error.AnalysisFail => { - const err_msg = sema.err orelse return err; - if (mem.eql(u8, err_msg.msg, recursive_msg)) return err; - try sema.errNote(block, call_src, err_msg, "called from here", .{}); - return err; - }, else => |e| return e, }; break :result try sema.analyzeBlockBody(block, call_src, &child_block, merges); @@ -7451,7 +7481,7 @@ fn analyzeCall( } if (should_memoize and is_comptime_call) { - const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, ""); + const result_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, result, undefined); const result_interned = try result_val.intern2(sema.fn_ret_ty, mod); // Transform ad-hoc inferred error set types into concrete error sets. @@ -7523,7 +7553,7 @@ fn analyzeCall( .data = .{ .pl_op = .{ .operand = func, .payload = sema.addExtraAssumeCapacity(Air.Call{ - .args_len = @as(u32, @intCast(args.len)), + .args_len = @intCast(args.len), }), } }, }); @@ -7549,11 +7579,11 @@ fn analyzeCall( } } try sema.safetyPanic(block, call_src, .noreturn_returned); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } if (func_ty_info.return_type == .noreturn_type) { _ = try block.addNoOp(.unreach); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } break :res func_inst; }; @@ -7580,7 +7610,7 @@ fn handleTailCall(sema: *Sema, block: *Block, call_src: LazySrcLoc, func_ty: Typ }); } _ = try block.addUnOp(.ret, result); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } /// Usually, returns null. If an argument was noreturn, returns that ref (which should become the call result). @@ -7597,13 +7627,12 @@ fn analyzeInlineCallArg( memoized_arg_values: []InternPool.Index, func_ty_info: InternPool.Key.FuncType, func_inst: Air.Inst.Ref, - has_comptime_args: *bool, ) !?Air.Inst.Ref { const mod = ics.sema.mod; const ip = &mod.intern_pool; const zir_tags = ics.callee().code.instructions.items(.tag); switch (zir_tags[inst]) { - .param_comptime, .param_anytype_comptime => has_comptime_args.* = true, + .param_comptime, .param_anytype_comptime => param_block.inlining.?.has_comptime_args = true, else => {}, } switch (zir_tags[inst]) { @@ -7628,20 +7657,22 @@ fn analyzeInlineCallArg( } const arg_src = args_info.argSrc(arg_block, arg_i.*); if (try ics.callee().typeRequiresComptime(param_ty.toType())) { - _ = ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, "argument to parameter with comptime-only type must be comptime-known") catch |err| { - if (err == error.AnalysisFail and param_block.comptime_reason != null) try param_block.comptime_reason.?.explain(ics.caller(), ics.caller().err); - return err; - }; + _ = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, .{ + .needed_comptime_reason = "argument to parameter with comptime-only type must be comptime-known", + .block_comptime_reason = param_block.comptime_reason, + }); } else if (!is_comptime_call and zir_tags[inst] == .param_comptime) { - _ = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, "parameter is comptime"); + _ = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, .{ + .needed_comptime_reason = "parameter is comptime", + }); } if (is_comptime_call) { ics.callee().inst_map.putAssumeCapacityNoClobber(inst, casted_arg); - const arg_val = ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, "argument to function being called at comptime must be comptime-known") catch |err| { - if (err == error.AnalysisFail and param_block.comptime_reason != null) try param_block.comptime_reason.?.explain(ics.caller(), ics.caller().err); - return err; - }; + const arg_val = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, casted_arg, .{ + .needed_comptime_reason = "argument to function being called at comptime must be comptime-known", + .block_comptime_reason = param_block.comptime_reason, + }); switch (arg_val.toIntern()) { .generic_poison, .generic_poison_type => { // This function is currently evaluated as part of an as-of-yet unresolvable @@ -7661,7 +7692,7 @@ fn analyzeInlineCallArg( } if (try ics.caller().resolveMaybeUndefVal(casted_arg)) |_| { - has_comptime_args.* = true; + param_block.inlining.?.has_comptime_args = true; } arg_i.* += 1; @@ -7677,10 +7708,10 @@ fn analyzeInlineCallArg( if (is_comptime_call) { ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg); - const arg_val = ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, "argument to function being called at comptime must be comptime-known") catch |err| { - if (err == error.AnalysisFail and param_block.comptime_reason != null) try param_block.comptime_reason.?.explain(ics.caller(), ics.caller().err); - return err; - }; + const arg_val = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, .{ + .needed_comptime_reason = "argument to function being called at comptime must be comptime-known", + .block_comptime_reason = param_block.comptime_reason, + }); switch (arg_val.toIntern()) { .generic_poison, .generic_poison_type => { // This function is currently evaluated as part of an as-of-yet unresolvable @@ -7697,13 +7728,15 @@ fn analyzeInlineCallArg( memoized_arg_values[arg_i.*] = try resolved_arg_val.intern(ics.caller().typeOf(uncasted_arg), mod); } else { if (zir_tags[inst] == .param_anytype_comptime) { - _ = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, "parameter is comptime"); + _ = try ics.caller().resolveConstMaybeUndefVal(arg_block, arg_src, uncasted_arg, .{ + .needed_comptime_reason = "parameter is comptime", + }); } ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg); } if (try ics.caller().resolveMaybeUndefVal(uncasted_arg)) |_| { - has_comptime_args.* = true; + param_block.inlining.?.has_comptime_args = true; } arg_i.* += 1; @@ -7744,7 +7777,9 @@ fn instantiateGenericCall( const gpa = sema.gpa; const ip = &mod.intern_pool; - const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known"); + const func_val = try sema.resolveConstValue(block, func_src, func, .{ + .needed_comptime_reason = "generic function being called must be comptime-known", + }); const generic_owner = switch (mod.intern_pool.indexToKey(func_val.toIntern())) { .func => func_val.toIntern(), .ptr => |ptr| mod.declPtr(ptr.addr.decl).val.toIntern(), @@ -7891,7 +7926,7 @@ fn instantiateGenericCall( } else switch (param_tag) { .param_comptime, .param_anytype_comptime, - => return sema.failWithOwnedErrorMsg(msg: { + => return sema.failWithOwnedErrorMsg(block, msg: { const arg_src = args_info.argSrc(block, arg_index); const msg = try sema.errMsg(block, arg_src, "runtime-known argument passed to comptime parameter", .{}); errdefer msg.destroy(sema.gpa); @@ -7906,7 +7941,7 @@ fn instantiateGenericCall( .param, .param_anytype, - => return sema.failWithOwnedErrorMsg(msg: { + => return sema.failWithOwnedErrorMsg(block, msg: { const arg_src = args_info.argSrc(block, arg_index); const msg = try sema.errMsg(block, arg_src, "runtime-known argument passed to parameter of comptime-only type", .{}); errdefer msg.destroy(sema.gpa); @@ -8010,7 +8045,7 @@ fn instantiateGenericCall( } if (func_ty.fnReturnType(mod).isNoReturn(mod)) { _ = try block.addNoOp(.unreach); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } return result; } @@ -8146,7 +8181,9 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; - const len = @as(u32, @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, "vector length must be comptime-known"))); + const len: u32 = @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, .{ + .needed_comptime_reason = "vector length must be comptime-known", + })); const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs); try sema.checkVectorElemType(block, elem_type_src, elem_type); const vector_type = try mod.vectorType(.{ @@ -8164,7 +8201,9 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node }; const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node }; - const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, "array length must be comptime-known"); + const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, .{ + .needed_comptime_reason = "array length must be comptime-known", + }); const elem_type = try sema.resolveType(block, elem_src, extra.rhs); try sema.validateArrayElemType(block, elem_type, elem_src); const array_ty = try sema.mod.arrayType(.{ @@ -8184,12 +8223,16 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node }; const sentinel_src: LazySrcLoc = .{ .node_offset_array_type_sentinel = inst_data.src_node }; const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node }; - const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, "array length must be comptime-known"); + const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, .{ + .needed_comptime_reason = "array length must be comptime-known", + }); const elem_type = try sema.resolveType(block, elem_src, extra.elem_type); try sema.validateArrayElemType(block, elem_type, elem_src); const uncasted_sentinel = try sema.resolveInst(extra.sentinel); const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src); - const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel, "array sentinel value must be comptime-known"); + const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel, .{ + .needed_comptime_reason = "array sentinel value must be comptime-known", + }); const array_ty = try sema.mod.arrayType(.{ .len = len, .sentinel = sentinel_val.toIntern(), @@ -8347,7 +8390,7 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD return block.addInst(.{ .tag = .bitcast, .data = .{ .ty_op = .{ - .ty = Air.Inst.Ref.anyerror_type, + .ty = .anyerror_type, .operand = operand, } }, }); @@ -8373,7 +8416,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr try sema.errNote(block, src, msg, "'||' merges error sets; 'or' performs boolean OR", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs); const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs); @@ -8384,7 +8427,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr // Anything merged with anyerror is anyerror. if (lhs_ty.toIntern() == .anyerror_type or rhs_ty.toIntern() == .anyerror_type) { - return Air.Inst.Ref.anyerror_type; + return .anyerror_type; } if (ip.isInferredErrorSetType(lhs_ty.toIntern())) { @@ -8497,7 +8540,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError try sema.addDeclaredHereNote(msg, dest_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (int_val.isUndef(mod)) { return sema.failWithUseOfUndef(block, operand_src); @@ -8514,7 +8557,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError try sema.addDeclaredHereNote(msg, dest_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return Air.internedToRef((try mod.getCoerced(int_val, dest_ty)).toIntern()); } @@ -8891,7 +8934,7 @@ fn zirFunc( const ret_ty: Type = switch (extra.data.ret_body_len) { 0 => Type.void, 1 => blk: { - const ret_ty_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const ret_ty_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; if (sema.resolveType(block, ret_ty_src, ret_ty_ref)) |ret_ty| { break :blk ret_ty; @@ -8906,7 +8949,9 @@ fn zirFunc( const ret_ty_body = sema.code.extra[extra_index..][0..extra.data.ret_body_len]; extra_index += ret_ty_body.len; - const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, "return type must be comptime-known"); + const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{ + .needed_comptime_reason = "return type must be comptime-known", + }); break :blk ret_ty_val.toType(); }, }; @@ -8953,7 +8998,7 @@ fn resolveGenericBody( body: []const Zir.Inst.Index, func_inst: Zir.Inst.Index, dest_ty: Type, - reason: []const u8, + reason: NeededComptimeReason, ) !Value { assert(body.len != 0); @@ -9089,7 +9134,7 @@ fn checkCallConvSupportsVarArgs(sema: *Sema, block: *Block, src: LazySrcLoc, cc: try sema.errNote(block, src, msg, "supported calling conventions: {}", .{CallingConventionsSupportingVarArgsList{}}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -9185,7 +9230,7 @@ fn funcCommon( try sema.addDeclaredHereNote(msg, param_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (!this_generic and !target_util.fnCallConvAllowsZigTypes(target, cc_resolved) and !try sema.validateExternType(param_ty, .param_ty)) { const msg = msg: { @@ -9200,7 +9245,7 @@ fn funcCommon( try sema.addDeclaredHereNote(msg, param_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (is_source_decl and requires_comptime and !param_is_comptime and has_body) { const msg = msg: { @@ -9215,7 +9260,7 @@ fn funcCommon( try sema.addDeclaredHereNote(msg, param_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (is_source_decl and !this_generic and is_noalias and !(param_ty.zigTypeTag(mod) == .Pointer or param_ty.isPtrLikeOptional(mod))) @@ -9477,7 +9522,7 @@ fn finishFunc( try sema.addDeclaredHereNote(msg, return_type); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (!ret_poison and !target_util.fnCallConvAllowsZigTypes(target, cc_resolved) and !try sema.validateExternType(return_type, .ret_ty)) @@ -9494,7 +9539,7 @@ fn finishFunc( try sema.addDeclaredHereNote(msg, return_type); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // If the return type is comptime-only but not dependent on parameters then @@ -9534,41 +9579,41 @@ fn finishFunc( } } } - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const arch = target.cpu.arch; - if (switch (cc_resolved) { + if (@as(?[]const u8, switch (cc_resolved) { .Unspecified, .C, .Naked, .Async, .Inline => null, .Interrupt => switch (arch) { .x86, .x86_64, .avr, .msp430 => null, - else => @as([]const u8, "x86, x86_64, AVR, and MSP430"), + else => "x86, x86_64, AVR, and MSP430", }, .Signal => switch (arch) { .avr => null, - else => @as([]const u8, "AVR"), + else => "AVR", }, .Stdcall, .Fastcall, .Thiscall => switch (arch) { .x86 => null, - else => @as([]const u8, "x86"), + else => "x86", }, .Vectorcall => switch (arch) { .x86, .aarch64, .aarch64_be, .aarch64_32 => null, - else => @as([]const u8, "x86 and AArch64"), + else => "x86 and AArch64", }, .APCS, .AAPCS, .AAPCSVFP => switch (arch) { .arm, .armeb, .aarch64, .aarch64_be, .aarch64_32, .thumb, .thumbeb => null, - else => @as([]const u8, "ARM"), + else => "ARM", }, .SysV, .Win64 => switch (arch) { .x86_64 => null, - else => @as([]const u8, "x86_64"), + else => "x86_64", }, .Kernel => switch (arch) { .nvptx, .nvptx64, .amdgcn, .spirv32, .spirv64 => null, - else => @as([]const u8, "nvptx, amdgcn and SPIR-V"), + else => "nvptx, amdgcn and SPIR-V", }, - }) |allowed_platform| { + })) |allowed_platform| { return sema.fail(block, cc_src, "callconv '{s}' is only available on {s}, not {s}", .{ @tagName(cc_resolved), allowed_platform, @@ -9852,7 +9897,9 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data; const object = try sema.resolveInst(extra.lhs); - const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, "field name must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ + .needed_comptime_reason = "field name must be comptime-known", + }); return sema.fieldVal(block, src, object, field_name, field_name_src); } @@ -9865,7 +9912,9 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data; const object_ptr = try sema.resolveInst(extra.lhs); - const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, "field name must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ + .needed_comptime_reason = "field name must be comptime-known", + }); return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, false); } @@ -10071,7 +10120,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Pointer => { @@ -10086,7 +10135,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Struct, .Union => if (dest_ty.containerLayout(mod) == .Auto) { const container = switch (dest_ty.zigTypeTag(mod)) { @@ -10135,7 +10184,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Pointer => { const msg = msg: { @@ -10149,7 +10198,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Struct, .Union => if (operand_ty.containerLayout(mod) == .Auto) { const container = switch (operand_ty.zigTypeTag(mod)) { @@ -10300,7 +10349,7 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return sema.elemPtrOneLayerOnly(block, src, array_ptr, elem_index, src, false, false); } @@ -10472,7 +10521,7 @@ const SwitchProngAnalysis = struct { if (sema.typeOf(capture_ref).isNoReturn(sema.mod)) { // This prong should be unreachable! - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } sema.inst_map.putAssumeCapacity(spa.switch_block_inst, capture_ref); @@ -10566,7 +10615,7 @@ const SwitchProngAnalysis = struct { try sema.addDeclaredHereNote(msg, operand_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } assert(inline_case_capture != .none); return inline_case_capture; @@ -10593,7 +10642,7 @@ const SwitchProngAnalysis = struct { const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = switch_node_offset }; if (inline_case_capture != .none) { - const item_val = sema.resolveConstValue(block, .unneeded, inline_case_capture, "") catch unreachable; + const item_val = sema.resolveConstValue(block, .unneeded, inline_case_capture, undefined) catch unreachable; if (operand_ty.zigTypeTag(mod) == .Union) { const field_index: u32 = @intCast(operand_ty.unionTagFieldIndex(item_val, mod).?); const union_obj = mod.typeToUnion(operand_ty).?; @@ -10641,7 +10690,7 @@ const SwitchProngAnalysis = struct { return sema.bitCast(block, ty, spa.operand, operand_src, null); } else { try block.addUnreachable(operand_src, false); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; }, else => return spa.operand, } @@ -10650,14 +10699,14 @@ const SwitchProngAnalysis = struct { switch (operand_ty.zigTypeTag(mod)) { .Union => { const union_obj = mod.typeToUnion(operand_ty).?; - const first_item_val = sema.resolveConstValue(block, .unneeded, case_vals[0], "") catch unreachable; + const first_item_val = sema.resolveConstValue(block, .unneeded, case_vals[0], undefined) catch unreachable; const first_field_index: u32 = mod.unionTagFieldIndex(union_obj, first_item_val).?; const first_field_ty = union_obj.field_types.get(ip)[first_field_index].toType(); const field_tys = try sema.arena.alloc(Type, case_vals.len); for (case_vals, field_tys) |item, *field_ty| { - const item_val = sema.resolveConstValue(block, .unneeded, item, "") catch unreachable; + const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable; const field_idx = mod.unionTagFieldIndex(union_obj, item_val).?; field_ty.* = union_obj.field_types.get(ip)[field_idx].toType(); } @@ -10684,7 +10733,7 @@ const SwitchProngAnalysis = struct { const multi_idx = raw_capture_src.multi_capture; const src_decl_ptr = sema.mod.declPtr(block.src_decl); for (case_srcs, 0..) |*case_src, i| { - const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @as(u32, @intCast(i)) } }; + const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @intCast(i) } }; case_src.* = raw_case_src.resolve(mod, src_decl_ptr, switch_node_offset, .none); } const capture_src = raw_capture_src.resolve(mod, src_decl_ptr, switch_node_offset, .none); @@ -10732,7 +10781,7 @@ const SwitchProngAnalysis = struct { const multi_idx = raw_capture_src.multi_capture; const src_decl_ptr = sema.mod.declPtr(block.src_decl); const capture_src = raw_capture_src.resolve(mod, src_decl_ptr, switch_node_offset, .none); - const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @as(u32, @intCast(i)) } }; + const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @intCast(i) } }; const case_src = raw_case_src.resolve(mod, src_decl_ptr, switch_node_offset, .none); const msg = msg: { const msg = try sema.errMsg(block, capture_src, "capture group with incompatible types", .{}); @@ -10744,7 +10793,7 @@ const SwitchProngAnalysis = struct { try sema.errNote(block, capture_src, msg, "this coercion is only possible when capturing by value", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } } @@ -10833,12 +10882,12 @@ const SwitchProngAnalysis = struct { var coerce_block = block.makeSubBlock(); defer coerce_block.instructions.deinit(sema.gpa); - const uncoerced = try coerce_block.addStructFieldVal(spa.operand, @as(u32, @intCast(idx)), field_tys[idx]); + const uncoerced = try coerce_block.addStructFieldVal(spa.operand, @intCast(idx), field_tys[idx]); const coerced = sema.coerce(&coerce_block, capture_ty, uncoerced, .unneeded) catch |err| switch (err) { error.NeededSourceLocation => { const multi_idx = raw_capture_src.multi_capture; const src_decl_ptr = sema.mod.declPtr(block.src_decl); - const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @as(u32, @intCast(idx)) } }; + const raw_case_src: Module.SwitchProngSrc = .{ .multi = .{ .prong = multi_idx, .item = @intCast(idx) } }; const case_src = raw_case_src.resolve(mod, src_decl_ptr, switch_node_offset, .none); _ = try sema.coerce(&coerce_block, capture_ty, uncoerced, case_src); unreachable; @@ -10849,7 +10898,7 @@ const SwitchProngAnalysis = struct { try cases_extra.ensureUnusedCapacity(3 + coerce_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(coerce_block.instructions.items.len))); // body_len + cases_extra.appendAssumeCapacity(@intCast(coerce_block.instructions.items.len)); // body_len cases_extra.appendAssumeCapacity(@intFromEnum(case_vals[idx])); // item cases_extra.appendSliceAssumeCapacity(coerce_block.instructions.items); // body } @@ -10860,7 +10909,7 @@ const SwitchProngAnalysis = struct { defer coerce_block.instructions.deinit(sema.gpa); const first_imc = in_mem_coercible.findFirstSet().?; - const uncoerced = try coerce_block.addStructFieldVal(spa.operand, @as(u32, @intCast(first_imc)), field_tys[first_imc]); + const uncoerced = try coerce_block.addStructFieldVal(spa.operand, @intCast(first_imc), field_tys[first_imc]); const coerced = try coerce_block.addBitCast(capture_ty, uncoerced); _ = try coerce_block.addBr(capture_block_inst, coerced); @@ -10873,14 +10922,14 @@ const SwitchProngAnalysis = struct { @typeInfo(Air.Block).Struct.fields.len + 1); - const switch_br_inst = @as(u32, @intCast(sema.air_instructions.len)); + const switch_br_inst: u32 = @intCast(sema.air_instructions.len); try sema.air_instructions.append(sema.gpa, .{ .tag = .switch_br, .data = .{ .pl_op = .{ .operand = spa.cond, .payload = sema.addExtraAssumeCapacity(Air.SwitchBr{ - .cases_len = @as(u32, @intCast(prong_count)), - .else_body_len = @as(u32, @intCast(else_body_len)), + .cases_len = @intCast(prong_count), + .else_body_len = @intCast(else_body_len), }), } }, }); @@ -10906,7 +10955,7 @@ const SwitchProngAnalysis = struct { } if (case_vals.len == 1) { - const item_val = sema.resolveConstValue(block, .unneeded, case_vals[0], "") catch unreachable; + const item_val = sema.resolveConstValue(block, .unneeded, case_vals[0], undefined) catch unreachable; const item_ty = try mod.singleErrorSetType(item_val.getErrorName(mod).unwrap().?); return sema.bitCast(block, item_ty, spa.operand, operand_src, null); } @@ -10914,7 +10963,7 @@ const SwitchProngAnalysis = struct { var names: InferredErrorSet.NameMap = .{}; try names.ensureUnusedCapacity(sema.arena, case_vals.len); for (case_vals) |err| { - const err_val = sema.resolveConstValue(block, .unneeded, err, "") catch unreachable; + const err_val = sema.resolveConstValue(block, .unneeded, err, undefined) catch unreachable; names.putAssumeCapacityNoClobber(err_val.getErrorName(mod).unwrap().?, {}); } const error_ty = try mod.errorSetFromUnsortedNames(names.keys()); @@ -10975,7 +11024,7 @@ fn switchCond( } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; return sema.unionToTag(block, enum_ty, operand, src); }, @@ -11067,7 +11116,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .has_tag_capture = false, }, .under, .@"else" => blk: { - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[header_extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[header_extra_index]); const extra_body_start = header_extra_index + 1; break :blk .{ .body = sema.code.extra[extra_body_start..][0..info.body_len], @@ -11128,7 +11177,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r ); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // Validate for duplicate items, missing else prong, and invalid range. @@ -11144,9 +11193,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r { var scalar_i: u32 = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { - const item_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + info.body_len; case_vals.appendAssumeCapacity(try sema.validateSwitchItemEnum( @@ -11167,7 +11216,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const items = sema.code.refSlice(extra_index, items_len); extra_index += items_len + info.body_len; @@ -11181,7 +11230,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r item_ref, operand_ty, src_node_offset, - .{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }, + .{ .multi = .{ .prong = multi_i, .item = @intCast(item_i) } }, )); } @@ -11228,7 +11277,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r ); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } else if (special_prong == .none and operand_ty.isNonexhaustiveEnum(mod) and !union_originally) { return sema.fail( block, @@ -11243,9 +11292,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r { var scalar_i: u32 = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { - const item_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + info.body_len; case_vals.appendAssumeCapacity(try sema.validateSwitchItemError( @@ -11265,7 +11314,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const items = sema.code.refSlice(extra_index, items_len); extra_index += items_len + info.body_len; @@ -11278,7 +11327,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r item_ref, operand_ty, src_node_offset, - .{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }, + .{ .multi = .{ .prong = multi_i, .item = @intCast(item_i) } }, )); } @@ -11328,7 +11377,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r if (maybe_msg) |msg| { maybe_msg = null; try sema.addDeclaredHereNote(msg, operand_ty); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (special_prong == .@"else" and @@ -11387,9 +11436,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r { var scalar_i: u32 = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { - const item_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + info.body_len; case_vals.appendAssumeCapacity(try sema.validateSwitchItemInt( @@ -11409,7 +11458,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const items = sema.code.refSlice(extra_index, items_len); extra_index += items_len; @@ -11422,16 +11471,16 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r item_ref, operand_ty, src_node_offset, - .{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }, + .{ .multi = .{ .prong = multi_i, .item = @intCast(item_i) } }, )); } try case_vals.ensureUnusedCapacity(gpa, 2 * ranges_len); var range_i: u32 = 0; while (range_i < ranges_len) : (range_i += 1) { - const item_first = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_first: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const item_last = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_last: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; const vals = try sema.validateSwitchRange( @@ -11482,9 +11531,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r { var scalar_i: u32 = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { - const item_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + info.body_len; case_vals.appendAssumeCapacity(try sema.validateSwitchItemBool( @@ -11504,7 +11553,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const items = sema.code.refSlice(extra_index, items_len); extra_index += items_len + info.body_len; @@ -11517,7 +11566,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r &false_count, item_ref, src_node_offset, - .{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }, + .{ .multi = .{ .prong = multi_i, .item = @intCast(item_i) } }, )); } @@ -11564,9 +11613,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r { var scalar_i: u32 = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { - const item_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const item_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; extra_index += info.body_len; @@ -11587,7 +11636,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const items = sema.code.refSlice(extra_index, items_len); extra_index += items_len + info.body_len; @@ -11600,7 +11649,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r item_ref, operand_ty, src_node_offset, - .{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }, + .{ .multi = .{ .prong = multi_i, .item = @intCast(item_i) } }, )); } @@ -11638,7 +11687,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .tag_capture_inst = tag_capture_inst, }; - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ .tag = .block, .data = undefined, @@ -11682,13 +11731,13 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r var scalar_i: usize = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const body = sema.code.extra[extra_index..][0..info.body_len]; extra_index += info.body_len; const item = case_vals.items[scalar_i]; - const item_val = sema.resolveConstValue(&child_block, .unneeded, item, "") catch unreachable; + const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable; if (operand_val.eql(item_val, operand_ty, sema.mod)) { if (err_set) try sema.maybeErrorUnwrapComptime(&child_block, body, operand); return spa.resolveProngComptime( @@ -11696,7 +11745,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .normal, body, info.capture, - .{ .scalar_capture = @as(u32, @intCast(scalar_i)) }, + .{ .scalar_capture = @intCast(scalar_i) }, &.{item}, if (info.is_inline) operand else .none, info.has_tag_capture, @@ -11713,7 +11762,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + items_len; const body = sema.code.extra[extra_index + 2 * ranges_len ..][0..info.body_len]; @@ -11722,7 +11771,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r for (items) |item| { // Validation above ensured these will succeed. - const item_val = sema.resolveConstValue(&child_block, .unneeded, item, "") catch unreachable; + const item_val = sema.resolveConstValue(&child_block, .unneeded, item, undefined) catch unreachable; if (operand_val.eql(item_val, operand_ty, sema.mod)) { if (err_set) try sema.maybeErrorUnwrapComptime(&child_block, body, operand); return spa.resolveProngComptime( @@ -11730,7 +11779,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .normal, body, info.capture, - .{ .multi_capture = @as(u32, @intCast(multi_i)) }, + .{ .multi_capture = @intCast(multi_i) }, items, if (info.is_inline) operand else .none, info.has_tag_capture, @@ -11746,8 +11795,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r case_val_idx += 2; // Validation above ensured these will succeed. - const first_val = sema.resolveConstValue(&child_block, .unneeded, range_items[0], "") catch unreachable; - const last_val = sema.resolveConstValue(&child_block, .unneeded, range_items[1], "") catch unreachable; + const first_val = sema.resolveConstValue(&child_block, .unneeded, range_items[0], undefined) catch unreachable; + const last_val = sema.resolveConstValue(&child_block, .unneeded, range_items[1], undefined) catch unreachable; if ((try sema.compareAll(resolved_operand_val, .gte, first_val, operand_ty)) and (try sema.compareAll(resolved_operand_val, .lte, last_val, operand_ty))) { @@ -11757,7 +11806,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .normal, body, info.capture, - .{ .multi_capture = @as(u32, @intCast(multi_i)) }, + .{ .multi_capture = @intCast(multi_i) }, undefined, // case_vals may be undefined for ranges if (info.is_inline) operand else .none, info.has_tag_capture, @@ -11771,7 +11820,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r } if (err_set) try sema.maybeErrorUnwrapComptime(&child_block, special.body, operand); if (empty_enum) { - return Air.Inst.Ref.void_value; + return .void_value; } return spa.resolveProngComptime( @@ -11789,13 +11838,13 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r if (scalar_cases_len + multi_cases_len == 0 and !special.is_inline) { if (empty_enum) { - return Air.Inst.Ref.void_value; + return .void_value; } if (special_prong == .none) { return sema.fail(block, src, "switch must handle all possibilities", .{}); } if (err_set and try sema.maybeErrorUnwrap(block, special.body, operand, operand_src)) { - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } if (mod.backendSupportsFeature(.is_named_enum_value) and block.wantSafety() and operand_ty.zigTypeTag(mod) == .Enum and (!operand_ty.isNonexhaustiveEnum(mod) or union_originally)) @@ -11819,10 +11868,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r } if (child_block.is_comptime) { - _ = sema.resolveConstValue(&child_block, operand_src, operand, "condition in comptime switch must be comptime-known") catch |err| { - if (err == error.AnalysisFail and child_block.comptime_reason != null) try child_block.comptime_reason.?.explain(sema, sema.err); - return err; - }; + _ = try sema.resolveConstValue(&child_block, operand_src, operand, .{ + .needed_comptime_reason = "condition in comptime switch must be comptime-known", + .block_comptime_reason = child_block.comptime_reason, + }); unreachable; } @@ -11842,7 +11891,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r var scalar_i: usize = 0; while (scalar_i < scalar_cases_len) : (scalar_i += 1) { extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1; const body = sema.code.extra[extra_index..][0..info.body_len]; extra_index += info.body_len; @@ -11857,7 +11906,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r // `item` is already guaranteed to be constant known. const analyze_body = if (union_originally) blk: { - const item_val = sema.resolveConstLazyValue(block, .unneeded, item, "") catch unreachable; + const item_val = sema.resolveConstLazyValue(block, .unneeded, item, undefined) catch unreachable; const field_ty = maybe_union_ty.unionFieldType(item_val, mod); break :blk field_ty.zigTypeTag(mod) != .NoReturn; } else true; @@ -11870,7 +11919,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r .normal, body, info.capture, - .{ .scalar_capture = @as(u32, @intCast(scalar_i)) }, + .{ .scalar_capture = @intCast(scalar_i) }, &.{item}, if (info.is_inline) item else .none, info.has_tag_capture, @@ -11883,7 +11932,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -11903,7 +11952,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r extra_index += 1; const ranges_len = sema.code.extra[extra_index]; extra_index += 1; - const info = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(sema.code.extra[extra_index])); + const info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(sema.code.extra[extra_index]); extra_index += 1 + items_len; const items = case_vals.items[case_val_idx..][0..items_len]; @@ -11946,7 +11995,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r if (emit_bb) sema.emitBackwardBranch(block, .unneeded) catch |err| switch (err) { error.NeededSourceLocation => { - const case_src = Module.SwitchProngSrc{ .range = .{ .prong = multi_i, .item = range_i } }; + const case_src = Module.SwitchProngSrc{ + .range = .{ .prong = multi_i, .item = range_i }, + }; const decl = mod.declPtr(case_block.src_decl); try sema.emitBackwardBranch(block, case_src.resolve(mod, decl, src_node_offset, .none)); unreachable; @@ -11968,7 +12019,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); @@ -11990,7 +12041,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r if (emit_bb) sema.emitBackwardBranch(block, .unneeded) catch |err| switch (err) { error.NeededSourceLocation => { - const case_src = Module.SwitchProngSrc{ .multi = .{ .prong = multi_i, .item = @as(u32, @intCast(item_i)) } }; + const case_src = Module.SwitchProngSrc{ + .multi = .{ .prong = multi_i, .item = @intCast(item_i) }, + }; const decl = mod.declPtr(case_block.src_decl); try sema.emitBackwardBranch(block, case_src.resolve(mod, decl, src_node_offset, .none)); unreachable; @@ -12016,7 +12069,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12035,7 +12088,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r const analyze_body = if (union_originally) for (items) |item| { - const item_val = sema.resolveConstValue(block, .unneeded, item, "") catch unreachable; + const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable; const field_ty = maybe_union_ty.unionFieldType(item_val, mod); if (field_ty.zigTypeTag(mod) != .NoReturn) break true; } else false @@ -12064,8 +12117,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 2 + items.len + case_block.instructions.items.len); - cases_extra.appendAssumeCapacity(@as(u32, @intCast(items.len))); - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(items.len)); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); for (items) |item| { cases_extra.appendAssumeCapacity(@intFromEnum(item)); @@ -12160,8 +12213,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r sema.air_instructions.items(.data)[prev_cond_br].pl_op.payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(prev_then_body.len)), - .else_body_len = @as(u32, @intCast(cond_body.len)), + .then_body_len = @intCast(prev_then_body.len), + .else_body_len = @intCast(cond_body.len), }); sema.air_extra.appendSliceAssumeCapacity(prev_then_body); sema.air_extra.appendSliceAssumeCapacity(cond_body); @@ -12186,7 +12239,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r if (f != null) continue; cases_len += 1; - const item_val = try mod.enumValueFieldIndex(operand_ty, @as(u32, @intCast(i))); + const item_val = try mod.enumValueFieldIndex(operand_ty, @intCast(i)); const item_ref = Air.internedToRef(item_val.toIntern()); case_block.instructions.shrinkRetainingCapacity(0); @@ -12217,7 +12270,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12258,7 +12311,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12289,7 +12342,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12310,14 +12363,14 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r special.body, special.capture, .special_capture, - &.{Air.Inst.Ref.bool_true}, - Air.Inst.Ref.bool_true, + &.{.bool_true}, + .bool_true, special.has_tag_capture, ); try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(Air.Inst.Ref.bool_true)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12336,14 +12389,14 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r special.body, special.capture, .special_capture, - &.{Air.Inst.Ref.bool_false}, - Air.Inst.Ref.bool_false, + &.{.bool_false}, + .bool_false, special.has_tag_capture, ); try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); cases_extra.appendAssumeCapacity(1); // items_len - cases_extra.appendAssumeCapacity(@as(u32, @intCast(case_block.instructions.items.len))); + cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); cases_extra.appendAssumeCapacity(@intFromEnum(Air.Inst.Ref.bool_false)); cases_extra.appendSliceAssumeCapacity(case_block.instructions.items); } @@ -12412,8 +12465,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r sema.air_instructions.items(.data)[prev_cond_br].pl_op.payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(prev_then_body.len)), - .else_body_len = @as(u32, @intCast(case_block.instructions.items.len)), + .then_body_len = @intCast(prev_then_body.len), + .else_body_len = @intCast(case_block.instructions.items.len), }); sema.air_extra.appendSliceAssumeCapacity(prev_then_body); sema.air_extra.appendSliceAssumeCapacity(case_block.instructions.items); @@ -12427,8 +12480,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r _ = try child_block.addInst(.{ .tag = .switch_br, .data = .{ .pl_op = .{ .operand = operand, .payload = sema.addExtraAssumeCapacity(Air.SwitchBr{ - .cases_len = @as(u32, @intCast(cases_len)), - .else_body_len = @as(u32, @intCast(final_else_body.len)), + .cases_len = @intCast(cases_len), + .else_body_len = @intCast(final_else_body.len), }), } } }); sema.air_extra.appendSliceAssumeCapacity(cases_extra.items); @@ -12535,10 +12588,12 @@ fn resolveSwitchItemVal( else => |e| return e, }; - const maybe_lazy = sema.resolveConstValue(block, .unneeded, item, "") catch |err| switch (err) { + const maybe_lazy = sema.resolveConstValue(block, .unneeded, item, undefined) catch |err| switch (err) { error.NeededSourceLocation => { const src = switch_prong_src.resolve(mod, mod.declPtr(block.src_decl), switch_node_offset, range_expand); - _ = try sema.resolveConstValue(block, src, item, "switch prong values must be comptime-known"); + _ = try sema.resolveConstValue(block, src, item, .{ + .needed_comptime_reason = "switch prong values must be comptime-known", + }); unreachable; }, else => |e| return e, @@ -12662,7 +12717,7 @@ fn validateSwitchDupe( ); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn validateSwitchItemBool( @@ -12736,7 +12791,7 @@ fn validateSwitchNoRange( ); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn maybeErrorUnwrap(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, operand: Air.Inst.Ref, operand_src: LazySrcLoc) !bool { @@ -12856,7 +12911,9 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const ty = try sema.resolveType(block, ty_src, extra.lhs); - const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, "field name must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{ + .needed_comptime_reason = "field name must be comptime-known", + }); try sema.resolveTypeFields(ty); const ip = &mod.intern_pool; @@ -12897,11 +12954,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai ty.fmt(mod), }); }; - if (has_field) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (has_field) .bool_true else .bool_false; } fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { @@ -12912,19 +12965,21 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const container_type = try sema.resolveType(block, lhs_src, extra.lhs); - const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, "decl name must be comptime-known"); + const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ + .needed_comptime_reason = "decl name must be comptime-known", + }); try sema.checkNamespaceType(block, lhs_src, container_type); const namespace = container_type.getNamespaceIndex(mod).unwrap() orelse - return Air.Inst.Ref.bool_false; + return .bool_false; if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl_index| { const decl = mod.declPtr(decl_index); if (decl.is_pub or decl.getFileScope(mod) == block.getFileScope(mod)) { - return Air.Inst.Ref.bool_true; + return .bool_true; } } - return Air.Inst.Ref.bool_false; + return .bool_false; } fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { @@ -12965,7 +13020,9 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A const mod = sema.mod; const inst_data = sema.code.instructions.items(.data)[inst].un_node; const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; - const name = try sema.resolveConstString(block, operand_src, inst_data.operand, "file path name must be comptime-known"); + const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ + .needed_comptime_reason = "file path name must be comptime-known", + }); if (name.len == 0) { return sema.fail(block, operand_src, "file path name cannot be empty", .{}); @@ -13588,8 +13645,12 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const rhs_sent = Air.internedToRef(rhs_sent_val.toIntern()); const lhs_sent_casted = try sema.coerce(block, resolved_elem_ty, lhs_sent, lhs_src); const rhs_sent_casted = try sema.coerce(block, resolved_elem_ty, rhs_sent, rhs_src); - const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, "array sentinel value must be comptime-known"); - const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, "array sentinel value must be comptime-known"); + const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, .{ + .needed_comptime_reason = "array sentinel value must be comptime-known", + }); + const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, .{ + .needed_comptime_reason = "array sentinel value must be comptime-known", + }); if (try sema.valuesEqual(lhs_sent_casted_val, rhs_sent_casted_val, resolved_elem_ty)) { break :s lhs_sent_casted_val; } else { @@ -13597,14 +13658,18 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } } else { const lhs_sent_casted = try sema.coerce(block, resolved_elem_ty, lhs_sent, lhs_src); - const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, "array sentinel value must be comptime-known"); + const lhs_sent_casted_val = try sema.resolveConstValue(block, lhs_src, lhs_sent_casted, .{ + .needed_comptime_reason = "array sentinel value must be comptime-known", + }); break :s lhs_sent_casted_val; } } else { if (rhs_info.sentinel) |rhs_sent_val| { const rhs_sent = Air.internedToRef(rhs_sent_val.toIntern()); const rhs_sent_casted = try sema.coerce(block, resolved_elem_ty, rhs_sent, rhs_src); - const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, "array sentinel value must be comptime-known"); + const rhs_sent_casted_val = try sema.resolveConstValue(block, rhs_src, rhs_sent_casted, .{ + .needed_comptime_reason = "array sentinel value must be comptime-known", + }); break :s rhs_sent_casted_val; } else { break :s null; @@ -13662,7 +13727,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try lhs_sub_val.elemValue(mod, lhs_elem_i) else elem_default_val; const elem_val_inst = Air.internedToRef(elem_val.toIntern()); const coerced_elem_val_inst = try sema.coerce(block, resolved_elem_ty, elem_val_inst, .unneeded); - const coerced_elem_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, coerced_elem_val_inst, ""); + const coerced_elem_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, coerced_elem_val_inst, undefined); element_vals[elem_i] = try coerced_elem_val.intern(resolved_elem_ty, mod); } while (elem_i < result_len) : (elem_i += 1) { @@ -13671,7 +13736,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try rhs_sub_val.elemValue(mod, rhs_elem_i) else elem_default_val; const elem_val_inst = Air.internedToRef(elem_val.toIntern()); const coerced_elem_val_inst = try sema.coerce(block, resolved_elem_ty, elem_val_inst, .unneeded); - const coerced_elem_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, coerced_elem_val_inst, ""); + const coerced_elem_val = try sema.resolveConstMaybeUndefVal(block, .unneeded, coerced_elem_val_inst, undefined); element_vals[elem_i] = try coerced_elem_val.intern(resolved_elem_ty, mod); } return sema.addConstantMaybeRef(block, result_ty, (try mod.intern(.{ .aggregate = .{ @@ -13748,7 +13813,9 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins // has a sentinel, and this code should compute the length based // on the sentinel value. .Slice, .Many => { - const val = try sema.resolveConstValue(block, src, operand, "slice value being concatenated must be comptime-known"); + const val = try sema.resolveConstValue(block, src, operand, .{ + .needed_comptime_reason = "slice value being concatenated must be comptime-known", + }); return Type.ArrayInfo{ .elem_type = ptr_info.child.toType(), .sentinel = switch (ptr_info.sentinel) { @@ -13842,7 +13909,7 @@ fn analyzeTupleMul( var i: u32 = 0; while (i < tuple_len) : (i += 1) { const operand_src = lhs_src; // TODO better source location - element_refs[i] = try sema.tupleFieldValByIndex(block, operand_src, operand, @as(u32, @intCast(i)), operand_ty); + element_refs[i] = try sema.tupleFieldValByIndex(block, operand_src, operand, @intCast(i), operand_ty); } i = 1; while (i < factor) : (i += 1) { @@ -13868,7 +13935,9 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai if (lhs_ty.isTuple(mod)) { // In `**` rhs must be comptime-known, but lhs can be runtime-known - const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, "array multiplication factor must be comptime-known"); + const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ + .needed_comptime_reason = "array multiplication factor must be comptime-known", + }); const factor_casted = try sema.usizeCast(block, rhs_src, factor); return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor_casted); } @@ -13886,11 +13955,13 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; // In `**` rhs must be comptime-known, but lhs can be runtime-known - const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, "array multiplication factor must be comptime-known"); + const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ + .needed_comptime_reason = "array multiplication factor must be comptime-known", + }); const result_len_u64 = std.math.mul(u64, lhs_info.len, factor) catch return sema.fail(block, rhs_src, "operation results in overflow", .{}); @@ -15886,10 +15957,10 @@ fn analyzePtrArithmetic( // The resulting pointer is aligned to the lcd between the offset (an // arbitrary number) and the alignment factor (always a power of two, // non zero). - const new_align = @as(Alignment, @enumFromInt(@min( + const new_align: Alignment = @enumFromInt(@min( @ctz(addend), @intFromEnum(ptr_info.flags.alignment), - ))); + )); assert(new_align != .none); break :t try mod.ptrType(.{ @@ -15968,15 +16039,17 @@ fn zirAsm( const extra = sema.code.extraData(Zir.Inst.Asm, extended.operand); const src = LazySrcLoc.nodeOffset(extra.data.src_node); const ret_ty_src: LazySrcLoc = .{ .node_offset_asm_ret_ty = extra.data.src_node }; - const outputs_len = @as(u5, @truncate(extended.small)); - const inputs_len = @as(u5, @truncate(extended.small >> 5)); - const clobbers_len = @as(u5, @truncate(extended.small >> 10)); + const outputs_len: u5 = @truncate(extended.small); + const inputs_len: u5 = @truncate(extended.small >> 5); + const clobbers_len: u5 = @truncate(extended.small >> 10); const is_volatile = @as(u1, @truncate(extended.small >> 15)) != 0; const is_global_assembly = sema.func_index == .none; const asm_source: []const u8 = if (tmpl_is_expr) blk: { - const tmpl = @as(Zir.Inst.Ref, @enumFromInt(extra.data.asm_source)); - const s: []const u8 = try sema.resolveConstString(block, src, tmpl, "assembly code must be comptime-known"); + const tmpl: Zir.Inst.Ref = @enumFromInt(extra.data.asm_source); + const s: []const u8 = try sema.resolveConstString(block, src, tmpl, .{ + .needed_comptime_reason = "assembly code must be comptime-known", + }); break :blk s; } else sema.code.nullTerminatedString(extra.data.asm_source); @@ -15994,7 +16067,7 @@ fn zirAsm( return sema.fail(block, src, "volatile keyword is redundant on module-level assembly", .{}); } try sema.mod.addGlobalAssembly(sema.owner_decl_index, asm_source); - return Air.Inst.Ref.void_value; + return .void_value; } if (block.is_comptime) { @@ -16076,9 +16149,9 @@ fn zirAsm( .data = .{ .ty_pl = .{ .ty = expr_ty, .payload = sema.addExtraAssumeCapacity(Air.Asm{ - .source_len = @as(u32, @intCast(asm_source.len)), + .source_len = @intCast(asm_source.len), .outputs_len = outputs_len, - .inputs_len = @as(u32, @intCast(args.len)), + .inputs_len = @intCast(args.len), .flags = (@as(u32, @intFromBool(is_volatile)) << 31) | @as(u32, @intCast(clobbers.len)), }), } }, @@ -16141,11 +16214,7 @@ fn zirCmpEq( const rhs_ty_tag = rhs_ty.zigTypeTag(mod); if (lhs_ty_tag == .Null and rhs_ty_tag == .Null) { // null == null, null != null - if (op == .eq) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (op == .eq) .bool_true else .bool_false; } // comparing null with optionals @@ -16177,11 +16246,10 @@ fn zirCmpEq( } const lkey = mod.intern_pool.indexToKey(lval.toIntern()); const rkey = mod.intern_pool.indexToKey(rval.toIntern()); - if ((lkey.err.name == rkey.err.name) == (op == .eq)) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if ((lkey.err.name == rkey.err.name) == (op == .eq)) + .bool_true + else + .bool_false; } else { break :src rhs_src; } @@ -16195,11 +16263,7 @@ fn zirCmpEq( if (lhs_ty_tag == .Type and rhs_ty_tag == .Type) { const lhs_as_type = try sema.analyzeAsType(block, lhs_src, lhs); const rhs_as_type = try sema.analyzeAsType(block, rhs_src, rhs); - if (lhs_as_type.eql(rhs_as_type, mod) == (op == .eq)) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (lhs_as_type.eql(rhs_as_type, mod) == (op == .eq)) .bool_true else .bool_false; } return sema.analyzeCmp(block, src, lhs, rhs, op, lhs_src, rhs_src, true); } @@ -16224,7 +16288,7 @@ fn analyzeCmpUnionTag( try mod.errNoteNonLazy(union_ty.declSrcLoc(mod), msg, "union '{}' is not a tagged union", .{union_ty.fmt(mod)}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; // Coerce both the union and the tag to the union's tag type, and then execute the // enum comparison codepath. @@ -16235,7 +16299,7 @@ fn analyzeCmpUnionTag( if (enum_val.isUndef(mod)) return mod.undefRef(Type.bool); const field_ty = union_ty.unionFieldType(enum_val, mod); if (field_ty.zigTypeTag(mod) == .NoReturn) { - return Air.Inst.Ref.bool_false; + return .bool_false; } } @@ -16343,11 +16407,10 @@ fn cmpSelf( return Air.internedToRef(cmp_val.toIntern()); } - if (try sema.compareAll(lhs_val, op, rhs_val, resolved_type)) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (try sema.compareAll(lhs_val, op, rhs_val, resolved_type)) + .bool_true + else + .bool_false; } else { if (resolved_type.zigTypeTag(mod) == .Bool) { // We can lower bool eq/neq more efficiently. @@ -16486,7 +16549,7 @@ fn zirThis( ) CompileError!Air.Inst.Ref { const mod = sema.mod; const this_decl_index = mod.namespaceDeclIndex(block.namespace); - const src = LazySrcLoc.nodeOffset(@as(i32, @bitCast(extended.operand))); + const src = LazySrcLoc.nodeOffset(@bitCast(extended.operand)); return sema.analyzeDeclVal(block, src, this_decl_index); } @@ -16560,7 +16623,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! // TODO add "declared here" note break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (capture == .runtime_val and !block.is_typeof and !block.is_comptime and sema.func_index != .none) { @@ -16590,7 +16653,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! // TODO add "declared here" note break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } switch (capture) { @@ -16624,7 +16687,7 @@ fn zirFrameAddress( block: *Block, extended: Zir.Inst.Extended.InstData, ) CompileError!Air.Inst.Ref { - const src = LazySrcLoc.nodeOffset(@as(i32, @bitCast(extended.operand))); + const src = LazySrcLoc.nodeOffset(@bitCast(extended.operand)); try sema.requireRuntimeBlock(block, src, null); return try block.addNoOp(.frame_addr); } @@ -17223,7 +17286,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai else try mod.intern(.{ .int = .{ .ty = .comptime_int_type, - .storage = .{ .u64 = @as(u64, @intCast(i)) }, + .storage = .{ .u64 = @intCast(i) }, } }); // TODO: write something like getCoercedInts to avoid needing to dupe const name = try sema.arena.dupe(u8, ip.stringToSlice(enum_type.names.get(ip)[i])); @@ -17996,10 +18059,7 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air if (try sema.resolveMaybeUndefVal(operand)) |val| { return if (val.isUndef(mod)) mod.undefRef(Type.bool) - else if (val.toBool()) - Air.Inst.Ref.bool_false - else - Air.Inst.Ref.bool_true; + else if (val.toBool()) .bool_false else .bool_true; } try sema.requireRuntimeBlock(block, src, null); return block.addTyOp(.not, Type.bool, operand); @@ -18025,9 +18085,9 @@ fn zirBoolBr( if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| { if (is_bool_or and lhs_val.toBool()) { - return Air.Inst.Ref.bool_true; + return .bool_true; } else if (!is_bool_or and !lhs_val.toBool()) { - return Air.Inst.Ref.bool_false; + return .bool_false; } // comptime-known left-hand side. No need for a block here; the result // is simply the rhs expression. Here we rely on there only being 1 @@ -18035,7 +18095,7 @@ fn zirBoolBr( return sema.resolveBody(parent_block, body, inst); } - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); try sema.air_instructions.append(gpa, .{ .tag = .block, .data = .{ .ty_pl = .{ @@ -18071,9 +18131,9 @@ fn zirBoolBr( if (!sema.typeOf(rhs_result).isNoReturn(mod)) { if (try sema.resolveDefinedValue(rhs_block, sema.src, rhs_result)) |rhs_val| { if (is_bool_or and rhs_val.toBool()) { - return Air.Inst.Ref.bool_true; + return .bool_true; } else if (!is_bool_or and !rhs_val.toBool()) { - return Air.Inst.Ref.bool_false; + return .bool_false; } } } @@ -18097,8 +18157,8 @@ fn finishCondBr( @typeInfo(Air.Block).Struct.fields.len + child_block.instructions.items.len + 1); const cond_br_payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(then_block.instructions.items.len)), - .else_body_len = @as(u32, @intCast(else_block.instructions.items.len)), + .then_body_len = @intCast(then_block.instructions.items.len), + .else_body_len = @intCast(else_block.instructions.items.len), }); sema.air_extra.appendSliceAssumeCapacity(then_block.instructions.items); sema.air_extra.appendSliceAssumeCapacity(else_block.instructions.items); @@ -18109,7 +18169,7 @@ fn finishCondBr( } } }); sema.air_instructions.items(.data)[block_inst].ty_pl.payload = sema.addExtraAssumeCapacity( - Air.Block{ .body_len = @as(u32, @intCast(child_block.instructions.items.len)) }, + Air.Block{ .body_len = @intCast(child_block.instructions.items.len) }, ); sema.air_extra.appendSliceAssumeCapacity(child_block.instructions.items); @@ -18272,8 +18332,8 @@ fn zirCondbr( .data = .{ .pl_op = .{ .operand = cond, .payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(true_instructions.len)), - .else_body_len = @as(u32, @intCast(sub_block.instructions.items.len)), + .then_body_len = @intCast(true_instructions.len), + .else_body_len = @intCast(sub_block.instructions.items.len), }), } }, }); @@ -18320,7 +18380,7 @@ fn zirTry(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError! .data = .{ .pl_op = .{ .operand = err_union, .payload = sema.addExtraAssumeCapacity(Air.Try{ - .body_len = @as(u32, @intCast(sub_block.instructions.items.len)), + .body_len = @intCast(sub_block.instructions.items.len), }), } }, }); @@ -18380,7 +18440,7 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr .ty = res_ty_ref, .payload = sema.addExtraAssumeCapacity(Air.TryPtr{ .ptr = operand, - .body_len = @as(u32, @intCast(sub_block.instructions.items.len)), + .body_len = @intCast(sub_block.instructions.items.len), }), } }, }); @@ -18396,7 +18456,7 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi const labeled_block = if (!gop.found_existing) blk: { try sema.post_hoc_blocks.ensureUnusedCapacity(sema.gpa, 1); - const new_block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const new_block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); gop.value_ptr.* = Air.indexToRef(new_block_inst); try sema.air_instructions.append(sema.gpa, .{ .tag = .block, @@ -18517,7 +18577,7 @@ fn zirRetImplicit( try sema.errNote(block, r_brace_src, msg, "control flow reaches end of body here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } else if (base_tag != .Void) { const msg = msg: { const msg = try sema.errMsg(block, ret_ty_src, "function with non-void return type '{}' implicitly returns", .{ @@ -18527,7 +18587,7 @@ fn zirRetImplicit( try sema.errNote(block, r_brace_src, msg, "control flow reaches end of body here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return sema.analyzeRet(block, operand, r_brace_src); @@ -18611,8 +18671,8 @@ fn retWithErrTracing( @typeInfo(Air.Block).Struct.fields.len + 1); const cond_br_payload = sema.addExtraAssumeCapacity(Air.CondBr{ - .then_body_len = @as(u32, @intCast(then_block.instructions.items.len)), - .else_body_len = @as(u32, @intCast(else_block.instructions.items.len)), + .then_body_len = @intCast(then_block.instructions.items.len), + .else_body_len = @intCast(else_block.instructions.items.len), }); sema.air_extra.appendSliceAssumeCapacity(then_block.instructions.items); sema.air_extra.appendSliceAssumeCapacity(else_block.instructions.items); @@ -18749,7 +18809,9 @@ fn analyzeRet( if (block.inlining) |inlining| { if (block.is_comptime) { - _ = try sema.resolveConstMaybeUndefVal(block, src, operand, "value being returned at comptime must be comptime-known"); + _ = try sema.resolveConstMaybeUndefVal(block, src, operand, .{ + .needed_comptime_reason = "value being returned at comptime must be comptime-known", + }); inlining.comptime_result = operand; return error.ComptimeReturn; } @@ -18767,7 +18829,7 @@ fn analyzeRet( try sema.errNote(block, src, msg, "can only return using assembly", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } try sema.resolveTypeLayout(sema.fn_ret_ty); @@ -18826,18 +18888,22 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air var extra_i = extra.end; const sentinel = if (inst_data.flags.has_sentinel) blk: { - const ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_i])); + const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]); extra_i += 1; const coerced = try sema.coerce(block, elem_ty, try sema.resolveInst(ref), sentinel_src); - const val = try sema.resolveConstValue(block, sentinel_src, coerced, "pointer sentinel value must be comptime-known"); + const val = try sema.resolveConstValue(block, sentinel_src, coerced, .{ + .needed_comptime_reason = "pointer sentinel value must be comptime-known", + }); break :blk val.toIntern(); } else .none; const abi_align: Alignment = if (inst_data.flags.has_align) blk: { - const ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_i])); + const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]); extra_i += 1; const coerced = try sema.coerce(block, Type.u32, try sema.resolveInst(ref), align_src); - const val = try sema.resolveConstValue(block, align_src, coerced, "pointer alignment must be comptime-known"); + const val = try sema.resolveConstValue(block, align_src, coerced, .{ + .needed_comptime_reason = "pointer alignment must be comptime-known", + }); // Check if this happens to be the lazy alignment of our element type, in // which case we can make this 0 without resolving it. switch (mod.intern_pool.indexToKey(val.toIntern())) { @@ -18847,29 +18913,33 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air }, else => {}, } - const abi_align = @as(u32, @intCast((try val.getUnsignedIntAdvanced(mod, sema)).?)); + const abi_align: u32 = @intCast((try val.getUnsignedIntAdvanced(mod, sema)).?); try sema.validateAlign(block, align_src, abi_align); break :blk Alignment.fromByteUnits(abi_align); } else .none; const address_space: std.builtin.AddressSpace = if (inst_data.flags.has_addrspace) blk: { - const ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_i])); + const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]); extra_i += 1; break :blk try sema.analyzeAddressSpace(block, addrspace_src, ref, .pointer); } else if (elem_ty.zigTypeTag(mod) == .Fn and target.cpu.arch == .avr) .flash else .generic; - const bit_offset = if (inst_data.flags.has_bit_range) blk: { - const ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_i])); + const bit_offset: u16 = if (inst_data.flags.has_bit_range) blk: { + const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]); extra_i += 1; - const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, "pointer bit-offset must be comptime-known"); - break :blk @as(u16, @intCast(bit_offset)); + const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{ + .needed_comptime_reason = "pointer bit-offset must be comptime-known", + }); + break :blk @intCast(bit_offset); } else 0; const host_size: u16 = if (inst_data.flags.has_bit_range) blk: { - const ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_i])); + const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]); extra_i += 1; - const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, "pointer host size must be comptime-known"); - break :blk @as(u16, @intCast(host_size)); + const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{ + .needed_comptime_reason = "pointer host size must be comptime-known", + }); + break :blk @intCast(host_size); } else 0; if (host_size != 0 and bit_offset >= host_size * 8) { @@ -18900,7 +18970,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air try sema.addDeclaredHereNote(msg, elem_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (elem_ty.zigTypeTag(mod) == .Opaque) { return sema.fail(block, elem_ty_src, "C pointers cannot point to opaque types", .{}); @@ -18994,9 +19064,11 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } - const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, "name of field being initialized must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ + .needed_comptime_reason = "name of field being initialized must be comptime-known", + }); const init = try sema.resolveInst(extra.init); return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src); } @@ -19098,13 +19170,15 @@ fn zirStructInit( try sema.errNote(block, other_field_src, msg, "other field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } found_fields[field_index] = item.data.field_type; field_inits[field_index] = try sema.resolveInst(item.data.init); if (!is_packed) if (try resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| { const init_val = (try sema.resolveMaybeUndefVal(field_inits[field_index])) orelse { - return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime-known"); + return sema.failWithNeededComptime(block, field_src, .{ + .needed_comptime_reason = "value stored in comptime field must be comptime-known", + }); }; if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index, mod), mod)) { @@ -19238,7 +19312,7 @@ fn finishStructInit( ); } root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // Find which field forces the expression to be runtime, if any. @@ -19270,7 +19344,7 @@ fn finishStructInit( }); const alloc = try block.addTy(.alloc, alloc_ty); for (field_inits, 0..) |field_init, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); const field_src = dest_src; const field_ptr = try sema.structFieldPtrByIndex(block, dest_src, alloc, i, field_src, struct_ty, true); try sema.storePtr(block, dest_src, field_ptr, field_init); @@ -19362,7 +19436,7 @@ fn structInitAnon( try sema.errNote(block, prev_source, msg, "other field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } gop.value_ptr.* = i; @@ -19378,7 +19452,7 @@ fn structInitAnon( try sema.addDeclaredHereNote(msg, field_ty.toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (try sema.resolveMaybeUndefVal(init)) |init_val| { values[i] = try init_val.intern(field_ty.toType(), mod); @@ -19423,7 +19497,7 @@ fn structInitAnon( const alloc = try block.addTy(.alloc, alloc_ty); var extra_index = extra_end; for (types, 0..) |field_ty, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); const item = switch (kind) { .anon_init => sema.code.extraData(Zir.Inst.StructInitAnon.Item, extra_index), .typed_init => sema.code.extraData(Zir.Inst.StructInit.Item, extra_index), @@ -19504,7 +19578,9 @@ fn zirArrayInit( const init_val = try sema.resolveMaybeUndefVal(resolved_args[i]) orelse { const decl = mod.declPtr(block.src_decl); const elem_src = mod.initSrc(src.node_offset.x, decl, i); - return sema.failWithNeededComptime(block, elem_src, "value stored in comptime field must be comptime-known"); + return sema.failWithNeededComptime(block, elem_src, .{ + .needed_comptime_reason = "value stored in comptime field must be comptime-known", + }); }; if (!field_val.eql(init_val, elem_ty, mod)) { const decl = mod.declPtr(block.src_decl); @@ -19520,7 +19596,7 @@ fn zirArrayInit( const opt_runtime_index: ?u32 = for (resolved_args, 0..) |arg, i| { const comptime_known = try sema.isComptimeKnown(arg); - if (!comptime_known) break @as(u32, @intCast(i)); + if (!comptime_known) break @intCast(i); } else null; const runtime_index = opt_runtime_index orelse { @@ -19629,7 +19705,7 @@ fn arrayInitAnon( try sema.addDeclaredHereNote(msg, types[i].toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (try sema.resolveMaybeUndefVal(elem)) |val| { values[i] = val.toIntern(); @@ -19665,7 +19741,7 @@ fn arrayInitAnon( }); const alloc = try block.addTy(.alloc, alloc_ty); for (operands, 0..) |operand, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); const field_ptr_ty = try mod.ptrType(.{ .child = types[i], .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, @@ -19712,7 +19788,9 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const field_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type); - const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, "field name must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ + .needed_comptime_reason = "field name must be comptime-known", + }); return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src); } @@ -19728,7 +19806,7 @@ fn zirFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A // generic poison should not result in a failed compilation, but the // generic poison type. This prevents unnecessary failures when // constructing types at compile-time. - error.GenericPoison => return Air.Inst.Ref.generic_poison_type, + error.GenericPoison => return .generic_poison_type, else => |e| return e, }; const zir_field_name = sema.code.nullTerminatedString(extra.name_start); @@ -19818,7 +19896,7 @@ fn zirFrame( block: *Block, extended: Zir.Inst.Extended.InstData, ) CompileError!Air.Inst.Ref { - const src = LazySrcLoc.nodeOffset(@as(i32, @bitCast(extended.operand))); + const src = LazySrcLoc.nodeOffset(@bitCast(extended.operand)); return sema.failWithUseOfAsync(block, src); } @@ -19983,7 +20061,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air try sema.resolveTypeLayout(operand_ty); const enum_ty = switch (operand_ty.zigTypeTag(mod)) { .EnumLiteral => { - const val = try sema.resolveConstValue(block, .unneeded, operand, ""); + const val = try sema.resolveConstValue(block, .unneeded, operand, undefined); const tag_name = ip.indexToKey(val.toIntern()).enum_literal; return sema.addStrLit(block, ip.stringToSlice(tag_name)); }, @@ -19997,7 +20075,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air try sema.addDeclaredHereNote(msg, operand_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, else => return sema.fail(block, operand_src, "expected enum or union; found '{}'", .{ operand_ty.fmt(mod), @@ -20023,7 +20101,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air try mod.errNoteNonLazy(enum_decl.srcLoc(mod), msg, "declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; // TODO: write something like getCoercedInts to avoid needing to dupe const field_name = enum_ty.enumFieldName(field_index, mod); @@ -20049,29 +20127,31 @@ fn zirReify( const mod = sema.mod; const gpa = sema.gpa; const ip = &mod.intern_pool; - const name_strategy = @as(Zir.Inst.NameStrategy, @enumFromInt(extended.small)); + const name_strategy: Zir.Inst.NameStrategy = @enumFromInt(extended.small); const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src = LazySrcLoc.nodeOffset(extra.node); const type_info_ty = try sema.getBuiltinType("Type"); const uncasted_operand = try sema.resolveInst(extra.operand); const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src); - const val = try sema.resolveConstValue(block, operand_src, type_info, "operand to @Type must be comptime-known"); + const val = try sema.resolveConstValue(block, operand_src, type_info, .{ + .needed_comptime_reason = "operand to @Type must be comptime-known", + }); const union_val = ip.indexToKey(val.toIntern()).un; const target = mod.getTarget(); if (try union_val.val.toValue().anyUndef(mod)) return sema.failWithUseOfUndef(block, src); const tag_index = type_info_ty.unionTagFieldIndex(union_val.tag.toValue(), mod).?; switch (@as(std.builtin.TypeId, @enumFromInt(tag_index))) { - .Type => return Air.Inst.Ref.type_type, - .Void => return Air.Inst.Ref.void_type, - .Bool => return Air.Inst.Ref.bool_type, - .NoReturn => return Air.Inst.Ref.noreturn_type, - .ComptimeFloat => return Air.Inst.Ref.comptime_float_type, - .ComptimeInt => return Air.Inst.Ref.comptime_int_type, - .Undefined => return Air.Inst.Ref.undefined_type, - .Null => return Air.Inst.Ref.null_type, + .Type => return .type_type, + .Void => return .void_type, + .Bool => return .bool_type, + .NoReturn => return .noreturn_type, + .ComptimeFloat => return .comptime_float_type, + .ComptimeInt => return .comptime_int_type, + .Undefined => return .undefined_type, + .Null => return .null_type, .AnyFrame => return sema.failWithUseOfAsync(block, src), - .EnumLiteral => return Air.Inst.Ref.enum_literal_type, + .EnumLiteral => return .enum_literal_type, .Int => { const fields = ip.typeOf(union_val.val).toType().structFields(mod); const signedness_val = try union_val.val.toValue().fieldValue( @@ -20084,7 +20164,7 @@ fn zirReify( ); const signedness = mod.toEnum(std.builtin.Signedness, signedness_val); - const bits = @as(u16, @intCast(bits_val.toUnsignedInt(mod))); + const bits: u16 = @intCast(bits_val.toUnsignedInt(mod)); const ty = try mod.intType(signedness, bits); return Air.internedToRef(ty.toIntern()); }, @@ -20097,7 +20177,7 @@ fn zirReify( try ip.getOrPutString(gpa, "child"), ).?); - const len = @as(u32, @intCast(len_val.toUnsignedInt(mod))); + const len: u32 = @intCast(len_val.toUnsignedInt(mod)); const child_ty = child_val.toType(); try sema.checkVectorElemType(block, src, child_ty); @@ -20114,7 +20194,7 @@ fn zirReify( try ip.getOrPutString(gpa, "bits"), ).?); - const bits = @as(u16, @intCast(bits_val.toUnsignedInt(mod))); + const bits: u16 = @intCast(bits_val.toUnsignedInt(mod)); const ty = switch (bits) { 16 => Type.f16, 32 => Type.f32, @@ -20206,7 +20286,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, elem_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (elem_ty.zigTypeTag(mod) == .Opaque) { return sema.fail(block, src, "C pointers cannot point to opaque types", .{}); @@ -20382,7 +20462,7 @@ fn zirReify( } // Define our empty enum decl - const fields_len = @as(u32, @intCast(try sema.usizeCast(block, src, fields_val.sliceLen(mod)))); + const fields_len: u32 = @intCast(try sema.usizeCast(block, src, fields_val.sliceLen(mod))); const incomplete_enum = try ip.getIncompleteEnum(gpa, .{ .decl = new_decl_index, .namespace = .none, @@ -20431,7 +20511,7 @@ fn zirReify( try sema.errNote(block, src, msg, "other field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (try incomplete_enum.addFieldValue(ip, gpa, (try mod.getCoerced(value_val, int_tag_ty)).toIntern())) |other| { @@ -20442,7 +20522,7 @@ fn zirReify( try sema.errNote(block, src, msg, "other enum tag value here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -20579,7 +20659,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, enum_tag_ty.toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; // No check for duplicate because the check already happened in order // to create the enum type in the first place. @@ -20610,7 +20690,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (layout == .Extern and !try sema.validateExternType(field_ty, .union_field)) { const msg = msg: { @@ -20623,7 +20703,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } else if (layout == .Packed and !(validatePackedType(field_ty, mod))) { const msg = msg: { const msg = try sema.errMsg(block, src, "packed unions cannot contain fields of type '{}'", .{field_ty.fmt(mod)}); @@ -20635,7 +20715,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -20655,7 +20735,7 @@ fn zirReify( try sema.addDeclaredHereNote(msg, enum_tag_ty.toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } else { enum_tag_ty = try sema.generateUnionTagTypeSimple(block, enum_field_names, .none); @@ -20753,7 +20833,7 @@ fn zirReify( if (!try sema.intFitsInType(alignment_val, Type.u32, null)) { return sema.fail(block, src, "alignment must fit in 'u32'", .{}); } - const alignment = @as(u29, @intCast(alignment_val.toUnsignedInt(mod))); + const alignment: u29 = @intCast(alignment_val.toUnsignedInt(mod)); if (alignment == target_util.defaultFunctionAlignment(target)) { break :alignment .none; } else { @@ -20948,7 +21028,9 @@ fn reifyStruct( const field_ty = type_val.toType(); const default_val = if (default_value_val.optionalValue(mod)) |opt_val| (try sema.pointerDeref(block, src, opt_val, try mod.singleConstPtrType(field_ty)) orelse - return sema.failWithNeededComptime(block, src, "struct field default value must be comptime-known")).toIntern() + return sema.failWithNeededComptime(block, src, .{ + .needed_comptime_reason = "struct field default value must be comptime-known", + })).toIntern() else .none; if (is_comptime_val.toBool() and default_val == .none) { @@ -20971,7 +21053,7 @@ fn reifyStruct( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (field_ty.zigTypeTag(mod) == .NoReturn) { const msg = msg: { @@ -20981,7 +21063,7 @@ fn reifyStruct( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (struct_obj.layout == .Extern and !try sema.validateExternType(field_ty, .struct_field)) { const msg = msg: { @@ -20994,7 +21076,7 @@ fn reifyStruct( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } else if (struct_obj.layout == .Packed and !(validatePackedType(field_ty, mod))) { const msg = msg: { const msg = try sema.errMsg(block, src, "packed structs cannot contain fields of type '{}'", .{field_ty.fmt(sema.mod)}); @@ -21006,7 +21088,7 @@ fn reifyStruct( try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -21034,7 +21116,7 @@ fn reifyStruct( try sema.checkBackingIntType(block, src, backing_int_ty, fields_bit_sum); struct_obj.backing_int_ty = backing_int_ty; } else { - struct_obj.backing_int_ty = try mod.intType(.unsigned, @as(u16, @intCast(fields_bit_sum))); + struct_obj.backing_int_ty = try mod.intType(.unsigned, @intCast(fields_bit_sum)); } struct_obj.status = .have_layout; @@ -21074,7 +21156,7 @@ fn zirCVaArg(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C try sema.addDeclaredHereNote(msg, arg_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } try sema.requireRuntimeBlock(block, src, null); @@ -21105,7 +21187,7 @@ fn zirCVaEnd(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C } fn zirCVaStart(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { - const src = LazySrcLoc.nodeOffset(@as(i32, @bitCast(extended.operand))); + const src = LazySrcLoc.nodeOffset(@bitCast(extended.operand)); const va_list_ty = try sema.getBuiltinType("VaList"); try sema.requireRuntimeBlock(block, src, null); @@ -21180,7 +21262,9 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const result_val = try sema.intFromFloat(block, operand_src, operand_val, operand_ty, dest_ty); return Air.internedToRef(result_val.toIntern()); } else if (dest_scalar_ty.zigTypeTag(mod) == .ComptimeInt) { - return sema.failWithNeededComptime(block, operand_src, "value being casted to 'comptime_int' must be comptime-known"); + return sema.failWithNeededComptime(block, operand_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known", + }); } try sema.requireRuntimeBlock(block, inst_data.src(), operand_src); @@ -21260,7 +21344,9 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, mod, sema); return Air.internedToRef(result_val.toIntern()); } else if (dest_scalar_ty.zigTypeTag(mod) == .ComptimeFloat) { - return sema.failWithNeededComptime(block, operand_src, "value being casted to 'comptime_float' must be comptime-known"); + return sema.failWithNeededComptime(block, operand_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known", + }); } try sema.requireRuntimeBlock(block, src, operand_src); @@ -21311,7 +21397,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! try sema.errNote(block, src, msg, "slice length cannot be inferred from address", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| { @@ -21447,7 +21533,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat try sema.addDeclaredHereNote(msg, dest_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (maybe_operand_val) |val| { @@ -21465,7 +21551,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat try sema.addDeclaredHereNote(msg, dest_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -21482,7 +21568,10 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat } fn zirPtrCastFull(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { - const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as(u5, @truncate(extended.small))); + const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as( + @typeInfo(Zir.Inst.FullPtrCastFlags).Struct.backing_integer.?, + @truncate(extended.small), + )); const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; const src = LazySrcLoc.nodeOffset(extra.node); const operand_src: LazySrcLoc = .{ .node_offset_ptrcast_operand = extra.node }; @@ -21568,7 +21657,7 @@ fn ptrCastFull( if (src_slice_like and dest_slice_like) break :check_size; if (src_info.flags.size == .C) break :check_size; if (dest_info.flags.size == .C) break :check_size; - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "cannot implicitly convert {s} pointer to {s} pointer", .{ pointerSizeString(src_info.flags.size), pointerSizeString(dest_info.flags.size), @@ -21604,7 +21693,7 @@ fn ptrCastFull( operand_src, ); if (imc_res == .ok) break :check_child; - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "pointer element type '{}' cannot coerce into element type '{}'", .{ src_child.fmt(mod), dest_child.fmt(mod), @@ -21631,7 +21720,7 @@ fn ptrCastFull( if (dest_info.sentinel == coerced_sent) break :check_sent; } } - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = if (src_info.sentinel == .none) blk: { break :blk try sema.errMsg(block, src, "destination pointer requires '{}' sentinel", .{ dest_info.sentinel.toValue().fmtValue(dest_info.child.toType(), mod), @@ -21649,7 +21738,7 @@ fn ptrCastFull( } if (src_info.packed_offset.host_size != dest_info.packed_offset.host_size) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "pointer host size '{}' cannot coerce into pointer host size '{}'", .{ src_info.packed_offset.host_size, dest_info.packed_offset.host_size, @@ -21661,7 +21750,7 @@ fn ptrCastFull( } if (src_info.packed_offset.bit_offset != dest_info.packed_offset.bit_offset) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "pointer bit offset '{}' cannot coerce into pointer bit offset '{}'", .{ src_info.packed_offset.bit_offset, dest_info.packed_offset.bit_offset, @@ -21678,7 +21767,7 @@ fn ptrCastFull( if (!src_allows_zero) break :check_allowzero; if (dest_allows_zero) break :check_allowzero; - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "'{}' could have null values which are illegal in type '{}'", .{ operand_ty.fmt(mod), dest_ty.fmt(mod), @@ -21696,7 +21785,7 @@ fn ptrCastFull( const dest_align = dest_info.flags.alignment.toByteUnitsOptional() orelse dest_info.child.toType().abiAlignment(mod); if (!flags.align_cast) { if (dest_align > src_align) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "cast increases pointer alignment", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, operand_src, msg, "'{}' has alignment '{d}'", .{ @@ -21713,7 +21802,7 @@ fn ptrCastFull( if (!flags.addrspace_cast) { if (src_info.flags.address_space != dest_info.flags.address_space) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "cast changes pointer address space", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, operand_src, msg, "'{}' has address space '{s}'", .{ @@ -21729,7 +21818,7 @@ fn ptrCastFull( } else { // Some address space casts are always disallowed if (!target_util.addrSpaceCastIsValid(mod.getTarget(), src_info.flags.address_space, dest_info.flags.address_space)) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "invalid address space cast", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, operand_src, msg, "address space '{s}' is not compatible with address space '{s}'", .{ @@ -21743,7 +21832,7 @@ fn ptrCastFull( if (!flags.const_cast) { if (src_info.flags.is_const and !dest_info.flags.is_const) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "cast discards const qualifier", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, src, msg, "use @constCast to discard const qualifier", .{}); @@ -21754,7 +21843,7 @@ fn ptrCastFull( if (!flags.volatile_cast) { if (src_info.flags.is_volatile and !dest_info.flags.is_volatile) { - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "cast discards volatile qualifier", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, src, msg, "use @volatileCast to discard volatile qualifier", .{}); @@ -21885,7 +21974,10 @@ fn ptrCastFull( fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { const mod = sema.mod; - const flags = @as(Zir.Inst.FullPtrCastFlags, @bitCast(@as(u5, @truncate(extended.small)))); + const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as( + @typeInfo(Zir.Inst.FullPtrCastFlags).Struct.backing_integer.?, + @truncate(extended.small), + )); const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src = LazySrcLoc.nodeOffset(extra.node); const operand_src: LazySrcLoc = .{ .node_offset_ptrcast_operand = extra.node }; @@ -21962,7 +22054,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai }); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -22174,7 +22266,9 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; const ty = try sema.resolveType(block, lhs_src, extra.lhs); - const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, "name of field must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ + .needed_comptime_reason = "name of field must be comptime-known", + }); const mod = sema.mod; try sema.resolveTypeLayout(ty); @@ -22187,7 +22281,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 try sema.addDeclaredHereNote(msg, ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } @@ -22298,7 +22392,7 @@ fn checkPtrOperand( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Optional => if (ty.childType(mod).zigTypeTag(mod) == .Pointer) return, else => {}, @@ -22329,7 +22423,7 @@ fn checkPtrType( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .Optional => if (ty.childType(mod).zigTypeTag(mod) == .Pointer) return, else => {}, @@ -22470,7 +22564,7 @@ fn checkComptimeVarStore( try sema.errNote(block, cond_src, msg, "runtime condition here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (block.runtime_loop) |loop_src| { const msg = msg: { @@ -22479,7 +22573,7 @@ fn checkComptimeVarStore( try sema.errNote(block, loop_src, msg, "non-inline loop here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } unreachable; } @@ -22621,7 +22715,7 @@ fn checkVectorizableBinaryOperands( try sema.errNote(block, rhs_src, msg, "length {d} here", .{rhs_len}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } else { const msg = msg: { @@ -22638,7 +22732,7 @@ fn checkVectorizableBinaryOperands( } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -22667,16 +22761,22 @@ fn resolveExportOptions( const visibility_src = sema.maybeOptionsSrc(block, src, "visibility"); const name_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "name"), name_src); - const name_val = try sema.resolveConstValue(block, name_src, name_operand, "name of exported value must be comptime-known"); + const name_val = try sema.resolveConstValue(block, name_src, name_operand, .{ + .needed_comptime_reason = "name of exported value must be comptime-known", + }); const name_ty = Type.slice_const_u8; const name = try name_val.toAllocatedBytes(name_ty, sema.arena, mod); const linkage_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "linkage"), linkage_src); - const linkage_val = try sema.resolveConstValue(block, linkage_src, linkage_operand, "linkage of exported value must be comptime-known"); + const linkage_val = try sema.resolveConstValue(block, linkage_src, linkage_operand, .{ + .needed_comptime_reason = "linkage of exported value must be comptime-known", + }); const linkage = mod.toEnum(std.builtin.GlobalLinkage, linkage_val); const section_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "section"), section_src); - const section_opt_val = try sema.resolveConstValue(block, section_src, section_operand, "linksection of exported value must be comptime-known"); + const section_opt_val = try sema.resolveConstValue(block, section_src, section_operand, .{ + .needed_comptime_reason = "linksection of exported value must be comptime-known", + }); const section_ty = Type.slice_const_u8; const section = if (section_opt_val.optionalValue(mod)) |section_val| try section_val.toAllocatedBytes(section_ty, sema.arena, mod) @@ -22684,7 +22784,9 @@ fn resolveExportOptions( null; const visibility_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "visibility"), visibility_src); - const visibility_val = try sema.resolveConstValue(block, visibility_src, visibility_operand, "visibility of exported value must be comptime-known"); + const visibility_val = try sema.resolveConstValue(block, visibility_src, visibility_operand, .{ + .needed_comptime_reason = "visibility of exported value must be comptime-known", + }); const visibility = mod.toEnum(std.builtin.SymbolVisibility, visibility_val); if (name.len < 1) { @@ -22711,7 +22813,7 @@ fn resolveBuiltinEnum( src: LazySrcLoc, zir_ref: Zir.Inst.Ref, comptime name: []const u8, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!@field(std.builtin, name) { const mod = sema.mod; const ty = try sema.getBuiltinType(name); @@ -22726,7 +22828,7 @@ fn resolveAtomicOrder( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: []const u8, + reason: NeededComptimeReason, ) CompileError!std.builtin.AtomicOrder { return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicOrder", reason); } @@ -22737,7 +22839,9 @@ fn resolveAtomicRmwOp( src: LazySrcLoc, zir_ref: Zir.Inst.Ref, ) CompileError!std.builtin.AtomicRmwOp { - return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", "@atomicRmW operation must be comptime-known"); + return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{ + .needed_comptime_reason = "@atomicRmW operation must be comptime-known", + }); } fn zirCmpxchg( @@ -22774,8 +22878,12 @@ fn zirCmpxchg( const uncasted_ptr = try sema.resolveInst(extra.ptr); const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false); const new_value = try sema.coerce(block, elem_ty, try sema.resolveInst(extra.new_value), new_value_src); - const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, "atomic order of cmpxchg success must be comptime-known"); - const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, "atomic order of cmpxchg failure must be comptime-known"); + const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, .{ + .needed_comptime_reason = "atomic order of cmpxchg success must be comptime-known", + }); + const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, .{ + .needed_comptime_reason = "atomic order of cmpxchg failure must be comptime-known", + }); if (@intFromEnum(success_order) < @intFromEnum(std.builtin.AtomicOrder.Monotonic)) { return sema.fail(block, success_order_src, "success atomic ordering must be Monotonic or stricter", .{}); @@ -22867,7 +22975,9 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; const op_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; - const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", "@reduce operation must be comptime-known"); + const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{ + .needed_comptime_reason = "@reduce operation must be comptime-known", + }); const operand = try sema.resolveInst(extra.rhs); const operand_ty = sema.typeOf(operand); const mod = sema.mod; @@ -22950,12 +23060,14 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air else => return sema.fail(block, mask_src, "expected vector or array, found '{}'", .{sema.typeOf(mask).fmt(sema.mod)}), }; mask_ty = try mod.vectorType(.{ - .len = @as(u32, @intCast(mask_len)), + .len = @intCast(mask_len), .child = .i32_type, }); mask = try sema.coerce(block, mask_ty, mask, mask_src); - const mask_val = try sema.resolveConstMaybeUndefVal(block, mask_src, mask, "shuffle mask must be comptime-known"); - return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @as(u32, @intCast(mask_len))); + const mask_val = try sema.resolveConstMaybeUndefVal(block, mask_src, mask, .{ + .needed_comptime_reason = "shuffle mask must be comptime-known", + }); + return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(mask_len)); } fn analyzeShuffle( @@ -22999,8 +23111,8 @@ fn analyzeShuffle( if (maybe_a_len == null and maybe_b_len == null) { return mod.undefRef(res_ty); } - const a_len = @as(u32, @intCast(maybe_a_len orelse maybe_b_len.?)); - const b_len = @as(u32, @intCast(maybe_b_len orelse a_len)); + const a_len: u32 = @intCast(maybe_a_len orelse maybe_b_len.?); + const b_len: u32 = @intCast(maybe_b_len orelse a_len); const a_ty = try mod.vectorType(.{ .len = a_len, @@ -23019,17 +23131,17 @@ fn analyzeShuffle( .{ b_len, b_src, b_ty }, }; - for (0..@as(usize, @intCast(mask_len))) |i| { + for (0..@intCast(mask_len)) |i| { const elem = try mask.elemValue(sema.mod, i); if (elem.isUndef(mod)) continue; const int = elem.toSignedInt(mod); var unsigned: u32 = undefined; var chosen: u32 = undefined; if (int >= 0) { - unsigned = @as(u32, @intCast(int)); + unsigned = @intCast(int); chosen = 0; } else { - unsigned = @as(u32, @intCast(~int)); + unsigned = @intCast(~int); chosen = 1; } if (unsigned >= operand_info[chosen][0]) { @@ -23048,7 +23160,7 @@ fn analyzeShuffle( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -23062,7 +23174,7 @@ fn analyzeShuffle( continue; } const int = mask_elem_val.toSignedInt(mod); - const unsigned = if (int >= 0) @as(u32, @intCast(int)) else @as(u32, @intCast(~int)); + const unsigned: u32 = @intCast(if (int >= 0) int else ~int); values[i] = try (try (if (int >= 0) a_val else b_val).elemValue(mod, unsigned)).intern(elem_ty, mod); } return Air.internedToRef((try mod.intern(.{ .aggregate = .{ @@ -23083,23 +23195,23 @@ fn analyzeShuffle( const max_len = try sema.usizeCast(block, max_src, @max(a_len, b_len)); const expand_mask_values = try sema.arena.alloc(InternPool.Index, max_len); - for (@as(usize, @intCast(0))..@as(usize, @intCast(min_len))) |i| { + for (@intCast(0)..@intCast(min_len)) |i| { expand_mask_values[i] = (try mod.intValue(Type.comptime_int, i)).toIntern(); } - for (@as(usize, @intCast(min_len))..@as(usize, @intCast(max_len))) |i| { + for (@intCast(min_len)..@intCast(max_len)) |i| { expand_mask_values[i] = (try mod.intValue(Type.comptime_int, -1)).toIntern(); } const expand_mask = try mod.intern(.{ .aggregate = .{ - .ty = (try mod.vectorType(.{ .len = @as(u32, @intCast(max_len)), .child = .comptime_int_type })).toIntern(), + .ty = (try mod.vectorType(.{ .len = @intCast(max_len), .child = .comptime_int_type })).toIntern(), .storage = .{ .elems = expand_mask_values }, } }); if (a_len < b_len) { const undef = try mod.undefRef(a_ty); - a = try sema.analyzeShuffle(block, src_node, elem_ty, a, undef, expand_mask.toValue(), @as(u32, @intCast(max_len))); + a = try sema.analyzeShuffle(block, src_node, elem_ty, a, undef, expand_mask.toValue(), @intCast(max_len)); } else { const undef = try mod.undefRef(b_ty); - b = try sema.analyzeShuffle(block, src_node, elem_ty, b, undef, expand_mask.toValue(), @as(u32, @intCast(max_len))); + b = try sema.analyzeShuffle(block, src_node, elem_ty, b, undef, expand_mask.toValue(), @intCast(max_len)); } } @@ -23136,7 +23248,7 @@ fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C .Vector, .Array => pred_ty.arrayLen(mod), else => return sema.fail(block, pred_src, "expected vector or array, found '{}'", .{pred_ty.fmt(mod)}), }; - const vec_len = @as(u32, @intCast(try sema.usizeCast(block, pred_src, vec_len_u64))); + const vec_len: u32 = @intCast(try sema.usizeCast(block, pred_src, vec_len_u64)); const bool_vec_ty = try mod.vectorType(.{ .len = vec_len, @@ -23218,7 +23330,9 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type); const uncasted_ptr = try sema.resolveInst(extra.ptr); const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, true); - const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicLoad must be comptime-known"); + const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ + .needed_comptime_reason = "atomic order of @atomicLoad must be comptime-known", + }); switch (order) { .Release, .AcqRel => { @@ -23283,7 +23397,9 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A }, else => {}, } - const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicRmW must be comptime-known"); + const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ + .needed_comptime_reason = "atomic order of @atomicRmW must be comptime-known", + }); if (order == .Unordered) { return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be Unordered", .{}); @@ -23350,7 +23466,9 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError const elem_ty = sema.typeOf(operand); const uncasted_ptr = try sema.resolveInst(extra.ptr); const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false); - const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, "atomic order of @atomicStore must be comptime-known"); + const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ + .needed_comptime_reason = "atomic order of @atomicStore must be comptime-known", + }); const air_tag: Air.Inst.Tag = switch (order) { .Acquire, .AcqRel => { @@ -23451,7 +23569,9 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError const modifier_ty = try sema.getBuiltinType("CallModifier"); const air_ref = try sema.resolveInst(extra.modifier); const modifier_ref = try sema.coerce(block, modifier_ty, air_ref, modifier_src); - const modifier_val = try sema.resolveConstValue(block, modifier_src, modifier_ref, "call modifier must be comptime-known"); + const modifier_val = try sema.resolveConstValue(block, modifier_src, modifier_ref, .{ + .needed_comptime_reason = "call modifier must be comptime-known", + }); var modifier = mod.toEnum(std.builtin.CallModifier, modifier_val); switch (modifier) { // These can be upgraded to comptime or nosuspend calls. @@ -23504,7 +23624,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError var resolved_args: []Air.Inst.Ref = try sema.arena.alloc(Air.Inst.Ref, args_ty.structFieldCount(mod)); for (resolved_args, 0..) |*resolved, i| { - resolved.* = try sema.tupleFieldValByIndex(block, args_src, args, @as(u32, @intCast(i)), args_ty); + resolved.* = try sema.tupleFieldValByIndex(block, args_src, args, @intCast(i), args_ty); } const callee_ty = sema.typeOf(func); @@ -23536,7 +23656,9 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node }; const parent_ty = try sema.resolveType(block, ty_src, extra.parent_type); - const field_name = try sema.resolveConstStringIntern(block, name_src, extra.field_name, "field name must be comptime-known"); + const field_name = try sema.resolveConstStringIntern(block, name_src, extra.field_name, .{ + .needed_comptime_reason = "field name must be comptime-known", + }); const field_ptr = try sema.resolveInst(extra.field_ptr); const field_ptr_ty = sema.typeOf(field_ptr); const mod = sema.mod; @@ -23623,7 +23745,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr try sema.addDeclaredHereNote(msg, parent_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return Air.internedToRef(field.base); } @@ -23636,7 +23758,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr .ty = Air.internedToRef(result_ptr.toIntern()), .payload = try block.sema.addExtra(Air.FieldParentPtr{ .field_ptr = casted_field_ptr, - .field_index = @as(u32, @intCast(field_index)), + .field_index = @intCast(field_index), }), } }, }); @@ -23977,7 +24099,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void }); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } var len_val: ?Value = null; @@ -23999,7 +24121,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void }); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } break :check; } @@ -24192,7 +24314,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void }, .Many, .C => {}, } - return sema.failWithOwnedErrorMsg(msg: { + return sema.failWithOwnedErrorMsg(block, msg: { const msg = try sema.errMsg(block, src, "unknown @memset length", .{}); errdefer msg.destroy(sema.gpa); try sema.errNote(block, dest_src, msg, "destination type '{}' provides no length", .{ @@ -24295,7 +24417,7 @@ fn zirVarExtended( const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand); const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 }; const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 }; - const small = @as(Zir.Inst.ExtendedVar.Small, @bitCast(extended.small)); + const small: Zir.Inst.ExtendedVar.Small = @bitCast(extended.small); var extra_index: usize = extra.end; @@ -24310,7 +24432,7 @@ fn zirVarExtended( assert(!small.has_align); const uncasted_init: Air.Inst.Ref = if (small.has_init) blk: { - const init_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const init_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; break :blk try sema.resolveInst(init_ref); } else .none; @@ -24327,8 +24449,11 @@ fn zirVarExtended( else uncasted_init; - break :blk ((try sema.resolveMaybeUndefVal(init)) orelse - return sema.failWithNeededComptime(block, init_src, "container level variable initializers must be comptime-known")).toIntern(); + break :blk ((try sema.resolveMaybeUndefVal(init)) orelse { + return sema.failWithNeededComptime(block, init_src, .{ + .needed_comptime_reason = "container level variable initializers must be comptime-known", + }); + }).toIntern(); } else .none; try sema.validateVarType(block, ty_src, var_ty, small.is_extern); @@ -24383,11 +24508,13 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A const body = sema.code.extra[extra_index..][0..body_len]; extra_index += body.len; - const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29, "alignment must be comptime-known"); + const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29, .{ + .needed_comptime_reason = "alignment must be comptime-known", + }); if (val.isGenericPoison()) { break :blk null; } - const alignment = @as(u32, @intCast(val.toUnsignedInt(mod))); + const alignment: u32 = @intCast(val.toUnsignedInt(mod)); try sema.validateAlign(block, align_src, alignment); if (alignment == target_util.defaultFunctionAlignment(target)) { break :blk .none; @@ -24395,15 +24522,17 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A break :blk Alignment.fromNonzeroByteUnits(alignment); } } else if (extra.data.bits.has_align_ref) blk: { - const align_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const align_tv = sema.resolveInstConst(block, align_src, align_ref, "alignment must be comptime-known") catch |err| switch (err) { + const align_tv = sema.resolveInstConst(block, align_src, align_ref, .{ + .needed_comptime_reason = "alignment must be comptime-known", + }) catch |err| switch (err) { error.GenericPoison => { break :blk null; }, else => |e| return e, }; - const alignment = @as(u32, @intCast(align_tv.val.toUnsignedInt(mod))); + const alignment: u32 = @intCast(align_tv.val.toUnsignedInt(mod)); try sema.validateAlign(block, align_src, alignment); if (alignment == target_util.defaultFunctionAlignment(target)) { break :blk .none; @@ -24419,15 +24548,19 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A extra_index += body.len; const addrspace_ty = try sema.getBuiltinType("AddressSpace"); - const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, "addrespace must be comptime-known"); + const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, .{ + .needed_comptime_reason = "addrspace must be comptime-known", + }); if (val.isGenericPoison()) { break :blk null; } break :blk mod.toEnum(std.builtin.AddressSpace, val); } else if (extra.data.bits.has_addrspace_ref) blk: { - const addrspace_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const addrspace_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref, "addrespace must be comptime-known") catch |err| switch (err) { + const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref, .{ + .needed_comptime_reason = "addrspace must be comptime-known", + }) catch |err| switch (err) { error.GenericPoison => { break :blk null; }, @@ -24443,15 +24576,19 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A extra_index += body.len; const ty = Type.slice_const_u8; - const val = try sema.resolveGenericBody(block, section_src, body, inst, ty, "linksection must be comptime-known"); + const val = try sema.resolveGenericBody(block, section_src, body, inst, ty, .{ + .needed_comptime_reason = "linksection must be comptime-known", + }); if (val.isGenericPoison()) { break :blk .generic; } break :blk .{ .explicit = try val.toIpString(ty, mod) }; } else if (extra.data.bits.has_section_ref) blk: { - const section_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const section_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const section_name = sema.resolveConstStringIntern(block, section_src, section_ref, "linksection must be comptime-known") catch |err| switch (err) { + const section_name = sema.resolveConstStringIntern(block, section_src, section_ref, .{ + .needed_comptime_reason = "linksection must be comptime-known", + }) catch |err| switch (err) { error.GenericPoison => { break :blk .generic; }, @@ -24467,15 +24604,19 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A extra_index += body.len; const cc_ty = try sema.getBuiltinType("CallingConvention"); - const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, "calling convention must be comptime-known"); + const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{ + .needed_comptime_reason = "calling convention must be comptime-known", + }); if (val.isGenericPoison()) { break :blk null; } break :blk mod.toEnum(std.builtin.CallingConvention, val); } else if (extra.data.bits.has_cc_ref) blk: { - const cc_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref, "calling convention must be comptime-known") catch |err| switch (err) { + const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref, .{ + .needed_comptime_reason = "calling convention must be comptime-known", + }) catch |err| switch (err) { error.GenericPoison => { break :blk null; }, @@ -24493,13 +24634,17 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A const body = sema.code.extra[extra_index..][0..body_len]; extra_index += body.len; - const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, "return type must be comptime-known"); + const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{ + .needed_comptime_reason = "return type must be comptime-known", + }); const ty = val.toType(); break :blk ty; } else if (extra.data.bits.has_ret_ty_ref) blk: { - const ret_ty_ref = @as(Zir.Inst.Ref, @enumFromInt(sema.code.extra[extra_index])); + const ret_ty_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]); extra_index += 1; - const ret_ty_tv = sema.resolveInstConst(block, ret_src, ret_ty_ref, "return type must be comptime-known") catch |err| switch (err) { + const ret_ty_tv = sema.resolveInstConst(block, ret_src, ret_ty_ref, .{ + .needed_comptime_reason = "return type must be comptime-known", + }) catch |err| switch (err) { error.GenericPoison => { break :blk Type.generic_poison; }, @@ -24554,9 +24699,11 @@ fn zirCUndef( const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; - const name = try sema.resolveConstString(block, src, extra.operand, "name of macro being undefined must be comptime-known"); + const name = try sema.resolveConstString(block, src, extra.operand, .{ + .needed_comptime_reason = "name of macro being undefined must be comptime-known", + }); try block.c_import_buf.?.writer().print("#undef {s}\n", .{name}); - return Air.Inst.Ref.void_value; + return .void_value; } fn zirCInclude( @@ -24567,9 +24714,11 @@ fn zirCInclude( const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; const src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; - const name = try sema.resolveConstString(block, src, extra.operand, "path being included must be comptime-known"); + const name = try sema.resolveConstString(block, src, extra.operand, .{ + .needed_comptime_reason = "path being included must be comptime-known", + }); try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name}); - return Air.Inst.Ref.void_value; + return .void_value; } fn zirCDefine( @@ -24582,15 +24731,19 @@ fn zirCDefine( const name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node }; const val_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node }; - const name = try sema.resolveConstString(block, name_src, extra.lhs, "name of macro being undefined must be comptime-known"); + const name = try sema.resolveConstString(block, name_src, extra.lhs, .{ + .needed_comptime_reason = "name of macro being undefined must be comptime-known", + }); const rhs = try sema.resolveInst(extra.rhs); if (sema.typeOf(rhs).zigTypeTag(mod) != .Void) { - const value = try sema.resolveConstString(block, val_src, extra.rhs, "value of macro being undefined must be comptime-known"); + const value = try sema.resolveConstString(block, val_src, extra.rhs, .{ + .needed_comptime_reason = "value of macro being undefined must be comptime-known", + }); try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value }); } else { try block.c_import_buf.?.writer().print("#define {s}\n", .{name}); } - return Air.Inst.Ref.void_value; + return .void_value; } fn zirWasmMemorySize( @@ -24606,7 +24759,9 @@ fn zirWasmMemorySize( return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)}); } - const index = @as(u32, @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, "wasm memory size index must be comptime-known"))); + const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{ + .needed_comptime_reason = "wasm memory size index must be comptime-known", + })); try sema.requireRuntimeBlock(block, builtin_src, null); return block.addInst(.{ .tag = .wasm_memory_size, @@ -24631,7 +24786,9 @@ fn zirWasmMemoryGrow( return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)}); } - const index = @as(u32, @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, "wasm memory size index must be comptime-known"))); + const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{ + .needed_comptime_reason = "wasm memory size index must be comptime-known", + })); const delta = try sema.coerce(block, Type.u32, try sema.resolveInst(extra.rhs), delta_src); try sema.requireRuntimeBlock(block, builtin_src, null); @@ -24661,17 +24818,23 @@ fn resolvePrefetchOptions( const cache_src = sema.maybeOptionsSrc(block, src, "cache"); const rw = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "rw"), rw_src); - const rw_val = try sema.resolveConstValue(block, rw_src, rw, "prefetch read/write must be comptime-known"); + const rw_val = try sema.resolveConstValue(block, rw_src, rw, .{ + .needed_comptime_reason = "prefetch read/write must be comptime-known", + }); const locality = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "locality"), locality_src); - const locality_val = try sema.resolveConstValue(block, locality_src, locality, "prefetch locality must be comptime-known"); + const locality_val = try sema.resolveConstValue(block, locality_src, locality, .{ + .needed_comptime_reason = "prefetch locality must be comptime-known", + }); const cache = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "cache"), cache_src); - const cache_val = try sema.resolveConstValue(block, cache_src, cache, "prefetch cache must be comptime-known"); + const cache_val = try sema.resolveConstValue(block, cache_src, cache, .{ + .needed_comptime_reason = "prefetch cache must be comptime-known", + }); return std.builtin.PrefetchOptions{ .rw = mod.toEnum(std.builtin.PrefetchOptions.Rw, rw_val), - .locality = @as(u2, @intCast(locality_val.toUnsignedInt(mod))), + .locality = @intCast(locality_val.toUnsignedInt(mod)), .cache = mod.toEnum(std.builtin.PrefetchOptions.Cache, cache_val), }; } @@ -24707,7 +24870,7 @@ fn zirPrefetch( }); } - return Air.Inst.Ref.void_value; + return .void_value; } fn resolveExternOptions( @@ -24734,18 +24897,26 @@ fn resolveExternOptions( const thread_local_src = sema.maybeOptionsSrc(block, src, "thread_local"); const name_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "name"), name_src); - const name_val = try sema.resolveConstValue(block, name_src, name_ref, "name of the extern symbol must be comptime-known"); + const name_val = try sema.resolveConstValue(block, name_src, name_ref, .{ + .needed_comptime_reason = "name of the extern symbol must be comptime-known", + }); const name = try name_val.toAllocatedBytes(Type.slice_const_u8, sema.arena, mod); const library_name_inst = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "library_name"), library_src); - const library_name_val = try sema.resolveConstValue(block, library_src, library_name_inst, "library in which extern symbol is must be comptime-known"); + const library_name_val = try sema.resolveConstValue(block, library_src, library_name_inst, .{ + .needed_comptime_reason = "library in which extern symbol is must be comptime-known", + }); const linkage_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "linkage"), linkage_src); - const linkage_val = try sema.resolveConstValue(block, linkage_src, linkage_ref, "linkage of the extern symbol must be comptime-known"); + const linkage_val = try sema.resolveConstValue(block, linkage_src, linkage_ref, .{ + .needed_comptime_reason = "linkage of the extern symbol must be comptime-known", + }); const linkage = mod.toEnum(std.builtin.GlobalLinkage, linkage_val); const is_thread_local = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, "is_thread_local"), thread_local_src); - const is_thread_local_val = try sema.resolveConstValue(block, thread_local_src, is_thread_local, "threadlocality of the extern symbol must be comptime-known"); + const is_thread_local_val = try sema.resolveConstValue(block, thread_local_src, is_thread_local, .{ + .needed_comptime_reason = "threadlocality of the extern symbol must be comptime-known", + }); const library_name = if (library_name_val.optionalValue(mod)) |payload| blk: { const library_name = try payload.toAllocatedBytes(Type.slice_const_u8, sema.arena, mod); @@ -24793,7 +24964,7 @@ fn zirBuiltinExtern( try sema.explainWhyTypeIsNotExtern(msg, ty_src.toSrcLoc(src_decl, mod), ty, .other); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const options = sema.resolveExternOptions(block, .unneeded, extra.rhs) catch |err| switch (err) { @@ -24870,7 +25041,9 @@ fn zirWorkItem( }, } - const dimension = @as(u32, @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, "dimension must be comptime-known"))); + const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{ + .needed_comptime_reason = "dimension must be comptime-known", + })); try sema.requireRuntimeBlock(block, builtin_src, null); return block.addInst(.{ @@ -24892,11 +25065,7 @@ fn zirInComptime( block: *Block, ) CompileError!Air.Inst.Ref { _ = sema; - if (block.is_comptime) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (block.is_comptime) .bool_true else .bool_false; } fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void { @@ -24913,7 +25082,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -24935,7 +25104,7 @@ fn validateVarType( try sema.explainWhyTypeIsNotExtern(msg, src.toSrcLoc(src_decl, mod), var_ty, .other); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } else { if (var_ty.zigTypeTag(mod) == .Opaque) { @@ -24962,7 +25131,7 @@ fn validateVarType( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const TypeSet = std.AutoHashMapUnmanaged(InternPool.Index, void); @@ -25412,7 +25581,7 @@ fn addSafetyCheckExtra( fail_block.instructions.items.len); try sema.air_instructions.ensureUnusedCapacity(gpa, 3); - const block_inst = @as(Air.Inst.Index, @intCast(sema.air_instructions.len)); + const block_inst: Air.Inst.Index = @intCast(sema.air_instructions.len); const cond_br_inst = block_inst + 1; const br_inst = cond_br_inst + 1; sema.air_instructions.appendAssumeCapacity(.{ @@ -25432,7 +25601,7 @@ fn addSafetyCheckExtra( .operand = ok, .payload = sema.addExtraAssumeCapacity(Air.CondBr{ .then_body_len = 1, - .else_body_len = @as(u32, @intCast(fail_block.instructions.items.len)), + .else_body_len = @intCast(fail_block.instructions.items.len), }), } }, }); @@ -25648,7 +25817,7 @@ fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void { "use @setEvalBranchQuota() to raise the branch limit from {d}", .{sema.branch_quota}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -25755,7 +25924,7 @@ fn fieldVal( try sema.addDeclaredHereNote(msg, child_type); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, .inferred_error_set_type => { return sema.fail(block, src, "TODO handle inferred error sets here", .{}); @@ -25785,7 +25954,7 @@ fn fieldVal( try sema.resolveTypeFields(child_type); if (child_type.unionTagType(mod)) |enum_ty| { if (enum_ty.enumFieldIndex(field_name, mod)) |field_index_usize| { - const field_index = @as(u32, @intCast(field_index_usize)); + const field_index: u32 = @intCast(field_index_usize); return Air.internedToRef((try mod.enumValueFieldIndex(enum_ty, field_index)).toIntern()); } } @@ -25799,7 +25968,7 @@ fn fieldVal( } const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); - const field_index = @as(u32, @intCast(field_index_usize)); + const field_index: u32 = @intCast(field_index_usize); const enum_val = try mod.enumValueFieldIndex(child_type, field_index); return Air.internedToRef(enum_val.toIntern()); }, @@ -25819,7 +25988,7 @@ fn fieldVal( if (child_type.zigTypeTag(mod) == .Array) try sema.errNote(block, src, msg, "array values have 'len' member", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } }, @@ -25956,7 +26125,7 @@ fn fieldPtr( } }, .Type => { - _ = try sema.resolveConstValue(block, .unneeded, object_ptr, ""); + _ = try sema.resolveConstValue(block, .unneeded, object_ptr, undefined); const result = try sema.analyzeLoad(block, src, object_ptr, object_ptr_src); const inner = if (is_pointer_to) try sema.analyzeLoad(block, src, result, object_ptr_src) @@ -26011,7 +26180,7 @@ fn fieldPtr( try sema.resolveTypeFields(child_type); if (child_type.unionTagType(mod)) |enum_ty| { if (enum_ty.enumFieldIndex(field_name, mod)) |field_index| { - const field_index_u32 = @as(u32, @intCast(field_index)); + const field_index_u32: u32 = @intCast(field_index); var anon_decl = try block.startAnonDecl(); defer anon_decl.deinit(); return sema.analyzeDeclRef(try anon_decl.finish( @@ -26032,7 +26201,7 @@ fn fieldPtr( const field_index = child_type.enumFieldIndex(field_name, mod) orelse { return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); }; - const field_index_u32 = @as(u32, @intCast(field_index)); + const field_index_u32: u32 = @intCast(field_index); var anon_decl = try block.startAnonDecl(); defer anon_decl.deinit(); return sema.analyzeDeclRef(try anon_decl.finish( @@ -26117,7 +26286,7 @@ fn fieldCallBind( if (mod.typeToStruct(concrete_ty)) |struct_obj| { const field_index_usize = struct_obj.fields.getIndex(field_name) orelse break :find_field; - const field_index = @as(u32, @intCast(field_index_usize)); + const field_index: u32 = @intCast(field_index_usize); const field = struct_obj.fields.values()[field_index]; return sema.finishFieldCallBind(block, src, ptr_ty, field.ty, field_index, object_ptr); @@ -26132,7 +26301,7 @@ fn fieldCallBind( } else { const max = concrete_ty.structFieldCount(mod); for (0..max) |i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); if (field_name == concrete_ty.structFieldName(i, mod)) { return sema.finishFieldCallBind(block, src, ptr_ty, concrete_ty.structFieldType(i, mod), i, object_ptr); } @@ -26238,7 +26407,7 @@ fn fieldCallBind( } break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn finishFieldCallBind( @@ -26302,7 +26471,7 @@ fn namespaceLookup( try mod.errNoteNonLazy(decl.srcLoc(mod), msg, "declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return decl_index; } @@ -26364,7 +26533,7 @@ fn structFieldPtr( const field_index_big = struct_obj.fields.getIndex(field_name) orelse return sema.failWithBadStructFieldAccess(block, struct_obj, field_name_src, field_name); - const field_index = @as(u32, @intCast(field_index_big)); + const field_index: u32 = @intCast(field_index_big); return sema.structFieldPtrByIndex(block, src, struct_ptr, field_index, field_name_src, struct_ty, initializing); } @@ -26413,7 +26582,7 @@ fn structFieldPtrByIndex( if (i == field_index) { ptr_ty_data.packed_offset.bit_offset = running_bits; } - running_bits += @as(u16, @intCast(f.ty.bitSize(mod))); + running_bits += @intCast(f.ty.bitSize(mod)); } ptr_ty_data.packed_offset.host_size = (running_bits + 7) / 8; @@ -26441,7 +26610,7 @@ fn structFieldPtrByIndex( const elem_size_bits = ptr_ty_data.child.toType().bitSize(mod); if (elem_size_bytes * 8 == elem_size_bits) { const byte_offset = ptr_ty_data.packed_offset.bit_offset / 8; - const new_align = @as(Alignment, @enumFromInt(@ctz(byte_offset | parent_align))); + const new_align: Alignment = @enumFromInt(@ctz(byte_offset | parent_align)); assert(new_align != .none); ptr_ty_data.flags.alignment = new_align; ptr_ty_data.packed_offset = .{ .host_size = 0, .bit_offset = 0 }; @@ -26505,7 +26674,7 @@ fn structFieldVal( const field_index_usize = struct_obj.fields.getIndex(field_name) orelse return sema.failWithBadStructFieldAccess(block, struct_obj, field_name_src, field_name); - const field_index = @as(u32, @intCast(field_index_usize)); + const field_index: u32 = @intCast(field_index_usize); const field = struct_obj.fields.values()[field_index]; if (field.is_comptime) { @@ -26660,7 +26829,7 @@ fn unionFieldPtr( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (try sema.resolveDefinedValue(block, src, union_ptr)) |union_ptr_val| ct: { @@ -26686,7 +26855,7 @@ fn unionFieldPtr( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } }, .Packed, .Extern => {}, @@ -26713,7 +26882,7 @@ fn unionFieldPtr( } if (field_ty.zigTypeTag(mod) == .NoReturn) { _ = try block.addNoOp(.unreach); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } return block.addStructFieldPtr(union_ptr, field_index, ptr_field_ty); } @@ -26758,7 +26927,7 @@ fn unionFieldVal( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } }, .Packed, .Extern => { @@ -26785,7 +26954,7 @@ fn unionFieldVal( } if (field_ty.zigTypeTag(mod) == .NoReturn) { _ = try block.addNoOp(.unreach); - return Air.Inst.Ref.unreachable_value; + return .unreachable_value; } return block.addStructFieldVal(union_byval, field_index, field_ty); } @@ -26814,8 +26983,10 @@ fn elemPtr( .Array, .Vector => return sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety), .Struct => { // Tuple field access. - const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, "tuple field access index must be comptime-known"); - const index = @as(u32, @intCast(index_val.toUnsignedInt(mod))); + const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, .{ + .needed_comptime_reason = "tuple field access index must be comptime-known", + }); + const index: u32 = @intCast(index_val.toUnsignedInt(mod)); return sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init); }, else => { @@ -26850,7 +27021,7 @@ fn elemPtrOneLayerOnly( const runtime_src = rs: { const ptr_val = maybe_ptr_val orelse break :rs indexable_src; const index_val = maybe_index_val orelse break :rs elem_index_src; - const index = @as(usize, @intCast(index_val.toUnsignedInt(mod))); + const index: usize = @intCast(index_val.toUnsignedInt(mod)); const result_ty = try sema.elemPtrType(indexable_ty, index); const elem_ptr = try ptr_val.elemPtr(result_ty, index, mod); return Air.internedToRef(elem_ptr.toIntern()); @@ -26868,8 +27039,10 @@ fn elemPtrOneLayerOnly( }, .Struct => { assert(child_ty.isTuple(mod)); - const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, "tuple field access index must be comptime-known"); - const index = @as(u32, @intCast(index_val.toUnsignedInt(mod))); + const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, .{ + .needed_comptime_reason = "tuple field access index must be comptime-known", + }); + const index: u32 = @intCast(index_val.toUnsignedInt(mod)); return sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false); }, else => unreachable, // Guaranteed by checkIndexable @@ -26907,7 +27080,7 @@ fn elemVal( const runtime_src = rs: { const indexable_val = maybe_indexable_val orelse break :rs indexable_src; const index_val = maybe_index_val orelse break :rs elem_index_src; - const index = @as(usize, @intCast(index_val.toUnsignedInt(mod))); + const index: usize = @intCast(index_val.toUnsignedInt(mod)); const elem_ty = indexable_ty.elemType2(mod); const many_ptr_ty = try mod.manyConstPtrType(elem_ty); const many_ptr_val = try mod.getCoerced(indexable_val, many_ptr_ty); @@ -26943,8 +27116,10 @@ fn elemVal( }, .Struct => { // Tuple field access. - const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, "tuple field access index must be comptime-known"); - const index = @as(u32, @intCast(index_val.toUnsignedInt(mod))); + const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index, .{ + .needed_comptime_reason = "tuple field access index must be comptime-known", + }); + const index: u32 = @intCast(index_val.toUnsignedInt(mod)); return sema.tupleField(block, indexable_src, indexable, elem_index_src, index); }, else => unreachable, @@ -26975,7 +27150,7 @@ fn validateRuntimeElemAccess( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -27105,7 +27280,7 @@ fn elemValArray( const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); if (maybe_index_val) |index_val| { - const index = @as(usize, @intCast(index_val.toUnsignedInt(mod))); + const index: usize = @intCast(index_val.toUnsignedInt(mod)); if (array_sent) |s| { if (index == array_len) { return Air.internedToRef(s.toIntern()); @@ -27121,7 +27296,7 @@ fn elemValArray( return mod.undefRef(elem_ty); } if (maybe_index_val) |index_val| { - const index = @as(usize, @intCast(index_val.toUnsignedInt(mod))); + const index: usize = @intCast(index_val.toUnsignedInt(mod)); const elem_val = try array_val.elemValue(mod, index); return Air.internedToRef(elem_val.toIntern()); } @@ -27234,7 +27409,7 @@ fn elemValSlice( return sema.fail(block, slice_src, "indexing into empty slice is not allowed", .{}); } if (maybe_index_val) |index_val| { - const index = @as(usize, @intCast(index_val.toUnsignedInt(mod))); + const index: usize = @intCast(index_val.toUnsignedInt(mod)); if (index >= slice_len_s) { const sentinel_label: []const u8 = if (slice_sent) " +1 (sentinel)" else ""; return sema.fail(block, elem_index_src, "index {d} outside slice of length {d}{s}", .{ index, slice_len, sentinel_label }); @@ -27457,7 +27632,7 @@ fn coerceExtra( // Function body to function pointer. if (inst_ty.zigTypeTag(mod) == .Fn) { - const fn_val = try sema.resolveConstValue(block, .unneeded, inst, ""); + const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined); const fn_decl = fn_val.pointerDecl(mod).?; const inst_as_ptr = try sema.analyzeDeclRef(fn_decl); return sema.coerce(block, dest_ty, inst_as_ptr, inst_src); @@ -27706,7 +27881,7 @@ fn coerceExtra( try sema.errNote(block, dest_ty_src, err_msg, "pointers to tuples can only coerce to constant pointers", .{}); break :err_msg err_msg; }; - return sema.failWithOwnedErrorMsg(err_msg); + return sema.failWithOwnedErrorMsg(block, err_msg); } return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src); }, @@ -27746,7 +27921,9 @@ fn coerceExtra( const val = (try sema.resolveMaybeUndefVal(inst)) orelse { if (dest_ty.zigTypeTag(mod) == .ComptimeInt) { if (!opts.report_err) return error.NotCoercible; - return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime-known"); + return sema.failWithNeededComptime(block, inst_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known", + }); } break :float; }; @@ -27777,7 +27954,9 @@ fn coerceExtra( if (dest_ty.zigTypeTag(mod) == .ComptimeInt) { if (!opts.report_err) return error.NotCoercible; if (opts.no_cast_to_comptime_int) return inst; - return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_int' must be comptime-known"); + return sema.failWithNeededComptime(block, inst_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known", + }); } // integer widening @@ -27798,7 +27977,7 @@ fn coerceExtra( }, .Float, .ComptimeFloat => switch (inst_ty.zigTypeTag(mod)) { .ComptimeFloat => { - const val = try sema.resolveConstValue(block, .unneeded, inst, ""); + const val = try sema.resolveConstValue(block, .unneeded, inst, undefined); const result_val = try val.floatCast(dest_ty, mod); return Air.internedToRef(result_val.toIntern()); }, @@ -27819,7 +27998,9 @@ fn coerceExtra( return Air.internedToRef(result_val.toIntern()); } else if (dest_ty.zigTypeTag(mod) == .ComptimeFloat) { if (!opts.report_err) return error.NotCoercible; - return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime-known"); + return sema.failWithNeededComptime(block, inst_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known", + }); } // float widening @@ -27837,7 +28018,9 @@ fn coerceExtra( const val = (try sema.resolveMaybeUndefVal(inst)) orelse { if (dest_ty.zigTypeTag(mod) == .ComptimeFloat) { if (!opts.report_err) return error.NotCoercible; - return sema.failWithNeededComptime(block, inst_src, "value being casted to 'comptime_float' must be comptime-known"); + return sema.failWithNeededComptime(block, inst_src, .{ + .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known", + }); } break :int; }; @@ -27862,7 +28045,7 @@ fn coerceExtra( .Enum => switch (inst_ty.zigTypeTag(mod)) { .EnumLiteral => { // enum literal to enum - const val = try sema.resolveConstValue(block, .unneeded, inst, ""); + const val = try sema.resolveConstValue(block, .unneeded, inst, undefined); const string = mod.intern_pool.indexToKey(val.toIntern()).enum_literal; const field_index = dest_ty.enumFieldIndex(string, mod) orelse { const msg = msg: { @@ -27876,9 +28059,9 @@ fn coerceExtra( try sema.addDeclaredHereNote(msg, dest_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; - return Air.internedToRef((try mod.enumValueFieldIndex(dest_ty, @as(u32, @intCast(field_index)))).toIntern()); + return Air.internedToRef((try mod.enumValueFieldIndex(dest_ty, @intCast(field_index))).toIntern()); }, .Union => blk: { // union to its own tag type @@ -28007,7 +28190,7 @@ fn coerceExtra( try mod.errNoteNonLazy(ret_ty_src.toSrcLoc(src_decl, mod), msg, "'noreturn' declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const msg = msg: { @@ -28053,7 +28236,7 @@ fn coerceExtra( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn coerceInMemory( @@ -28282,8 +28465,8 @@ const InMemoryCoercionResult = union(enum) { var index: u6 = 0; var actual_noalias = false; while (true) : (index += 1) { - const actual = @as(u1, @truncate(param.actual >> index)); - const wanted = @as(u1, @truncate(param.wanted >> index)); + const actual: u1 = @truncate(param.actual >> index); + const wanted: u1 = @truncate(param.wanted >> index); if (actual != wanted) { actual_noalias = actual == 1; break; @@ -28998,7 +29181,7 @@ fn coerceVarArgParam( .{}, ), .Fn => blk: { - const fn_val = try sema.resolveConstValue(block, .unneeded, inst, ""); + const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined); const fn_decl = fn_val.pointerDecl(mod).?; break :blk try sema.analyzeDeclRef(fn_decl); }, @@ -29029,7 +29212,7 @@ fn coerceVarArgParam( try sema.addDeclaredHereNote(msg, coerced_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } return coerced; } @@ -29446,7 +29629,7 @@ fn beginComptimePtrMutation( // bytes.len may be one greater than dest_len because of the case when // assigning `[N:S]T` to `[N]T`. This is allowed; the sentinel is omitted. assert(bytes.len >= dest_len); - const elems = try arena.alloc(Value, @as(usize, @intCast(dest_len))); + const elems = try arena.alloc(Value, @intCast(dest_len)); for (elems, 0..) |*elem, i| { elem.* = try mod.intValue(elem_ty, bytes[i]); } @@ -29458,7 +29641,7 @@ fn beginComptimePtrMutation( block, src, elem_ty, - &elems[@as(usize, @intCast(elem_ptr.index))], + &elems[@intCast(elem_ptr.index)], ptr_elem_ty, parent.mut_decl, ); @@ -29486,7 +29669,7 @@ fn beginComptimePtrMutation( block, src, elem_ty, - &elems[@as(usize, @intCast(elem_ptr.index))], + &elems[@intCast(elem_ptr.index)], ptr_elem_ty, parent.mut_decl, ); @@ -29497,7 +29680,7 @@ fn beginComptimePtrMutation( block, src, elem_ty, - &val_ptr.castTag(.aggregate).?.data[@as(usize, @intCast(elem_ptr.index))], + &val_ptr.castTag(.aggregate).?.data[@intCast(elem_ptr.index)], ptr_elem_ty, parent.mut_decl, ), @@ -29523,7 +29706,7 @@ fn beginComptimePtrMutation( block, src, elem_ty, - &elems[@as(usize, @intCast(elem_ptr.index))], + &elems[@intCast(elem_ptr.index)], ptr_elem_ty, parent.mut_decl, ); @@ -29578,7 +29761,7 @@ fn beginComptimePtrMutation( }, .field => |field_ptr| { const base_child_ty = mod.intern_pool.typeOf(field_ptr.base).toType().childType(mod); - const field_index = @as(u32, @intCast(field_ptr.index)); + const field_index: u32 = @intCast(field_ptr.index); var parent = try sema.beginComptimePtrMutation(block, src, field_ptr.base.toValue(), base_child_ty); switch (parent.pointee) { @@ -30015,12 +30198,12 @@ fn beginComptimePtrLoad( } deref.pointee = TypedValue{ .ty = elem_ty, - .val = try array_tv.val.elemValue(mod, @as(usize, @intCast(elem_ptr.index))), + .val = try array_tv.val.elemValue(mod, @intCast(elem_ptr.index)), }; break :blk deref; }, .field => |field_ptr| blk: { - const field_index = @as(u32, @intCast(field_ptr.index)); + const field_index: u32 = @intCast(field_ptr.index); const container_ty = mod.intern_pool.typeOf(field_ptr.base).toType().childType(mod); var deref = try sema.beginComptimePtrLoad(block, src, field_ptr.base.toValue(), container_ty); @@ -30284,7 +30467,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; const enum_tag = try sema.coerce(block, tag_ty, inst, inst_src); @@ -30298,7 +30481,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; const union_obj = mod.typeToUnion(union_ty).?; @@ -30316,7 +30499,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const opv = (try sema.typeHasOnePossibleValue(field_ty)) orelse { const msg = msg: { @@ -30333,7 +30516,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }; return Air.internedToRef((try mod.unionValue(union_ty, val, opv)).toIntern()); @@ -30350,7 +30533,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, tag_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const union_obj = mod.typeToUnion(union_ty).?; @@ -30374,7 +30557,7 @@ fn coerceEnumToUnion( if (msg) |some| { msg = null; try sema.addDeclaredHereNote(some, union_ty); - return sema.failWithOwnedErrorMsg(some); + return sema.failWithOwnedErrorMsg(block, some); } } @@ -30404,7 +30587,7 @@ fn coerceEnumToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn coerceAnonStructToUnion( @@ -30461,7 +30644,7 @@ fn coerceAnonStructToUnion( try sema.addDeclaredHereNote(msg, union_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } } @@ -30533,7 +30716,7 @@ fn coerceArrayLike( try sema.errNote(block, inst_src, msg, "source has length {d}", .{inst_len}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const dest_elem_ty = dest_ty.childType(mod); @@ -30592,7 +30775,7 @@ fn coerceTupleToArray( try sema.errNote(block, inst_src, msg, "source has length {d}", .{inst_len}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } const dest_elems = try sema.usizeCast(block, dest_ty_src, dest_len); @@ -30602,7 +30785,7 @@ fn coerceTupleToArray( var runtime_src: ?LazySrcLoc = null; for (element_vals, element_refs, 0..) |*val, *ref, i_usize| { - const i = @as(u32, @intCast(i_usize)); + const i: u32 = @intCast(i_usize); if (i_usize == inst_len) { const sentinel_val = dest_ty.sentinel(mod).?; val.* = sentinel_val.toIntern(); @@ -30713,7 +30896,7 @@ fn coerceTupleToStruct( else => unreachable, }; for (0..field_count) |field_index_usize| { - const field_i = @as(u32, @intCast(field_index_usize)); + const field_i: u32 = @intCast(field_index_usize); const field_src = inst_src; // TODO better source location // https://github.com/ziglang/zig/issues/15709 const field_name: InternPool.NullTerminatedString = switch (ip.indexToKey(inst_ty.toIntern())) { @@ -30731,7 +30914,9 @@ fn coerceTupleToStruct( field_refs[field_index] = coerced; if (field.is_comptime) { const init_val = (try sema.resolveMaybeUndefVal(coerced)) orelse { - return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime-known"); + return sema.failWithNeededComptime(block, field_src, .{ + .needed_comptime_reason = "value stored in comptime field must be comptime-known", + }); }; if (!init_val.eql(field.default_val.toValue(), field.ty, sema.mod)) { @@ -30777,7 +30962,7 @@ fn coerceTupleToStruct( if (root_msg) |msg| { try sema.addDeclaredHereNote(msg, struct_ty); root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (runtime_src) |rs| { @@ -30829,7 +31014,7 @@ fn coerceTupleToTuple( var runtime_src: ?LazySrcLoc = null; for (0..dest_field_count) |field_index_usize| { - const field_i = @as(u32, @intCast(field_index_usize)); + const field_i: u32 = @intCast(field_index_usize); const field_src = inst_src; // TODO better source location // https://github.com/ziglang/zig/issues/15709 const field_name: InternPool.NullTerminatedString = switch (ip.indexToKey(inst_ty.toIntern())) { @@ -30862,7 +31047,9 @@ fn coerceTupleToTuple( field_refs[field_index] = coerced; if (default_val != .none) { const init_val = (try sema.resolveMaybeUndefVal(coerced)) orelse { - return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime-known"); + return sema.failWithNeededComptime(block, field_src, .{ + .needed_comptime_reason = "value stored in comptime field must be comptime-known", + }); }; if (!init_val.eql(default_val.toValue(), field_ty, sema.mod)) { @@ -30921,7 +31108,7 @@ fn coerceTupleToTuple( if (root_msg) |msg| { try sema.addDeclaredHereNote(msg, tuple_ty); root_msg = null; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } if (runtime_src) |rs| { @@ -30982,7 +31169,7 @@ fn ensureDeclAnalyzed(sema: *Sema, decl_index: Decl.Index) CompileError!void { const decl = mod.declPtr(decl_index); if (decl.analysis == .in_progress) { const msg = try Module.ErrorMsg.create(sema.gpa, decl.srcLoc(mod), "dependency loop detected", .{}); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); } mod.ensureDeclAnalyzed(decl_index) catch |err| { @@ -31217,14 +31404,10 @@ fn analyzeIsNull( } const is_null = opt_val.isNull(mod); const bool_value = if (invert_logic) !is_null else is_null; - if (bool_value) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + return if (bool_value) .bool_true else .bool_false; } - const inverted_non_null_res = if (invert_logic) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false; + const inverted_non_null_res: Air.Inst.Ref = if (invert_logic) .bool_true else .bool_false; const operand_ty = sema.typeOf(operand); if (operand_ty.zigTypeTag(mod) == .Optional and operand_ty.optionalChild(mod).zigTypeTag(mod) == .NoReturn) { return inverted_non_null_res; @@ -31249,14 +31432,14 @@ fn analyzePtrIsNonErrComptimeOnly( const child_ty = ptr_ty.childType(mod); const child_tag = child_ty.zigTypeTag(mod); - if (child_tag != .ErrorSet and child_tag != .ErrorUnion) return Air.Inst.Ref.bool_true; - if (child_tag == .ErrorSet) return Air.Inst.Ref.bool_false; + if (child_tag != .ErrorSet and child_tag != .ErrorUnion) return .bool_true; + if (child_tag == .ErrorSet) return .bool_false; assert(child_tag == .ErrorUnion); _ = block; _ = src; - return Air.Inst.Ref.none; + return .none; } fn analyzeIsNonErrComptimeOnly( @@ -31606,7 +31789,9 @@ fn analyzeSlice( const sentinel = s: { if (sentinel_opt != .none) { const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src); - break :s try sema.resolveConstValue(block, sentinel_src, casted, "slice sentinel must be comptime-known"); + break :s try sema.resolveConstValue(block, sentinel_src, casted, .{ + .needed_comptime_reason = "slice sentinel must be comptime-known", + }); } // If we are slicing to the end of something that is sentinel-terminated // then the resulting slice type is also sentinel-terminated. @@ -31676,7 +31861,7 @@ fn analyzeSlice( break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } else { runtime_src = ptr_src; @@ -31878,11 +32063,11 @@ fn cmpNumeric( // Compare ints: const vs. undefined (or vice versa) if (!lhs_val.isUndef(mod) and (lhs_ty.isInt(mod) or lhs_ty_tag == .ComptimeInt) and rhs_ty.isInt(mod) and rhs_val.isUndef(mod)) { if (try sema.compareIntsOnlyPossibleResult(try sema.resolveLazyValue(lhs_val), op, rhs_ty)) |res| { - return if (res) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false; + return if (res) .bool_true else .bool_false; } } else if (!rhs_val.isUndef(mod) and (rhs_ty.isInt(mod) or rhs_ty_tag == .ComptimeInt) and lhs_ty.isInt(mod) and lhs_val.isUndef(mod)) { if (try sema.compareIntsOnlyPossibleResult(try sema.resolveLazyValue(rhs_val), op.reverse(), lhs_ty)) |res| { - return if (res) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false; + return if (res) .bool_true else .bool_false; } } @@ -31890,22 +32075,17 @@ fn cmpNumeric( return mod.undefRef(Type.bool); } if (lhs_val.isNan(mod) or rhs_val.isNan(mod)) { - if (op == std.math.CompareOperator.neq) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } - } - if (try Value.compareHeteroAdvanced(lhs_val, op, rhs_val, mod, sema)) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; + return if (op == std.math.CompareOperator.neq) .bool_true else .bool_false; } + return if (try Value.compareHeteroAdvanced(lhs_val, op, rhs_val, mod, sema)) + .bool_true + else + .bool_false; } else { if (!lhs_val.isUndef(mod) and (lhs_ty.isInt(mod) or lhs_ty_tag == .ComptimeInt) and rhs_ty.isInt(mod)) { // Compare ints: const vs. var if (try sema.compareIntsOnlyPossibleResult(try sema.resolveLazyValue(lhs_val), op, rhs_ty)) |res| { - return if (res) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false; + return if (res) .bool_true else .bool_false; } } break :src rhs_src; @@ -31915,7 +32095,7 @@ fn cmpNumeric( if (!rhs_val.isUndef(mod) and (rhs_ty.isInt(mod) or rhs_ty_tag == .ComptimeInt) and lhs_ty.isInt(mod)) { // Compare ints: var vs. const if (try sema.compareIntsOnlyPossibleResult(try sema.resolveLazyValue(rhs_val), op.reverse(), lhs_ty)) |res| { - return if (res) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false; + return if (res) .bool_true else .bool_false; } } } @@ -31982,34 +32162,34 @@ fn cmpNumeric( if (lhs_val.isUndef(mod)) return mod.undefRef(Type.bool); if (lhs_val.isNan(mod)) switch (op) { - .neq => return Air.Inst.Ref.bool_true, - else => return Air.Inst.Ref.bool_false, + .neq => return .bool_true, + else => return .bool_false, }; if (lhs_val.isInf(mod)) switch (op) { - .neq => return Air.Inst.Ref.bool_true, - .eq => return Air.Inst.Ref.bool_false, - .gt, .gte => return if (lhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true, - .lt, .lte => return if (lhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false, + .neq => return .bool_true, + .eq => return .bool_false, + .gt, .gte => return if (lhs_val.isNegativeInf(mod)) .bool_false else .bool_true, + .lt, .lte => return if (lhs_val.isNegativeInf(mod)) .bool_true else .bool_false, }; if (!rhs_is_signed) { switch (lhs_val.orderAgainstZero(mod)) { .gt => {}, .eq => switch (op) { // LHS = 0, RHS is unsigned - .lte => return Air.Inst.Ref.bool_true, - .gt => return Air.Inst.Ref.bool_false, + .lte => return .bool_true, + .gt => return .bool_false, else => {}, }, .lt => switch (op) { // LHS < 0, RHS is unsigned - .neq, .lt, .lte => return Air.Inst.Ref.bool_true, - .eq, .gt, .gte => return Air.Inst.Ref.bool_false, + .neq, .lt, .lte => return .bool_true, + .eq, .gt, .gte => return .bool_false, }, } } if (lhs_is_float) { if (lhs_val.floatHasFraction(mod)) { switch (op) { - .eq => return Air.Inst.Ref.bool_false, - .neq => return Air.Inst.Ref.bool_true, + .eq => return .bool_false, + .neq => return .bool_true, else => {}, } } @@ -32040,34 +32220,34 @@ fn cmpNumeric( if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool); if (rhs_val.isNan(mod)) switch (op) { - .neq => return Air.Inst.Ref.bool_true, - else => return Air.Inst.Ref.bool_false, + .neq => return .bool_true, + else => return .bool_false, }; if (rhs_val.isInf(mod)) switch (op) { - .neq => return Air.Inst.Ref.bool_true, - .eq => return Air.Inst.Ref.bool_false, - .gt, .gte => return if (rhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_true else Air.Inst.Ref.bool_false, - .lt, .lte => return if (rhs_val.isNegativeInf(mod)) Air.Inst.Ref.bool_false else Air.Inst.Ref.bool_true, + .neq => return .bool_true, + .eq => return .bool_false, + .gt, .gte => return if (rhs_val.isNegativeInf(mod)) .bool_true else .bool_false, + .lt, .lte => return if (rhs_val.isNegativeInf(mod)) .bool_false else .bool_true, }; if (!lhs_is_signed) { switch (rhs_val.orderAgainstZero(mod)) { .gt => {}, .eq => switch (op) { // RHS = 0, LHS is unsigned - .gte => return Air.Inst.Ref.bool_true, - .lt => return Air.Inst.Ref.bool_false, + .gte => return .bool_true, + .lt => return .bool_false, else => {}, }, .lt => switch (op) { // RHS < 0, LHS is unsigned - .neq, .gt, .gte => return Air.Inst.Ref.bool_true, - .eq, .lt, .lte => return Air.Inst.Ref.bool_false, + .neq, .gt, .gte => return .bool_true, + .eq, .lt, .lte => return .bool_false, }, } } if (rhs_is_float) { if (rhs_val.floatHasFraction(mod)) { switch (op) { - .eq => return Air.Inst.Ref.bool_false, - .neq => return Air.Inst.Ref.bool_true, + .eq => return .bool_false, + .neq => return .bool_true, else => {}, } } @@ -32185,7 +32365,7 @@ fn compareIntsOnlyPossibleResult( const ty = try mod.intType( if (is_negative) .signed else .unsigned, - @as(u16, @intCast(req_bits)), + @intCast(req_bits), ); const pop_count = lhs_val.popCount(ty, mod); @@ -32687,7 +32867,7 @@ fn resolvePeerTypes( .success => |ty| return ty, else => |result| { const msg = try result.report(sema, block, src, instructions, candidate_srcs); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); }, } } @@ -32960,7 +33140,7 @@ fn resolvePeerTypesInner( }; return .{ .success = try mod.vectorType(.{ - .len = @as(u32, @intCast(len.?)), + .len = @intCast(len.?), .child = child_ty.toIntern(), }) }; }, @@ -34045,7 +34225,7 @@ fn resolveStructLayout(sema: *Sema, ty: Type) CompileError!void { "struct '{}' depends on itself", .{ty.fmt(mod)}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); }, .have_layout, .fully_resolved_wip, .fully_resolved => return, } @@ -34080,7 +34260,7 @@ fn resolveStructLayout(sema: *Sema, ty: Type) CompileError!void { "struct layout depends on it having runtime bits", .{}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); } if (struct_obj.layout == .Auto and !struct_obj.is_tuple and @@ -34090,7 +34270,7 @@ fn resolveStructLayout(sema: *Sema, ty: Type) CompileError!void { for (struct_obj.fields.values(), 0..) |field, i| { optimized_order[i] = if (try sema.typeHasRuntimeBits(field.ty)) - @as(u32, @intCast(i)) + @intCast(i) else Module.Struct.omitted_field; } @@ -34131,7 +34311,7 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi const zir = mod.namespacePtr(struct_obj.namespace).file_scope.zir; const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended; assert(extended.opcode == .struct_decl); - const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); if (small.has_backing_int) { var extra_index: usize = extended.operand; @@ -34182,7 +34362,7 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi const backing_int_src: LazySrcLoc = .{ .node_offset_container_tag = 0 }; const backing_int_ty = blk: { if (backing_int_body_len == 0) { - const backing_int_ref = @as(Zir.Inst.Ref, @enumFromInt(zir.extra[extra_index])); + const backing_int_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]); break :blk try sema.resolveType(&block, backing_int_src, backing_int_ref); } else { const body = zir.extra[extra_index..][0..backing_int_body_len]; @@ -34228,7 +34408,7 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi }; return sema.fail(&block, LazySrcLoc.nodeOffset(0), "size of packed struct '{d}' exceeds maximum bit width of 65535", .{fields_bit_sum}); } - struct_obj.backing_int_ty = try mod.intType(.unsigned, @as(u16, @intCast(fields_bit_sum))); + struct_obj.backing_int_ty = try mod.intType(.unsigned, @intCast(fields_bit_sum)); } } @@ -34257,7 +34437,7 @@ fn checkIndexable(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { try sema.errNote(block, src, msg, "operand must be an array, slice, tuple, or vector", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } } @@ -34280,7 +34460,7 @@ fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void try sema.errNote(block, src, msg, "operand must be a slice, a many pointer or a pointer to an array", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void { @@ -34297,7 +34477,7 @@ fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void { "union '{}' depends on itself", .{ty.fmt(mod)}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); }, .have_layout, .fully_resolved_wip, .fully_resolved => return, } @@ -34328,7 +34508,7 @@ fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void { "union layout depends on it having runtime bits", .{}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); } } @@ -34586,7 +34766,7 @@ fn resolveTypeFieldsStruct( "struct '{}' depends on itself", .{ty.fmt(sema.mod)}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); }, .have_field_types, .have_layout, @@ -34626,7 +34806,7 @@ fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Key.Unio "union '{}' depends on itself", .{ty.fmt(mod)}, ); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); }, .have_field_types, .have_layout, @@ -34680,7 +34860,7 @@ fn resolveInferredErrorSet( try sema.mod.errNoteNonLazy(ies_func_owner_decl.srcLoc(mod), msg, "generic function declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(block, msg); } // In this case we are dealing with the actual InferredErrorSet object that // corresponds to the function, not one created to track an inline/comptime call. @@ -34789,7 +34969,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void const zir = mod.namespacePtr(struct_obj.namespace).file_scope.zir; const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended; assert(extended.opcode == .struct_decl); - const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small)); + const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); var extra_index: usize = extended.operand; const src = LazySrcLoc.nodeOffset(0); @@ -34917,7 +35097,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void if (has_type_body) { fields[field_i].type_body_len = zir.extra[extra_index]; } else { - fields[field_i].type_ref = @as(Zir.Inst.Ref, @enumFromInt(zir.extra[extra_index])); + fields[field_i].type_ref = @enumFromInt(zir.extra[extra_index]); } extra_index += 1; @@ -34940,7 +35120,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void try sema.errNote(&block_scope, src, msg, "struct declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } gop.value_ptr.* = .{ .ty = Type.noreturn, @@ -35016,7 +35196,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } if (field_ty.zigTypeTag(mod) == .NoReturn) { const msg = msg: { @@ -35030,7 +35210,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } if (struct_obj.layout == .Extern and !try sema.validateExternType(field.ty, .struct_field)) { const msg = msg: { @@ -35046,7 +35226,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void try sema.addDeclaredHereNote(msg, field.ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } else if (struct_obj.layout == .Packed and !(validatePackedType(field.ty, mod))) { const msg = msg: { const ty_src = mod.fieldSrcLoc(struct_obj.owner_decl, .{ @@ -35061,7 +35241,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void try sema.addDeclaredHereNote(msg, field.ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } if (zir_field.align_body_len > 0) { @@ -35112,7 +35292,9 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void .index = field_i, .range = .value, }).lazy; - return sema.failWithNeededComptime(&block_scope, init_src, "struct field default value must be comptime-known"); + return sema.failWithNeededComptime(&block_scope, init_src, .{ + .needed_comptime_reason = "struct field default value must be comptime-known", + }); }; field.default_val = try default_val.intern(field.ty, mod); } @@ -35247,7 +35429,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un }); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } enum_field_names = try sema.arena.alloc(InternPool.NullTerminatedString, fields_len); try enum_field_vals.ensureTotalCapacity(sema.arena, fields_len); @@ -35362,7 +35544,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.errNote(&block_scope, other_field_src, msg, "other occurrence here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } } @@ -35407,7 +35589,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.errNote(&block_scope, src, msg, "union declared here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } if (explicit_tags_seen.len > 0) { @@ -35425,7 +35607,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.addDeclaredHereNote(msg, union_type.tagTypePtr(ip).toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); }; // No check for duplicate because the check already happened in order // to create the enum type in the first place. @@ -35447,7 +35629,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.errNote(&block_scope, enum_field_src, msg, "enum field here", .{}); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } } @@ -35463,7 +35645,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } const layout = union_type.getLayout(ip); if (layout == .Extern and @@ -35482,7 +35664,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } else if (layout == .Packed and !validatePackedType(field_ty, mod)) { const msg = msg: { const ty_src = mod.fieldSrcLoc(union_type.decl, .{ @@ -35497,7 +35679,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.addDeclaredHereNote(msg, field_ty); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } field_types.appendAssumeCapacity(field_ty.toIntern()); @@ -35541,7 +35723,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un try sema.addDeclaredHereNote(msg, union_type.tagTypePtr(ip).toType()); break :msg msg; }; - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(&block_scope, msg); } } else if (enum_field_vals.count() > 0) { const enum_ty = try sema.generateUnionTagTypeNumbered(&block_scope, enum_field_names, enum_field_vals.keys(), mod.declPtr(union_type.decl)); @@ -35554,7 +35736,9 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un fn semaUnionFieldVal(sema: *Sema, block: *Block, src: LazySrcLoc, int_tag_ty: Type, tag_ref: Air.Inst.Ref) CompileError!Value { const coerced = try sema.coerce(block, int_tag_ty, tag_ref, src); - return sema.resolveConstValue(block, src, coerced, "enum tag value must be comptime-known"); + return sema.resolveConstValue(block, src, coerced, .{ + .needed_comptime_reason = "enum tag value must be comptime-known", + }); } fn generateUnionTagTypeNumbered( @@ -35950,7 +36134,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { .{ty.fmt(mod)}, ); try sema.addFieldErrNote(ty, i, msg, "while checking this field", .{}); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); } if (try sema.typeHasOnePossibleValue(field.ty)) |field_opv| { field_val.* = try field_opv.intern(field.ty, mod); @@ -36004,7 +36188,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { .{ty.fmt(mod)}, ); try sema.addFieldErrNote(ty, 0, msg, "while checking this field", .{}); - return sema.failWithOwnedErrorMsg(msg); + return sema.failWithOwnedErrorMsg(null, msg); } const val_val = (try sema.typeHasOnePossibleValue(only_field_ty)) orelse return null; @@ -36076,13 +36260,12 @@ pub fn addExtra(sema: *Sema, extra: anytype) Allocator.Error!u32 { pub fn addExtraAssumeCapacity(sema: *Sema, extra: anytype) u32 { const fields = std.meta.fields(@TypeOf(extra)); - const result = @as(u32, @intCast(sema.air_extra.items.len)); + const result: u32 = @intCast(sema.air_extra.items.len); inline for (fields) |field| { sema.air_extra.appendAssumeCapacity(switch (field.type) { u32 => @field(extra, field.name), - Air.Inst.Ref => @intFromEnum(@field(extra, field.name)), - i32 => @as(u32, @bitCast(@field(extra, field.name))), - InternPool.Index => @intFromEnum(@field(extra, field.name)), + i32 => @bitCast(@field(extra, field.name)), + Air.Inst.Ref, InternPool.Index => @intFromEnum(@field(extra, field.name)), else => @compileError("bad field type: " ++ @typeName(field.type)), }); } @@ -36090,8 +36273,7 @@ pub fn addExtraAssumeCapacity(sema: *Sema, extra: anytype) u32 { } fn appendRefsAssumeCapacity(sema: *Sema, refs: []const Air.Inst.Ref) void { - const coerced = @as([]const u32, @ptrCast(refs)); - sema.air_extra.appendSliceAssumeCapacity(coerced); + sema.air_extra.appendSliceAssumeCapacity(@ptrCast(refs)); } fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index { @@ -36180,7 +36362,9 @@ pub fn analyzeAddressSpace( ctx: AddressSpaceContext, ) !std.builtin.AddressSpace { const mod = sema.mod; - const addrspace_tv = try sema.resolveInstConst(block, src, zir_ref, "address space must be comptime-known"); + const addrspace_tv = try sema.resolveInstConst(block, src, zir_ref, .{ + .needed_comptime_reason = "address space must be comptime-known", + }); const address_space = mod.toEnum(std.builtin.AddressSpace, addrspace_tv.val); const target = sema.mod.getTarget(); const arch = target.cpu.arch; @@ -36547,7 +36731,7 @@ fn structFieldAlignment(sema: *Sema, field: Module.Struct.Field, layout: std.bui const mod = sema.mod; if (field.abi_align.toByteUnitsOptional()) |a| { assert(layout != .Packed); - return @as(u32, @intCast(a)); + return @intCast(a); } switch (layout) { .Packed => return 0, @@ -36612,7 +36796,7 @@ fn structFieldIndex( const struct_obj = mod.typeToStruct(struct_ty).?; const field_index_usize = struct_obj.fields.getIndex(field_name) orelse return sema.failWithBadStructFieldAccess(block, struct_obj, field_src, field_name); - return @as(u32, @intCast(field_index_usize)); + return @intCast(field_index_usize); } } @@ -36626,12 +36810,12 @@ fn anonStructFieldIndex( const mod = sema.mod; switch (mod.intern_pool.indexToKey(struct_ty.toIntern())) { .anon_struct_type => |anon_struct_type| for (anon_struct_type.names, 0..) |name, i| { - if (name == field_name) return @as(u32, @intCast(i)); + if (name == field_name) return @intCast(i); }, .struct_type => |struct_type| if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| { for (struct_obj.fields.keys(), 0..) |name, i| { if (name == field_name) { - return @as(u32, @intCast(i)); + return @intCast(i); } } }, @@ -37229,9 +37413,9 @@ fn elemPtrType(sema: *Sema, ptr_ty: Type, offset: ?usize) !Type { if (!is_packed) break :blk .{}; break :blk .{ - .host_size = @as(u16, @intCast(parent_ty.arrayLen(mod))), - .alignment = @as(u32, @intCast(parent_ty.abiAlignment(mod))), - .vector_index = if (offset) |some| @as(VI, @enumFromInt(some)) else .runtime, + .host_size = @intCast(parent_ty.arrayLen(mod)), + .alignment = @intCast(parent_ty.abiAlignment(mod)), + .vector_index = if (offset) |some| @enumFromInt(some) else .runtime, }; } else .{}; @@ -37250,10 +37434,10 @@ fn elemPtrType(sema: *Sema, ptr_ty: Type, offset: ?usize) !Type { // The resulting pointer is aligned to the lcd between the offset (an // arbitrary number) and the alignment factor (always a power of two, // non zero). - const new_align = @as(Alignment, @enumFromInt(@min( + const new_align: Alignment = @enumFromInt(@min( @ctz(addend), @intFromEnum(ptr_info.flags.alignment), - ))); + )); assert(new_align != .none); break :a new_align; }; |
