authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-11 23:06:50+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-12 11:41:09+03:00
log09f273136c74639d18695f63ba9c0fdc68ad678c
treefd08ce85f0c34023c27d15fe1d7d4a193f6dd8e2
parent7c9979a02e830a4383995e66ff623a7d07cac091

stage2: check for zero in `@intToError` safety


4 files changed, 34 insertions(+), 11 deletions(-)

src/Sema.zig+12-10
...@@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
6804 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };6804 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6805 const uncasted_operand = try sema.resolveInst(extra.operand);6805 const uncasted_operand = try sema.resolveInst(extra.operand);
6806 const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);6806 const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
6807 const result_ty = Type.u16;
68086807
6809 if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {6808 if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
6810 if (val.isUndef()) {6809 if (val.isUndef()) {
6811 return sema.addConstUndef(result_ty);6810 return sema.addConstUndef(Type.err_int);
6812 }6811 }
6813 switch (val.tag()) {6812 switch (val.tag()) {
6814 .@"error" => {6813 .@"error" => {
...@@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
6817 .base = .{ .tag = .int_u64 },6816 .base = .{ .tag = .int_u64 },
6818 .data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,6817 .data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,
6819 };6818 };
6820 return sema.addConstant(result_ty, Value.initPayload(&payload.base));6819 return sema.addConstant(Type.err_int, Value.initPayload(&payload.base));
6821 },6820 },
68226821
6823 // This is not a valid combination with the type `anyerror`.6822 // This is not a valid combination with the type `anyerror`.
6824 .the_only_possible_value => unreachable,6823 .the_only_possible_value => unreachable,
68256824
6826 // Assume it's already encoded as an integer.6825 // 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),
6828 }6827 }
6829 }6828 }
68306829
...@@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
6833 if (!op_ty.isAnyError()) {6832 if (!op_ty.isAnyError()) {
6834 const names = op_ty.errorSetNames();6833 const names = op_ty.errorSetNames();
6835 switch (names.len) {6834 switch (names.len) {
6836 0 => return sema.addConstant(result_ty, Value.zero),6835 0 => return sema.addConstant(Type.err_int, Value.zero),
6837 1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),6836 1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
6838 else => {},6837 else => {},
6839 }6838 }
6840 }6839 }
68416840
6842 try sema.requireRuntimeBlock(block, src, operand_src);6841 try sema.requireRuntimeBlock(block, src, operand_src);
6843 return block.addBitCast(result_ty, operand);6842 return block.addBitCast(Type.err_int, operand);
6844}6843}
68456844
6846fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {6845fn 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...@@ -6851,7 +6850,7 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
6851 const src = LazySrcLoc.nodeOffset(extra.node);6850 const src = LazySrcLoc.nodeOffset(extra.node);
6852 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };6851 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
6853 const uncasted_operand = try sema.resolveInst(extra.operand);6852 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);
6855 const target = sema.mod.getTarget();6854 const target = sema.mod.getTarget();
68566855
6857 if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {6856 if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
...@@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
6868 try sema.requireRuntimeBlock(block, src, operand_src);6867 try sema.requireRuntimeBlock(block, src, operand_src);
6869 if (block.wantSafety()) {6868 if (block.wantSafety()) {
6870 const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);6869 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);
6872 }6874 }
6873 return block.addInst(.{6875 return block.addInst(.{
6874 .tag = .bitcast,6876 .tag = .bitcast,
...@@ -17360,7 +17362,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -17360,7 +17362,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1736017362
17361 try sema.requireRuntimeBlock(block, src, operand_src);17363 try sema.requireRuntimeBlock(block, src, operand_src);
17362 if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {17364 if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {
17363 const err_int_inst = try block.addBitCast(Type.u16, operand);17365 const err_int_inst = try block.addBitCast(Type.err_int, operand);
17364 const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);17366 const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
17365 try sema.addSafetyCheck(block, ok, .invalid_error_code);17367 try sema.addSafetyCheck(block, ok, .invalid_error_code);
17366 }17368 }
src/codegen/llvm.zig+1-1
...@@ -8005,7 +8005,7 @@ pub const FuncGen = struct {...@@ -8005,7 +8005,7 @@ pub const FuncGen = struct {
8005 .data = err_int,8005 .data = err_int,
8006 };8006 };
8007 break :int try self.dg.lowerValue(.{8007 break :int try self.dg.lowerValue(.{
8008 .ty = Type.u16,8008 .ty = Type.err_int,
8009 .val = Value.initPayload(&tag_val_payload.base),8009 .val = Value.initPayload(&tag_val_payload.base),
8010 });8010 });
8011 };8011 };
src/type.zig+2
...@@ -6303,6 +6303,8 @@ pub const Type = extern union {...@@ -6303,6 +6303,8 @@ pub const Type = extern union {
6303 pub const @"anyopaque" = initTag(.anyopaque);6303 pub const @"anyopaque" = initTag(.anyopaque);
6304 pub const @"null" = initTag(.@"null");6304 pub const @"null" = initTag(.@"null");
63056305
6306 pub const err_int = Type.u16;
6307
6306 pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {6308 pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {
6307 const target = mod.getTarget();6309 const target = mod.getTarget();
63086310
test/cases/safety/zero casted to error.zig created+19
...@@ -0,0 +1,19 @@
1const std = @import("std");
2
3pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
4 _ = stack_trace;
5 if (std.mem.eql(u8, message, "invalid error code")) {
6 std.process.exit(0);
7 }
8 std.process.exit(1);
9}
10pub fn main() !void {
11 bar(0) catch {};
12 return error.TestFailed;
13}
14fn bar(x: u16) anyerror {
15 return @intToError(x);
16}
17// run
18// backend=llvm
19// target=native