Merge pull request #12416 from Vexu/stage2-safety

Stage2 error set safety improvements
This commit is contained in:
Veikka Tuominen 2022-08-15 11:32:26 +03:00 committed by GitHub
commit 40eac90280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 99 additions and 20 deletions

View File

@ -673,6 +673,10 @@ pub const Inst = struct {
/// Uses the `un_op` field.
error_name,
/// Returns true if error set has error with value.
/// Uses the `ty_op` field.
error_set_has_value,
/// Constructs a vector, tuple, struct, or array value out of runtime-known elements.
/// Some of the elements may be comptime-known.
/// 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 {
.is_err_ptr,
.is_non_err_ptr,
.is_named_enum_value,
.error_set_has_value,
=> return Type.bool,
.const_ty => return Type.type,

View File

@ -267,6 +267,7 @@ pub fn categorizeOperand(
.byte_swap,
.bit_reverse,
.splat,
.error_set_has_value,
=> {
const o = air_datas[inst].ty_op;
if (o.operand == operand_ref) return matchOperandSmallIndex(l, inst, 0, .none);
@ -842,6 +843,7 @@ fn analyzeInst(
.byte_swap,
.bit_reverse,
.splat,
.error_set_has_value,
=> {
const o = inst_datas[inst].ty_op;
return trackOperands(a, new_set, inst, main_tomb, .{ o.operand, .none, .none });

View File

@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
const uncasted_operand = try sema.resolveInst(extra.operand);
const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
const result_ty = Type.u16;
if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
if (val.isUndef()) {
return sema.addConstUndef(result_ty);
return sema.addConstUndef(Type.err_int);
}
switch (val.tag()) {
.@"error" => {
@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
.base = .{ .tag = .int_u64 },
.data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,
};
return sema.addConstant(result_ty, Value.initPayload(&payload.base));
return sema.addConstant(Type.err_int, Value.initPayload(&payload.base));
},
// This is not a valid combination with the type `anyerror`.
.the_only_possible_value => unreachable,
// Assume it's already encoded as an integer.
else => return sema.addConstant(result_ty, val),
else => return sema.addConstant(Type.err_int, val),
}
}
@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
if (!op_ty.isAnyError()) {
const names = op_ty.errorSetNames();
switch (names.len) {
0 => return sema.addConstant(result_ty, Value.zero),
1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
0 => return sema.addConstant(Type.err_int, Value.zero),
1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
else => {},
}
}
try sema.requireRuntimeBlock(block, src, operand_src);
return block.addBitCast(result_ty, operand);
return block.addBitCast(Type.err_int, operand);
}
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
const src = LazySrcLoc.nodeOffset(extra.node);
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
const uncasted_operand = try sema.resolveInst(extra.operand);
const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
const operand = try sema.coerce(block, Type.err_int, uncasted_operand, operand_src);
const target = sema.mod.getTarget();
if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
try sema.requireRuntimeBlock(block, src, operand_src);
if (block.wantSafety()) {
const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
try sema.addSafetyCheck(block, is_lt_len, .invalid_error_code);
const zero_val = try sema.addConstant(Type.err_int, Value.zero);
const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
const ok = try block.addBinOp(.bit_and, is_lt_len, is_non_zero);
try sema.addSafetyCheck(block, ok, .invalid_error_code);
}
return block.addInst(.{
.tag = .bitcast,
@ -17370,17 +17372,10 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
}
try sema.requireRuntimeBlock(block, src, operand_src);
if (block.wantSafety() and !dest_ty.isAnyError()) {
const err_int_inst = try block.addBitCast(Type.u16, operand);
// TODO: Output a switch instead of chained OR's.
var found_match: Air.Inst.Ref = undefined;
for (dest_ty.errorSetNames()) |dest_err_name, i| {
const dest_err_int = (try sema.mod.getErrorValue(dest_err_name)).value;
const dest_err_int_inst = try sema.addIntUnsigned(Type.u16, dest_err_int);
const next_match = try block.addBinOp(.cmp_eq, dest_err_int_inst, err_int_inst);
found_match = if (i == 0) next_match else try block.addBinOp(.bool_or, found_match, next_match);
}
try sema.addSafetyCheck(block, found_match, .invalid_error_code);
if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {
const err_int_inst = try block.addBitCast(Type.err_int, operand);
const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
try sema.addSafetyCheck(block, ok, .invalid_error_code);
}
return block.addBitCast(dest_ty, operand);
}

View File

@ -778,6 +778,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
=> return self.fail("TODO implement optimized float mode", .{}),
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
.wasm_memory_size => unreachable,
.wasm_memory_grow => unreachable,

View File

@ -796,6 +796,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
=> return self.fail("TODO implement optimized float mode", .{}),
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
.wasm_memory_size => unreachable,
.wasm_memory_grow => unreachable,

View File

@ -694,6 +694,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
=> return self.fail("TODO implement optimized float mode", .{}),
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
.wasm_memory_size => unreachable,
.wasm_memory_grow => unreachable,

View File

@ -706,6 +706,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
=> @panic("TODO implement optimized float mode"),
.is_named_enum_value => @panic("TODO implement is_named_enum_value"),
.error_set_has_value => @panic("TODO implement error_set_has_value"),
.wasm_memory_size => unreachable,
.wasm_memory_grow => unreachable,

View File

@ -1694,6 +1694,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
.err_return_trace,
.set_err_return_trace,
.is_named_enum_value,
.error_set_has_value,
=> |tag| return self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}),
.add_optimized,

View File

@ -776,6 +776,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
=> return self.fail("TODO implement optimized float mode", .{}),
.is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
.error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
.wasm_memory_size => unreachable,
.wasm_memory_grow => unreachable,

View File

@ -1954,6 +1954,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
=> return f.fail("TODO implement optimized float mode", .{}),
.is_named_enum_value => return f.fail("TODO: C backend: implement is_named_enum_value", .{}),
.error_set_has_value => return f.fail("TODO: C backend: implement error_set_has_value", .{}),
// zig fmt: on
};
switch (result_value) {

View File

@ -4247,6 +4247,7 @@ pub const FuncGen = struct {
.prefetch => try self.airPrefetch(inst),
.is_named_enum_value => try self.airIsNamedEnumValue(inst),
.error_set_has_value => try self.airErrorSetHasValue(inst),
.reduce => try self.airReduce(inst, false),
.reduce_optimized => try self.airReduce(inst, true),
@ -7983,6 +7984,53 @@ pub const FuncGen = struct {
}
}
fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const operand = try self.resolveInst(ty_op.operand);
const error_set_ty = self.air.getRefType(ty_op.ty);
const names = error_set_ty.errorSetNames();
const valid_block = self.dg.context.appendBasicBlock(self.llvm_func, "Valid");
const invalid_block = self.dg.context.appendBasicBlock(self.llvm_func, "Invalid");
const end_block = self.context.appendBasicBlock(self.llvm_func, "End");
const switch_instr = self.builder.buildSwitch(operand, invalid_block, @intCast(c_uint, names.len));
for (names) |name| {
const err_int = self.dg.module.global_error_set.get(name).?;
const this_tag_int_value = int: {
var tag_val_payload: Value.Payload.U64 = .{
.base = .{ .tag = .int_u64 },
.data = err_int,
};
break :int try self.dg.lowerValue(.{
.ty = Type.err_int,
.val = Value.initPayload(&tag_val_payload.base),
});
};
switch_instr.addCase(this_tag_int_value, valid_block);
}
self.builder.positionBuilderAtEnd(valid_block);
_ = self.builder.buildBr(end_block);
self.builder.positionBuilderAtEnd(invalid_block);
_ = self.builder.buildBr(end_block);
self.builder.positionBuilderAtEnd(end_block);
const llvm_type = self.dg.context.intType(1);
const incoming_values: [2]*const llvm.Value = .{
llvm_type.constInt(1, .False), llvm_type.constInt(0, .False),
};
const incoming_blocks: [2]*const llvm.BasicBlock = .{
valid_block, invalid_block,
};
const phi_node = self.builder.buildPhi(llvm_type, "");
phi_node.addIncoming(&incoming_values, &incoming_blocks, 2);
return phi_node;
}
fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;

View File

@ -243,6 +243,7 @@ const Writer = struct {
.popcount,
.byte_swap,
.bit_reverse,
.error_set_has_value,
=> try w.writeTyOp(s, inst),
.block,

View File

@ -6303,6 +6303,8 @@ pub const Type = extern union {
pub const @"anyopaque" = initTag(.anyopaque);
pub const @"null" = initTag(.@"null");
pub const err_int = Type.u16;
pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {
const target = mod.getTarget();

View File

@ -0,0 +1,19 @@
const std = @import("std");
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
_ = stack_trace;
if (std.mem.eql(u8, message, "invalid error code")) {
std.process.exit(0);
}
std.process.exit(1);
}
pub fn main() !void {
bar(0) catch {};
return error.TestFailed;
}
fn bar(x: u16) anyerror {
return @intToError(x);
}
// run
// backend=llvm
// target=native