Sema: fix for loops with comptime-known int ranges

This commit is contained in:
Andrew Kelley 2023-02-18 10:27:08 -07:00
parent 552e8095ae
commit f2a6a1756b
2 changed files with 17 additions and 1 deletions

View File

@ -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;

View File

@ -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);
}