---
title: "규칙 7.5: 표현식과 문, TypeShade 셰이더 언어 설계 규칙과 그 근거, 출처, 검증 방식"
description: "TypeShade 언어 설계 규칙 7.5(표현식과 문)입니다. A for loop means what it means in TypeScript: its initializer runs once…"
url: https://typeshade.dev/ko/reference/rules/7-5/
language: ko
---

# 규칙 7.5

7장, 표현식과 문 테스트

A `for` loop means what it means in TypeScript: its initializer runs once, its condition (`true` when it has none) is tested before each trip, and its update runs after each trip and at each `continue` (change 0136). A `for` is counted when it has the counted shape: one `let` that declares an `i32` or `u32` induction variable, a constant step, and an exit that compares the induction variable to a bound; the start and the bound may be runtime values, the loop body does not write the bound, and the step moves the variable toward the bound. A runtime bound is then compared with `<`, `<=`, `>` or `>=`, and a multiplicative step with a runtime start or bound is a whole factor of 2 or more. The compiler records the counted fact of a counted `for`; any other `for` is an open loop, as a `while` is: a header with no initializer, no condition or no update, two declarations or a comma update, a counter of another type, a bound the body writes, an exit that is not one comparison with a bound, and an update that is not a constant step. A header whose start and bound are constants must reach its bound within the induction type’s range, and its step must move the variable. A name the header declares with no annotation and a value written as an integer is an `i32`, or a `u32` when the value is a non-negative integer literal and the bound it is compared with is a `u32`; that is the integer type the position declares for [Rule 5.1](https://typeshade.dev/ko/reference/rules/5-1/). A name it declares with any other value takes that value’s type, as a `let` does. A `while` loop is an open loop and may have any `bool` condition; an open loop whose condition is constantly `true` must have a `break` or a `return` in its body. No limit is placed on a trip count. A `for…of` over an array (`array<T, N>`, or a runtime-sized storage array) is a counted loop over the array’s indices, from `0` to its length; its variable is a `const` or a `let` holding a copy of the element, and the array must be a name, or a member or index path to one. A `for…in` must be refused.

규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.

## 근거

both targets accept a runtime bound and an open loop ([#203](https://github.com/typeshade/typeshade/issues/203) measured each on Tint and on ANGLE), and a program over data loops over a count it learns at run time. The analyses of roadmap items 15, 18 and 21 need a counted shape, a step whose direction is known and a bound the body does not move; none of them needs the bound to be a literal ([#203](https://github.com/typeshade/typeshade/issues/203)). Whether a `for` has that shape is a fact the compiler can tell, so the author does not say it (`OD-3`, change 0136): a `for` of another shape is the open loop a `while` is, and both targets accept each such header (measured on Tint and on ANGLE for change 0136). The cost, recorded in change 0136: a `for` no longer promises a reader that it is counted, and a mistake in a counted header (`for (let i = 0; i < n; j++)`) compiles as an open loop that may not end. `for (let i = 0; i < data.length; i++)` is the loop a TypeScript author writes first, and `data.length` is a `u32`: a counter that stayed `i32` would make it a comparison of two integer types the author never wrote. `for (const x of xs)` is the other loop a TypeScript author writes over data: its trip count is the array’s length, which the body cannot change (WGSL has no array that grows), so it is a counted loop with nothing left to check. `for…in` enumerates an object’s keys, which a shader value does not have. Roadmap item 15 is the first to read it: the IR `for` carries the counted fact the front end proved (`counted`, absent on an open loop: a `while`, which is a `for` over a counter it made, and a `for` that is not counted), and the proof of a kernel function’s loops takes it from there ([Rule 8.22](https://typeshade.dev/ko/reference/rules/8-22/)). The IR `for` holds one initializer and one update, as WGSL’s header does, so a header with two declarations or a comma update runs its initializer ahead of the loop and its update at the end of the body and at each `continue` of the loop, over the counter a `while` uses; each name the header declares has an IR name no other local of the function has, so nothing after the loop sees it.

## 출처

[Loop Statement](https://gpuweb.github.io/gpuweb/wgsl/#loop-statement), [For Statement](https://gpuweb.github.io/gpuweb/wgsl/#for-statement), [While Statement](https://gpuweb.github.io/gpuweb/wgsl/#while-statement); ECMAScript [`for` statement](https://tc39.es/ecma262/#sec-for-statement); surface §17; [#203](https://github.com/typeshade/typeshade/issues/203); change `0136` in `changes/`.

## 검증 방식

테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.

컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.

`analyzeCountedFor` in `src/compiler/ts/loop-bound.ts`, which classifies each `for` header, and `lowerFor` in `src/compiler/ts/lower/control.ts`, which lowers a `for` that is not counted as an open loop and leaves an error for every `for` it does not lower, so a refused loop is never dropped from the module; `TS8006 LOOP_BOUND` (a constant header that leaves its type), `TS8007 LOOP_INFINITE` (a constant header whose step never moves the variable or never reaches the bound; a `for` with no condition, or one that folds to `true`, and `while (true)`, or a `while` whose condition folds to `true` from constants through `!`, `&&`, `||` and comparisons, a float compared only over literals and module constants that `f32` holds exactly, with nothing that leaves it, each sentence naming the loop as written: `for (;;) has no break or return in its body, so it never ends. Leave it with a break, or write the exit into the condition.`), [`TS8003`](https://typeshade.dev/ko/reference/errors/ts8003/) for a `while` or a `for` condition that is not a `bool`, as for an `if` condition, with the comparison with zero or the `any`/`all` that says a number’s or a vector of bools’ truthiness (`lowerWhile`); a header clause is lowered as the statement it is, so an update that is no statement is refused as the same statement in a body is; pinned by `src/compiler/ts/runtime-loop-bound.test.ts`, `src/compiler/ts/loop-shapes.test.ts`, `src/compiler/ts/counter-cast.test.ts` (an integer conversion that starts a counter gives it its type, in both halves) and `src/compiler/ts/open-for.test.ts`, which reads both halves and both CPU engines, and by `examples/gallery/loops-over-data/loops-over-data.shade.ts` and the open-loop leg on the compile gate. What is not checked is in Appendix B.

커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.

- [`examples/shade-examples.test.ts:26`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/shade-examples.test.ts#L26)
- [`src/compiler/ts/counter-cast.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/counter-cast.test.ts#L1)
- [`src/compiler/ts/loop-bound.ts:3`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/loop-bound.ts#L3)
- [`src/compiler/ts/loop-shapes.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/loop-shapes.test.ts#L1)
- [`src/compiler/ts/lower/control.ts:10`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/control.ts#L10)
- [`src/compiler/ts/open-for.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/open-for.test.ts#L1)
- [`src/compiler/ts/runtime-loop-bound.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/runtime-loop-bound.test.ts#L1)

## 설명하는 절

커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.

- [§17 What a `for` loop may say, and what it is told](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#17-what-a-for-loop-may-say-and-what-it-is-told)
- [§63 An array's methods](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#63-an-arrays-methods)
- [§65 A loop that runs as a kernel](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#65-a-loop-that-runs-as-a-kernel)

## 적용하는 오류 코드

규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.

**[`TS8003`](https://typeshade.dev/ko/reference/errors/ts8003/) `TYPE_MISMATCH`**

연산자나 조건, 선언, 반환, 인자처럼 두 값이 만나는 자리에서 타입이 서로 맞지 않거나, 검사로 좁히지 않은 선택적 값(`T | undefined`, `T | null`, `T | null | undefined`)을 읽거나([규칙 7.10](https://typeshade.dev/ko/reference/rules/7-10/)), 컴파일러가 접는 상수를 WGSL이 거부합니다. 31을 넘는 시프트 양, 0인 제수, 서로 엇갈린 clamp 경계, 범위를 넘는 `f32` 값, `Math.pow(-2., 2.)`처럼 정의역 밖에서 부르는 내장 함수가 그렇습니다.

**[`TS8006`](https://typeshade.dev/ko/reference/errors/ts8006/) `LOOP_BOUND`**

컴파일러가 조건을 받아들이지 않는 루프입니다. 시작값, 경계, 증분이 모두 상수인 `for`에서 조건이 거짓이 되기 전에 카운터가 타입의 범위를 벗어나거나, 루프 조건 안에서 값을 쓰는 함수를 호출합니다. 이름이 아닌 배열을 도는 `for...of`도 이 코드로 거부됩니다.

**[`TS8007`](https://typeshade.dev/ko/reference/errors/ts8007/) `LOOP_INFINITE`**

끝나지 않는 것이 확실한 루프입니다. `break`나 `return`이 없는 `while (true)`나 조건 없는 `for`가 그렇고, 경계가 상수인 `for`에서 카운터를 경계 쪽으로 움직이지 않는 증분도 그렇습니다. 조건은 그대로 썼든 상수만으로 계산했든 같습니다.

## 함께 보기

- [규칙 7.4](https://typeshade.dev/ko/reference/rules/7-4/) 테스트
- [규칙 7.6](https://typeshade.dev/ko/reference/rules/7-6/) 테스트
- [규칙 5.1](https://typeshade.dev/ko/reference/rules/5-1/)
- [규칙 8.22](https://typeshade.dev/ko/reference/rules/8-22/)
- [설계 규칙](https://typeshade.dev/ko/reference/rules/)
- [오류 코드](https://typeshade.dev/ko/reference/errors/)

## 소스

커밋 af54ddad5의 규칙 원문:

- [`docs/language-design.md:694`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L694) (설계 문서)
- [`reqs/rules/RULE-0705.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0705.md) (추적 항목)
