Commit f2a6a1756b

Andrew Kelley <andrew@ziglang.org>
2023-02-18 18:27:08
Sema: fix for loops with comptime-known int ranges
1 parent 552e809
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -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;
 
test/behavior/for.zig
@@ -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);
+}