Skip to content

Commit 40eac90

Browse files
authored
Merge pull request #12416 from Vexu/stage2-safety
Stage2 error set safety improvements
2 parents cb901e5 + 09f2731 commit 40eac90

File tree

14 files changed

+99
-20
lines changed

14 files changed

+99
-20
lines changed

src/Air.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ pub const Inst = struct {
673673
/// Uses the `un_op` field.
674674
error_name,
675675

676+
/// Returns true if error set has error with value.
677+
/// Uses the `ty_op` field.
678+
error_set_has_value,
679+
676680
/// Constructs a vector, tuple, struct, or array value out of runtime-known elements.
677681
/// Some of the elements may be comptime-known.
678682
/// Uses the `ty_pl` field, payload is index of an array of elements, each of which
@@ -1062,6 +1066,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
10621066
.is_err_ptr,
10631067
.is_non_err_ptr,
10641068
.is_named_enum_value,
1069+
.error_set_has_value,
10651070
=> return Type.bool,
10661071

10671072
.const_ty => return Type.type,

src/Liveness.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub fn categorizeOperand(
267267
.byte_swap,
268268
.bit_reverse,
269269
.splat,
270+
.error_set_has_value,
270271
=> {
271272
const o = air_datas[inst].ty_op;
272273
if (o.operand == operand_ref) return matchOperandSmallIndex(l, inst, 0, .none);
@@ -842,6 +843,7 @@ fn analyzeInst(
842843
.byte_swap,
843844
.bit_reverse,
844845
.splat,
846+
.error_set_has_value,
845847
=> {
846848
const o = inst_datas[inst].ty_op;
847849
return trackOperands(a, new_set, inst, main_tomb, .{ o.operand, .none, .none });

src/Sema.zig

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68046804
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
68056805
const uncasted_operand = try sema.resolveInst(extra.operand);
68066806
const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
6807-
const result_ty = Type.u16;
68086807

68096808
if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
68106809
if (val.isUndef()) {
6811-
return sema.addConstUndef(result_ty);
6810+
return sema.addConstUndef(Type.err_int);
68126811
}
68136812
switch (val.tag()) {
68146813
.@"error" => {
@@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68176816
.base = .{ .tag = .int_u64 },
68186817
.data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,
68196818
};
6820-
return sema.addConstant(result_ty, Value.initPayload(&payload.base));
6819+
return sema.addConstant(Type.err_int, Value.initPayload(&payload.base));
68216820
},
68226821

68236822
// This is not a valid combination with the type `anyerror`.
68246823
.the_only_possible_value => unreachable,
68256824

68266825
// Assume it's already encoded as an integer.
6827-
else => return sema.addConstant(result_ty, val),
6826+
else => return sema.addConstant(Type.err_int, val),
68286827
}
68296828
}
68306829

@@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68336832
if (!op_ty.isAnyError()) {
68346833
const names = op_ty.errorSetNames();
68356834
switch (names.len) {
6836-
0 => return sema.addConstant(result_ty, Value.zero),
6837-
1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
6835+
0 => return sema.addConstant(Type.err_int, Value.zero),
6836+
1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
68386837
else => {},
68396838
}
68406839
}
68416840

68426841
try sema.requireRuntimeBlock(block, src, operand_src);
6843-
return block.addBitCast(result_ty, operand);
6842+
return block.addBitCast(Type.err_int, operand);
68446843
}
68456844

68466845
fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
@@ -6851,7 +6850,7 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68516850
const src = LazySrcLoc.nodeOffset(extra.node);
68526851
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
68536852
const uncasted_operand = try sema.resolveInst(extra.operand);
6854-
const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
6853+
const operand = try sema.coerce(block, Type.err_int, uncasted_operand, operand_src);
68556854
const target = sema.mod.getTarget();
68566855

68576856
if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
@@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68686867
try sema.requireRuntimeBlock(block, src, operand_src);
68696868
if (block.wantSafety()) {
68706869
const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
6871-
try sema.addSafetyCheck(block, is_lt_len, .invalid_error_code);
6870+
const zero_val = try sema.addConstant(Type.err_int, Value.zero);
6871+
const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
6872+
const ok = try block.addBinOp(.bit_and, is_lt_len, is_non_zero);
6873+
try sema.addSafetyCheck(block, ok, .invalid_error_code);
68726874
}
68736875
return block.addInst(.{
68746876
.tag = .bitcast,
@@ -17370,17 +17372,10 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1737017372
}
1737117373

1737217374
try sema.requireRuntimeBlock(block, src, operand_src);
17373-
if (block.wantSafety() and !dest_ty.isAnyError()) {
17374-
const err_int_inst = try block.addBitCast(Type.u16, operand);
17375-
// TODO: Output a switch instead of chained OR's.
17376-
var found_match: Air.Inst.Ref = undefined;
17377-
for (dest_ty.errorSetNames()) |dest_err_name, i| {
17378-
const dest_err_int = (try sema.mod.getErrorValue(dest_err_name)).value;
17379-
const dest_err_int_inst = try sema.addIntUnsigned(Type.u16, dest_err_int);
17380-
const next_match = try block.addBinOp(.cmp_eq, dest_err_int_inst, err_int_inst);
17381-
found_match = if (i == 0) next_match else try block.addBinOp(.bool_or, found_match, next_match);
17382-
}
17383-
try sema.addSafetyCheck(block, found_match, .invalid_error_code);
17375+
if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {
17376+
const err_int_inst = try block.addBitCast(Type.err_int, operand);
17377+
const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
17378+
try sema.addSafetyCheck(block, ok, .invalid_error_code);
1738417379
}
1738517380
return block.addBitCast(dest_ty, operand);
1738617381
}

src/arch/aarch64/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
778778
=> return self.fail("TODO implement optimized float mode", .{}),
779779

780780
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
781+
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
781782

782783
.wasm_memory_size => unreachable,
783784
.wasm_memory_grow => unreachable,

src/arch/arm/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
796796
=> return self.fail("TODO implement optimized float mode", .{}),
797797

798798
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
799+
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
799800

800801
.wasm_memory_size => unreachable,
801802
.wasm_memory_grow => unreachable,

src/arch/riscv64/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
694694
=> return self.fail("TODO implement optimized float mode", .{}),
695695

696696
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
697+
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
697698

698699
.wasm_memory_size => unreachable,
699700
.wasm_memory_grow => unreachable,

src/arch/sparc64/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
706706
=> @panic("TODO implement optimized float mode"),
707707

708708
.is_named_enum_value => @panic("TODO implement is_named_enum_value"),
709+
.error_set_has_value => @panic("TODO implement error_set_has_value"),
709710

710711
.wasm_memory_size => unreachable,
711712
.wasm_memory_grow => unreachable,

src/arch/wasm/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
16941694
.err_return_trace,
16951695
.set_err_return_trace,
16961696
.is_named_enum_value,
1697+
.error_set_has_value,
16971698
=> |tag| return self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}),
16981699

16991700
.add_optimized,

src/arch/x86_64/CodeGen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
776776
=> return self.fail("TODO implement optimized float mode", .{}),
777777

778778
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
779+
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
779780

780781
.wasm_memory_size => unreachable,
781782
.wasm_memory_grow => unreachable,

src/codegen/c.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
19541954
=> return f.fail("TODO implement optimized float mode", .{}),
19551955

19561956
.is_named_enum_value => return f.fail("TODO: C backend: implement is_named_enum_value", .{}),
1957+
.error_set_has_value => return f.fail("TODO: C backend: implement error_set_has_value", .{}),
19571958
// zig fmt: on
19581959
};
19591960
switch (result_value) {

0 commit comments

Comments
 (0)