Skip to content

Commit

Permalink
Sema: type safety for "runtime_index" field
Browse files Browse the repository at this point in the history
This commit does not change any behavior, but changes the type of
the runtime_index field from u32 to a non-exhaustive enum. This allows
us to put `std.math.maxInt(u32)` only in the enum type definition and
give it an official meaning.
  • Loading branch information
andrewrk committed Jun 11, 2022
1 parent 13123af commit 9360cfe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub const Block = struct {
src_decl: Decl.Index,
/// Non zero if a non-inline loop or a runtime conditional have been encountered.
/// Stores to to comptime variables are only allowed when var.runtime_index <= runtime_index.
runtime_index: u32 = 0,
runtime_index: Value.RuntimeIndex = .zero,

is_comptime: bool,
is_typeof: bool = false,
Expand Down Expand Up @@ -4276,7 +4276,7 @@ fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError
child_block.label = &label;
child_block.runtime_cond = null;
child_block.runtime_loop = src;
child_block.runtime_index += 1;
child_block.runtime_index.increment();
const merges = &child_block.label.?.merges;

defer child_block.instructions.deinit(gpa);
Expand Down Expand Up @@ -4795,7 +4795,7 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
const br_ref = try start_block.addBr(label.merges.block_inst, operand);
try label.merges.results.append(sema.gpa, operand);
try label.merges.br_list.append(sema.gpa, Air.refToIndex(br_ref).?);
block.runtime_index += 1;
block.runtime_index.increment();
if (block.runtime_cond == null and block.runtime_loop == null) {
block.runtime_cond = start_block.runtime_cond orelse start_block.runtime_loop;
block.runtime_loop = start_block.runtime_loop;
Expand Down Expand Up @@ -8650,7 +8650,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
var case_block = child_block.makeSubBlock();
case_block.runtime_loop = null;
case_block.runtime_cond = operand_src;
case_block.runtime_index += 1;
case_block.runtime_index.increment();
defer case_block.instructions.deinit(gpa);

var extra_index: usize = special.end;
Expand Down Expand Up @@ -12938,7 +12938,7 @@ fn zirBoolBr(
var child_block = parent_block.makeSubBlock();
child_block.runtime_loop = null;
child_block.runtime_cond = lhs_src;
child_block.runtime_index += 1;
child_block.runtime_index.increment();
defer child_block.instructions.deinit(gpa);

var then_block = child_block.makeSubBlock();
Expand Down Expand Up @@ -13066,7 +13066,7 @@ fn zirCondbr(
var sub_block = parent_block.makeSubBlock();
sub_block.runtime_loop = null;
sub_block.runtime_cond = cond_src;
sub_block.runtime_index += 1;
sub_block.runtime_index.increment();
defer sub_block.instructions.deinit(gpa);

_ = sema.analyzeBodyInner(&sub_block, then_body) catch |err| switch (err) {
Expand Down Expand Up @@ -13263,7 +13263,7 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi
const br_ref = try child_block.addBr(labeled_block.label.merges.block_inst, operand);
try labeled_block.label.merges.results.append(sema.gpa, operand);
try labeled_block.label.merges.br_list.append(sema.gpa, Air.refToIndex(br_ref).?);
labeled_block.block.runtime_index += 1;
labeled_block.block.runtime_index.increment();
if (labeled_block.block.runtime_cond == null and labeled_block.block.runtime_loop == null) {
labeled_block.block.runtime_cond = child_block.runtime_cond orelse child_block.runtime_loop;
labeled_block.block.runtime_loop = child_block.runtime_loop;
Expand Down Expand Up @@ -15812,7 +15812,7 @@ fn checkComptimeVarStore(
src: LazySrcLoc,
decl_ref_mut: Value.Payload.DeclRefMut.Data,
) CompileError!void {
if (decl_ref_mut.runtime_index < block.runtime_index) {
if (@enumToInt(decl_ref_mut.runtime_index) < @enumToInt(block.runtime_index)) {
if (block.runtime_cond) |cond_src| {
const msg = msg: {
const msg = try sema.errMsg(block, src, "store to comptime variable depends on runtime condition", .{});
Expand Down Expand Up @@ -20526,8 +20526,7 @@ fn storePtrVal(

const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, mut_kit.ty, 0);

if (mut_kit.decl_ref_mut.runtime_index == std.math.maxInt(u32)) {
// Special case for comptime field ptr.
if (mut_kit.decl_ref_mut.runtime_index == .comptime_field_ptr) {
if (!mut_kit.val.eql(bitcasted_val, mut_kit.ty, sema.mod)) {
return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});
}
Expand Down Expand Up @@ -20590,7 +20589,7 @@ fn beginComptimePtrMutation(
return ComptimePtrMutationKit{
.decl_ref_mut = .{
.decl_index = @intToEnum(Module.Decl.Index, 0),
.runtime_index = std.math.maxInt(u32),
.runtime_index = .comptime_field_ptr,
},
.val = duped,
.ty = payload.field_ty,
Expand Down
12 changes: 11 additions & 1 deletion src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4768,7 +4768,7 @@ pub const Value = extern union {

pub const Data = struct {
decl_index: Module.Decl.Index,
runtime_index: u32,
runtime_index: RuntimeIndex,
};
};

Expand Down Expand Up @@ -4965,6 +4965,16 @@ pub const Value = extern union {
pub fn makeBool(x: bool) Value {
return if (x) Value.@"true" else Value.@"false";
}

pub const RuntimeIndex = enum(u32) {
zero = 0,
comptime_field_ptr = std.math.maxInt(u32),
_,

pub fn increment(ri: *RuntimeIndex) void {
ri.* = @intToEnum(RuntimeIndex, @enumToInt(ri.*) + 1);
}
};
};

var negative_one_payload: Value.Payload.I64 = .{
Expand Down

1 comment on commit 9360cfe

@andrewrk
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed #11848 to track this CI failure.

Please sign in to comment.