Skip to content

Commit c41ac8f

Browse files
authored
Merge pull request #24534 from ziglang/fix-missed-opvs
Sema: fix missed simplifications of OPVs
2 parents 9abc323 + 7d02b69 commit c41ac8f

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/Sema.zig

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5000,9 +5000,11 @@ fn validateUnionInit(
50005000
}
50015001
if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
50025002

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+
}
50065008
}
50075009

50085010
fn validateStructInit(
@@ -6560,6 +6562,11 @@ fn resolveAnalyzedBlock(
65606562
} },
65616563
});
65626564
}
6565+
6566+
if (try sema.typeHasOnePossibleValue(resolved_ty)) |block_only_value| {
6567+
return Air.internedToRef(block_only_value.toIntern());
6568+
}
6569+
65636570
return merges.block_inst.toRef();
65646571
}
65656572

@@ -9056,6 +9063,10 @@ fn analyzeErrUnionPayload(
90569063
try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
90579064
}
90589065

9066+
if (try sema.typeHasOnePossibleValue(payload_ty)) |payload_only_value| {
9067+
return Air.internedToRef(payload_only_value.toIntern());
9068+
}
9069+
90599070
return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand);
90609071
}
90619072

@@ -19690,8 +19701,10 @@ fn zirStructInit(
1969019701
const base_ptr = try sema.optEuBasePtrInit(block, alloc, src);
1969119702
const field_ptr = try sema.unionFieldPtr(block, field_src, base_ptr, field_name, field_src, resolved_ty, true);
1969219703
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+
}
1969519708
return sema.makePtrConst(block, alloc);
1969619709
}
1969719710

@@ -28079,10 +28092,16 @@ fn unionFieldVal(
2807928092
const active_tag = try block.addTyOp(.get_union_tag, .fromInterned(union_obj.enum_tag_ty), union_byval);
2808028093
try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag);
2808128094
}
28095+
2808228096
if (field_ty.zigTypeTag(zcu) == .noreturn) {
2808328097
_ = try block.addNoOp(.unreach);
2808428098
return .unreachable_value;
2808528099
}
28100+
28101+
if (try sema.typeHasOnePossibleValue(field_ty)) |field_only_value| {
28102+
return Air.internedToRef(field_only_value.toIntern());
28103+
}
28104+
2808628105
try field_ty.resolveLayout(pt);
2808728106
return block.addStructFieldVal(union_byval, field_index, field_ty);
2808828107
}
@@ -28214,12 +28233,12 @@ fn elemVal(
2821428233
.many, .c => {
2821528234
const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
2821628235
const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
28236+
const elem_ty = indexable_ty.elemType2(zcu);
2821728237

2821828238
ct: {
2821928239
const indexable_val = maybe_indexable_val orelse break :ct;
2822028240
const index_val = maybe_index_val orelse break :ct;
2822128241
const index: usize = @intCast(try index_val.toUnsignedIntSema(pt));
28222-
const elem_ty = indexable_ty.elemType2(zcu);
2822328242
const many_ptr_ty = try pt.manyConstPtrType(elem_ty);
2822428243
const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty);
2822528244
const elem_ptr_ty = try pt.singleConstPtrType(elem_ty);
@@ -28228,6 +28247,10 @@ fn elemVal(
2822828247
return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
2822928248
}
2823028249

28250+
if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28251+
return Air.internedToRef(elem_only_value.toIntern());
28252+
}
28253+
2823128254
try sema.checkLogicalPtrOperation(block, src, indexable_ty);
2823228255
return block.addBinOp(.ptr_elem_val, indexable, elem_index);
2823328256
},
@@ -28578,6 +28601,10 @@ fn elemValSlice(
2857828601
}
2857928602
}
2858028603

28604+
if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28605+
return Air.internedToRef(elem_only_value.toIntern());
28606+
}
28607+
2858128608
try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
2858228609
try sema.validateRuntimeValue(block, slice_src, slice);
2858328610

0 commit comments

Comments
 (0)