@@ -10924,50 +10924,51 @@ const SwitchProngAnalysis = struct {
// By-reference captures have some further restrictions which make them easier to emit
if (capture_byref) {
const operand_ptr_info = operand_ptr_ty.ptrInfo(mod);
const capture_ptr_ty = try sema.ptrType(.{
.child = capture_ty.toIntern(),
.flags = .{
// TODO: alignment!
.is_const = operand_ptr_info.flags.is_const,
.is_volatile = operand_ptr_info.flags.is_volatile,
.address_space = operand_ptr_info.flags.address_space,
},
});
// By-ref captures of hetereogeneous types are only allowed if each field
// pointer type is in-memory coercible to the capture pointer type.
if (!same_types) {
for (field_indices, 0..) |field_idx, i| {
const capture_ptr_ty = resolve: {
// By-ref captures of hetereogeneous types are only allowed if all field
// pointer types are peer resolvable to each other.
// We need values to run PTR on, so make a bunch of undef constants.
const dummy_captures = try sema.arena.alloc(Air.Inst.Ref, case_vals.len);
for (field_indices, dummy_captures) |field_idx, *dummy| {
const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_idx]);
const field_ptr_ty = try sema.ptrType(.{
.child = field_ty.toIntern(),
.flags = .{
// TODO: alignment!
.is_const = operand_ptr_info.flags.is_const,
.is_volatile = operand_ptr_info.flags.is_volatile,
.address_space = operand_ptr_info.flags.address_space,
.alignment = union_obj.fieldAlign(ip, field_idx),
},
});
if (.ok != try sema.coerceInMemoryAllowed(block, capture_ptr_ty, field_ptr_ty, false, sema.mod.getTarget(), .unneeded, .unneeded)) {
dummy.* = try mod.undefRef(field_ptr_ty);
}
const case_srcs = try sema.arena.alloc(?LazySrcLoc, case_vals.len);
@memset(case_srcs, .unneeded);
break :resolve sema.resolvePeerTypes(block, .unneeded, dummy_captures, .{ .override = case_srcs }) catch |err| switch (err) {
error.NeededSourceLocation => {
// This must be a multi-prong so this must be a `multi_capture` src
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 = @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);
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", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, case_src, msg, "pointer type child '{}' cannot cast into resolved pointer type child '{}'", .{
field_ty.fmt(sema.mod),
capture_ty.fmt(sema.mod),
});
try sema.errNote(block, capture_src, msg, "this coercion is only possible when capturing by value", .{});
break :msg msg;
_ = sema.resolvePeerTypes(block, capture_src, dummy_captures, .{ .override = case_srcs }) catch |err1| switch (err1) {
error.AnalysisFail => {
const msg = sema.err orelse return error.AnalysisFail;
try sema.errNote(block, capture_src, msg, "this coercion is only possible when capturing by value", .{});
try sema.reparentOwnedErrorMsg(block, capture_src, msg, "capture group with incompatible types", .{});
return error.AnalysisFail;
},
else => |e| return e,
};
return sema.failWithOwnedErrorMsg(block, msg);
}
}
}
unreachable;
},
else => |e| return e,
};
};
if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |op_ptr_val| {
if (op_ptr_val.isUndef(mod)) return mod.undefRef(capture_ptr_ty);