authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 12:22:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 09:53:55-04:00
log8688c437455d8e8f1f031177375e570022c16ce7
treedcb130a083ff3fe086cee9c79899b467ebd0ebb7
parente947f0c7409c719377ca08fb09ec72a558a60d99
signaturelock-open Commit is signed but in an unrecognized format.

when result loc is a slice, avoid evaluating lazy start..end

This prevents lazy values from being unnecessarily evaluated.

1 files changed, 18 insertions(+), 3 deletions(-)

src/ir.cpp+18-3
......@@ -26191,6 +26191,16 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy
2619126191 return ir_build_memcpy_gen(ira, &instruction->base.base, casted_dest_ptr, casted_src_ptr, casted_count);
2619226192}
2619326193
26194static ZigType *get_result_loc_type(IrAnalyze *ira, ResultLoc *result_loc) {
26195 if (result_loc == nullptr) return nullptr;
26196
26197 if (result_loc->id == ResultLocIdCast) {
26198 return ir_resolve_type(ira, result_loc->source_instruction->child);
26199 }
26200
26201 return nullptr;
26202}
26203
2619426204static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *instruction) {
2619526205 Error err;
2619626206
......@@ -26297,11 +26307,16 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2629726307 ZigType *child_array_type = (array_type->id == ZigTypeIdPointer &&
2629826308 array_type->data.pointer.ptr_len == PtrLenSingle) ? array_type->data.pointer.child_type : array_type;
2629926309
26300 // If start index and end index are both comptime known, then the result type is a pointer to array
26301 // not a slice.
2630226310 ZigType *return_type;
2630326311
26304 if (value_is_comptime(casted_start->value) &&
26312 // If start index and end index are both comptime known, then the result type is a pointer to array
26313 // not a slice. However, if the start or end index is a lazy value, and the result location is a slice,
26314 // then the pointer-to-array would be casted to a slice anyway. So, we preserve the laziness of these
26315 // values by making the return type a slice.
26316 ZigType *res_loc_type = get_result_loc_type(ira, instruction->result_loc);
26317
26318 if ((res_loc_type == nullptr || !is_slice(res_loc_type)) &&
26319 value_is_comptime(casted_start->value) &&
2630526320 ((end != nullptr && value_is_comptime(end->value)) ||
2630626321 (end == nullptr && child_array_type->id == ZigTypeIdArray)))
2630726322 {