diff options
| author | LemonBoy <thatlemon@gmail.com> | 2020-03-03 12:13:13 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-03-08 18:52:44 -0400 |
| commit | e2fd289a33bb35cf4b86daa4d80adb7cc0c2c2b0 (patch) | |
| tree | abb7d449b2d8e6d505e4ef4598793e3bbe711b16 /src/ir.cpp | |
| parent | 06d0dac0fb58c2d20036e34f5c1a1bc9c386c189 (diff) | |
| download | zig-e2fd289a33bb35cf4b86daa4d80adb7cc0c2c2b0.tar.gz zig-e2fd289a33bb35cf4b86daa4d80adb7cc0c2c2b0.zip | |
ir: Create usize result_loc for array subscript expr
Allow the subscript expression to infer the resulting type.
Closes #4169
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 34f3ef26a7..110778fb9a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5910,10 +5910,18 @@ static IrInstSrc *ir_gen_array_access(IrBuilderSrc *irb, Scope *scope, AstNode * if (array_ref_instruction == irb->codegen->invalid_inst_src) return array_ref_instruction; + // Create an usize-typed result location to hold the subscript value, this + // makes it possible for the compiler to infer the subscript expression type + // if needed + IrInstSrc *usize_type_inst = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize); + ResultLocCast *result_loc_cast = ir_build_cast_result_loc(irb, usize_type_inst, no_result_loc()); + AstNode *subscript_node = node->data.array_access_expr.subscript; - IrInstSrc *subscript_instruction = ir_gen_node(irb, subscript_node, scope); - if (subscript_instruction == irb->codegen->invalid_inst_src) - return subscript_instruction; + IrInstSrc *subscript_value = ir_gen_node_extra(irb, subscript_node, scope, LValNone, &result_loc_cast->base); + if (subscript_value == irb->codegen->invalid_inst_src) + return irb->codegen->invalid_inst_src; + + IrInstSrc *subscript_instruction = ir_build_implicit_cast(irb, scope, subscript_node, subscript_value, result_loc_cast); IrInstSrc *ptr_instruction = ir_build_elem_ptr(irb, scope, node, array_ref_instruction, subscript_instruction, true, PtrLenSingle, nullptr); |
