Make slice always return a reference

Previously this returned an rvalue, which leads to unexpected behaviour
when writing expressions such as `x[1..][1..].`
This commit is contained in:
Robin Voetter 2021-08-25 03:44:43 +02:00 committed by Andrew Kelley
parent 3aa533519d
commit cfae70ec8e
2 changed files with 24 additions and 3 deletions

View File

@ -698,7 +698,13 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerEr
.lhs = lhs,
.start = start,
});
return rvalue(gz, rl, result, node);
switch (rl) {
.ref, .none_or_ref => return result,
else => {
const dereffed = try gz.addUnNode(.load, result, node);
return rvalue(gz, rl, dereffed, node);
},
}
},
.slice => {
const lhs = try expr(gz, scope, .ref, node_datas[node].lhs);
@ -710,7 +716,13 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerEr
.start = start,
.end = end,
});
return rvalue(gz, rl, result, node);
switch (rl) {
.ref, .none_or_ref => return result,
else => {
const dereffed = try gz.addUnNode(.load, result, node);
return rvalue(gz, rl, dereffed, node);
},
}
},
.slice_sentinel => {
const lhs = try expr(gz, scope, .ref, node_datas[node].lhs);
@ -724,7 +736,13 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerEr
.end = end,
.sentinel = sentinel,
});
return rvalue(gz, rl, result, node);
switch (rl) {
.ref, .none_or_ref => return result,
else => {
const dereffed = try gz.addUnNode(.load, result, node);
return rvalue(gz, rl, dereffed, node);
},
}
},
.deref => {

View File

@ -495,12 +495,15 @@ pub const Inst = struct {
/// Uses the `ptr_type` union field.
ptr_type,
/// Slice operation `lhs[rhs..]`. No sentinel and no end offset.
/// Returns a pointer to the subslice.
/// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceStart`.
slice_start,
/// Slice operation `array_ptr[start..end]`. No sentinel.
/// Returns a pointer to the subslice.
/// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceEnd`.
slice_end,
/// Slice operation `array_ptr[start..end:sentinel]`.
/// Returns a pointer to the subslice.
/// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceSentinel`.
slice_sentinel,
/// Write a value to a pointer. For loading, see `load`.