@@ -5000,9 +5000,11 @@ fn validateUnionInit(
5000
5000
}
5001
5001
if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
5002
5002
5003
- const new_tag = Air.internedToRef(tag_val.toIntern());
5004
- const set_tag_inst = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
5005
- try sema.checkComptimeKnownStore(block, set_tag_inst, LazySrcLoc.unneeded); // `unneeded` since this isn't a "proper" store
5003
+ if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) {
5004
+ const new_tag = Air.internedToRef(tag_val.toIntern());
5005
+ const set_tag_inst = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
5006
+ try sema.checkComptimeKnownStore(block, set_tag_inst, LazySrcLoc.unneeded); // `unneeded` since this isn't a "proper" store
5007
+ }
5006
5008
}
5007
5009
5008
5010
fn validateStructInit(
@@ -6560,6 +6562,11 @@ fn resolveAnalyzedBlock(
6560
6562
} },
6561
6563
});
6562
6564
}
6565
+
6566
+ if (try sema.typeHasOnePossibleValue(resolved_ty)) |block_only_value| {
6567
+ return Air.internedToRef(block_only_value.toIntern());
6568
+ }
6569
+
6563
6570
return merges.block_inst.toRef();
6564
6571
}
6565
6572
@@ -9056,6 +9063,10 @@ fn analyzeErrUnionPayload(
9056
9063
try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
9057
9064
}
9058
9065
9066
+ if (try sema.typeHasOnePossibleValue(payload_ty)) |payload_only_value| {
9067
+ return Air.internedToRef(payload_only_value.toIntern());
9068
+ }
9069
+
9059
9070
return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand);
9060
9071
}
9061
9072
@@ -19690,8 +19701,10 @@ fn zirStructInit(
19690
19701
const base_ptr = try sema.optEuBasePtrInit(block, alloc, src);
19691
19702
const field_ptr = try sema.unionFieldPtr(block, field_src, base_ptr, field_name, field_src, resolved_ty, true);
19692
19703
try sema.storePtr(block, src, field_ptr, init_inst);
19693
- const new_tag = Air.internedToRef(tag_val.toIntern());
19694
- _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag);
19704
+ if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) {
19705
+ const new_tag = Air.internedToRef(tag_val.toIntern());
19706
+ _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag);
19707
+ }
19695
19708
return sema.makePtrConst(block, alloc);
19696
19709
}
19697
19710
@@ -28079,10 +28092,16 @@ fn unionFieldVal(
28079
28092
const active_tag = try block.addTyOp(.get_union_tag, .fromInterned(union_obj.enum_tag_ty), union_byval);
28080
28093
try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag);
28081
28094
}
28095
+
28082
28096
if (field_ty.zigTypeTag(zcu) == .noreturn) {
28083
28097
_ = try block.addNoOp(.unreach);
28084
28098
return .unreachable_value;
28085
28099
}
28100
+
28101
+ if (try sema.typeHasOnePossibleValue(field_ty)) |field_only_value| {
28102
+ return Air.internedToRef(field_only_value.toIntern());
28103
+ }
28104
+
28086
28105
try field_ty.resolveLayout(pt);
28087
28106
return block.addStructFieldVal(union_byval, field_index, field_ty);
28088
28107
}
@@ -28214,12 +28233,12 @@ fn elemVal(
28214
28233
.many, .c => {
28215
28234
const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
28216
28235
const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
28236
+ const elem_ty = indexable_ty.elemType2(zcu);
28217
28237
28218
28238
ct: {
28219
28239
const indexable_val = maybe_indexable_val orelse break :ct;
28220
28240
const index_val = maybe_index_val orelse break :ct;
28221
28241
const index: usize = @intCast(try index_val.toUnsignedIntSema(pt));
28222
- const elem_ty = indexable_ty.elemType2(zcu);
28223
28242
const many_ptr_ty = try pt.manyConstPtrType(elem_ty);
28224
28243
const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty);
28225
28244
const elem_ptr_ty = try pt.singleConstPtrType(elem_ty);
@@ -28228,6 +28247,10 @@ fn elemVal(
28228
28247
return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
28229
28248
}
28230
28249
28250
+ if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28251
+ return Air.internedToRef(elem_only_value.toIntern());
28252
+ }
28253
+
28231
28254
try sema.checkLogicalPtrOperation(block, src, indexable_ty);
28232
28255
return block.addBinOp(.ptr_elem_val, indexable, elem_index);
28233
28256
},
@@ -28578,6 +28601,10 @@ fn elemValSlice(
28578
28601
}
28579
28602
}
28580
28603
28604
+ if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28605
+ return Air.internedToRef(elem_only_value.toIntern());
28606
+ }
28607
+
28581
28608
try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
28582
28609
try sema.validateRuntimeValue(block, slice_src, slice);
28583
28610
0 commit comments