mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
The old lowering was kind of neat, but it unintentionally allowed the
syntax `for (123) |_| { ... }`, and there wasn't really a way to fix
that. So, instead, we include both the start and the end of the range in
the `for_len` instruction (each operand to `for` now has *two* entries
in this multi-op instruction). This slightly increases the size of ZIR
for loops of predominantly indexables, but the difference is small
enough that it's not worth complicating ZIR to try and fix it.
48 lines
1.2 KiB
Zig
48 lines
1.2 KiB
Zig
export fn a() void {
|
|
for (0..10, 10..21) |i, j| {
|
|
_ = i;
|
|
_ = j;
|
|
}
|
|
}
|
|
export fn b() void {
|
|
const s1 = "hello";
|
|
const s2 = true;
|
|
for (s1, s2) |i, j| {
|
|
_ = i;
|
|
_ = j;
|
|
}
|
|
}
|
|
export fn c() void {
|
|
var buf: [10]u8 = undefined;
|
|
for (buf) |*byte| {
|
|
_ = byte;
|
|
}
|
|
_ = &buf;
|
|
}
|
|
export fn d() void {
|
|
const x: [*]const u8 = "hello";
|
|
const y: [*]const u8 = "world";
|
|
for (x, 0.., y) |x1, x2, x3| {
|
|
_ = x1;
|
|
_ = x2;
|
|
_ = x3;
|
|
}
|
|
}
|
|
export fn e() void {
|
|
for (123) |_| {}
|
|
}
|
|
|
|
// error
|
|
//
|
|
// :2:5: error: non-matching for loop lengths
|
|
// :2:11: note: length 10 here
|
|
// :2:19: note: length 11 here
|
|
// :10:14: error: type 'bool' is not indexable and not a range
|
|
// :10:14: note: for loop operand must be a range, array, slice, tuple, or vector
|
|
// :17:16: error: pointer capture of non pointer type '[10]u8'
|
|
// :17:10: note: consider using '&' here
|
|
// :25:5: error: unbounded for loop
|
|
// :25:10: note: type '[*]const u8' has no upper bound
|
|
// :25:18: note: type '[*]const u8' has no upper bound
|
|
// :32:10: error: type 'comptime_int' is not indexable and not a range
|
|
// :32:10: note: for loop operand must be a range, array, slice, tuple, or vector
|