Skip to content

Commit 7d6c967

Browse files
committed
copy elision: implicit cast error set to error union runtime
```zig export fn entry() void { var x = error.Failure; var y: error!Foo = x; } ``` ```llvm define void @entry() #2 !dbg !42 { Entry: %x = alloca i16, align 2 %y = alloca { i16, %Foo }, align 4 store i16 1, i16* %x, align 2, !dbg !63 call void @llvm.dbg.declare(metadata i16* %x, metadata !46, metadata !DIExpression()), !dbg !64 %0 = load i16, i16* %x, align 2, !dbg !65 %1 = getelementptr inbounds { i16, %Foo }, { i16, %Foo }* %y, i32 0, i32 0, !dbg !66 store i16 %0, i16* %1, align 2, !dbg !65 call void @llvm.dbg.declare(metadata { i16, %Foo }* %y, metadata !48, metadata !DIExpression()), !dbg !66 ret void, !dbg !67 } ```
1 parent 88427ae commit 7d6c967

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/ir.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13377,30 +13377,25 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1337713377
}
1337813378

1337913379
static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
13380-
IrInstruction *ptr, IrInstruction *value)
13380+
IrInstruction *uncasted_ptr, IrInstruction *value)
1338113381
{
13382-
if (ptr->value.type->id != ZigTypeIdPointer) {
13383-
ir_add_error(ira, ptr,
13384-
buf_sprintf("attempt to dereference non pointer type '%s'", buf_ptr(&ptr->value.type->name)));
13382+
if (uncasted_ptr->value.type->id != ZigTypeIdPointer) {
13383+
ir_add_error(ira, uncasted_ptr,
13384+
buf_sprintf("attempt to dereference non pointer type '%s'", buf_ptr(&uncasted_ptr->value.type->name)));
1338513385
return ira->codegen->invalid_instruction;
1338613386
}
1338713387

13388-
Error err;
13389-
if ((err = resolve_possible_alloca_inference(ira, ptr, value->value.type)))
13390-
return ira->codegen->invalid_instruction;
13391-
13392-
if (ptr->value.data.x_ptr.special == ConstPtrSpecialDiscard) {
13388+
if (uncasted_ptr->value.data.x_ptr.special == ConstPtrSpecialDiscard) {
1339313389
return ir_const_void(ira, source_instr);
1339413390
}
1339513391

13396-
if (ptr->value.type->data.pointer.is_const && !source_instr->is_gen) {
13392+
if (uncasted_ptr->value.type->data.pointer.is_const && !source_instr->is_gen) {
1339713393
ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
1339813394
return ira->codegen->invalid_instruction;
1339913395
}
1340013396

13401-
ZigType *child_type = ptr->value.type->data.pointer.child_type;
13402-
IrInstruction *casted_value = ir_implicit_cast(ira, value, child_type);
13403-
if (casted_value == ira->codegen->invalid_instruction)
13397+
IrInstruction *ptr = ir_implicit_cast_result(ira, uncasted_ptr, value->value.type);
13398+
if (type_is_invalid(ptr->value.type))
1340413399
return ira->codegen->invalid_instruction;
1340513400

1340613401
if (instr_is_comptime(ptr) && ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
@@ -13409,12 +13404,12 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1340913404
return ira->codegen->invalid_instruction;
1341013405
}
1341113406
if (ptr->value.data.x_ptr.mut == ConstPtrMutComptimeVar) {
13412-
if (instr_is_comptime(casted_value)) {
13407+
if (instr_is_comptime(value)) {
1341313408
ConstExprValue *dest_val = ir_const_ptr_pointee(ira, &ptr->value, source_instr->source_node);
1341413409
if (dest_val == nullptr)
1341513410
return ira->codegen->invalid_instruction;
1341613411
if (dest_val->special != ConstValSpecialRuntime) {
13417-
*dest_val = casted_value->value;
13412+
*dest_val = value->value;
1341813413
if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {
1341913414
ira->new_irb.current_basic_block->must_be_comptime_source_instr = source_instr;
1342013415
}
@@ -13431,7 +13426,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1343113426
}
1343213427

1343313428
IrInstruction *result = ir_build_store_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node,
13434-
ptr, casted_value);
13429+
ptr, value);
1343513430
result->value.type = ira->codegen->builtin_types.entry_void;
1343613431
return result;
1343713432
}

0 commit comments

Comments
 (0)