mirror of
https://github.com/ziglang/zig.git
synced 2026-02-16 14:28:57 +00:00
Sema: fix for loops with comptime-known int ranges
This commit is contained in:
parent
552e8095ae
commit
f2a6a1756b
@ -3924,7 +3924,11 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
|
||||
const object_ty = sema.typeOf(object);
|
||||
// Each arg could be an indexable, or a range, in which case the length
|
||||
// is passed directly as an integer.
|
||||
const arg_len = if (object_ty.zigTypeTag() == .Int) object else l: {
|
||||
const is_int = switch (object_ty.zigTypeTag()) {
|
||||
.Int, .ComptimeInt => true,
|
||||
else => false,
|
||||
};
|
||||
const arg_len = if (is_int) object else l: {
|
||||
try checkIndexable(sema, block, src, object_ty);
|
||||
if (!object_ty.indexableHasLen()) continue;
|
||||
|
||||
|
||||
@ -249,3 +249,15 @@ test "for loop with else branch" {
|
||||
try expect(q == 4);
|
||||
}
|
||||
}
|
||||
|
||||
test "count over fixed range" {
|
||||
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
||||
|
||||
var sum: usize = 0;
|
||||
for (0..6) |i| {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
try expect(sum == 15);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user