mirror of
https://github.com/ziglang/zig.git
synced 2025-12-25 15:43:06 +00:00
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:
parent
3aa533519d
commit
cfae70ec8e
@ -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 => {
|
||||
|
||||
@ -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`.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user