규칙 7.2
7장, 표현식과 문 테스트
Where TypeShade lowers a TypeScript form to a WGSL form, the mapping must be a rule with a recorded divergence; the mappings today are:
| TypeScript form | WGSL form | Divergence recorded in |
|---|---|---|
| whole-value parameter reassignment | a fresh mutable local initialized from the immutable parameter; body reads and writes use that local | Rule 8.8; surface §14 and §52 |
c ? a : b on a scalar or vector | select(b, a, c), WGSL’s own argument order; an if with a temporary when an arm is costly (a call of a function of the module, a texture read, or 8 or more operations; change 0097), unless an arm holds a derivative, an implicit-LOD sample, a barrier or a call that reaches one, which keeps select | surface §10; CHANGELOG.md (ternary as select); select |
c ? a : b on a struct or an array | an if with a temporary, since WGSL has no select over a composite | surface §31 (#113) |
v.xy = r, v.xy op= r (change 0106) | a let that reads every old component the operation needs, then one write per destination component; no swizzle_assignment extension | a non-constant index of v is read once, first; a repeated component (v.xx) is refused with TS8018 |
a ** b | pow(a, b), float-only | surface §10 |
atan(y, x) | atan2(y, x) | surface §10 |
switch | WGSL’s switch, which never falls through; a trailing break is dropped | surface §14; Switch Statement |
x << n, x >> n | the amount is a u32; an i32 amount is cast (x << u32(n)), binary and compound alike | surface §14, §22 and §52; Bit Expressions |
[a, b] in a typed position | array<T, 2>(a, b) | surface §18 and §28 |
{ field: value } in a typed position | a struct constructor | surface §16 |
obj.method() | Cls_method(obj, …), dispatched statically | surface §26 |
o.x, o.x = v on an accessor | Cls_get_x(o), Cls_set_x(&o, v); o.x += v goes through both | Rule 8.11; surface §26 |
a private name #x | x, private through the front end’s check alone | Rule 8.12; surface §26 |
| a static field the file writes | a var<private> Cls_f, as a top-level let is | Rule 8.13; surface §26 |
| a call that writes, in an expression | a let ahead of it, in source order; an if for ?:, && and || | Rule 7.9; surface §26 |
v.setX(1.).setY(2.) as a statement | V_setX(&v, 1.0); V_setY(&v, 2.0);, each call on the chain’s root | Rule 8.10; surface §26 |
D.f() on a static D inherits | D_f, the base’s body lowered again with this as D | Rule 8.13; surface §26 |
const v = new V(); v.bump() | var v: V = V_new(); V_bump(&v);, a var from the first write through it | Rule 6.10; surface §26 |
f = (x: f32): f32 => … in a class | Cls_f, a method under the field’s name | Rule 8.16; surface §26 |
a local function that reads n of the function around it | fs_f(n, …): n is a parameter of fs_f, which every call passes | Rule 8.17; surface §14 |
a local function that writes n of the function around it | fs_f(&n, …), n: ptr<function, T> in fs_f (inout in GLSL ES 3.00) | Rule 8.17; surface §14 |
apply(sq, x), f a parameter of function type | apply_sq(x): apply once for each function its calls hand it | Rule 8.18; surface §14 |
s.each(sq), each a method that takes a function | Swarm_each_sq(s), and &s where a function handed over writes s, the one reference both use | Rules 8.10 and 8.18; surface §26 |
| an arrow function written as an argument | a local function of the calling body, typed by the parameter | Rule 8.18; surface §14 |
| a function, a method or an accessor with no return type | the type of its first return with a value, -> f32; none without one | Rule 8.19; surface §14 |
() => n += k with no return type | the assignment as a statement, and the function returns nothing, where TypeScript returns the new n | Rule 8.19; surface §14 |
return g(), g returning nothing | g(); return; | Rule 8.19; surface §14 |
const w = v, let w = v on an object (a struct, an instance of a class, an array) | a second name of v: the same place, with no copy, as in JavaScript; TS8099 where w is assigned again later, unless the program never writes an object of that type (change 0142, Rule 8.8) | Rule 6.10; Rule 8.8; surface §26 |
for (const x of a) over an array | a counted for over a’s indices, x read at the top of each trip: a copy of a[i] for a number or a vector, and the element a[i] itself for an object | Rule 7.5; Rule 8.8; surface §17 |
xs.map(f) and the other methods of an array | a call of a function made for the array’s type and f, a counted for: array_map_sq(xs) | Rule 8.18; surface §63 |
the index an array’s method passes its function | an i32, where TypeScript passes a number | Rule 7.5; surface §63 |
the value of xs.map(f) | an array<R, N> value that a const holds; an array of values is new, as in TypeScript, and an array of objects follows Rule 8.8, rules 3 and 4 | Rule 6.10; surface §63 |
s += x in a loop of a kernel function the proof accepts, and xs.reduce(f, init) on its array (change 0079) | a fold in the tree order on every tier, where TypeScript adds in iteration order; an f32 or f64 sum or product may differ from that reading in the last places, and the determinism report lists it as order | Rule 8.22; surface §65; src/core/kernel-tree.ts |
g[idx[i]] += x[i] on an f32 parameter array | the CPU tier folds each element in the tree order, where TypeScript adds in iteration order; the determinism report lists it as order. No GPU tier lowers it yet (TS8070) | Rule 8.22; surface §65; src/core/kernel-tree.ts |
a[k] += e into an integer array in such a loop | an atomic* on a[k] over array<atomic<T>>, the same bytes; exact in any order, where TypeScript adds in iteration order | Rule 8.22; surface §65 |
| a write to a field or an element of an object parameter that its function writes (changes 0040 and 0142) | a write through ptr<function, T> to the caller’s place on WGSL, an inout parameter on GLSL ES 3.00, a store back on the CPU: the write reaches the caller’s object, as it does in TypeScript | Rule 8.25; surface §70; no divergence from TypeScript |
T | undefined as a local, a parameter or a result (change 0088) | an internal struct of the value and a presence flag, __opt_f32 { value: f32, present: bool }, which each writer spells afresh (_opt_f32_, surface §62); the same bytes on every target | Rule 7.10; surface §76 |
undefined where a T | undefined is expected | the absent value: the zero of T and false; let v: T | undefined; with no initializer starts absent on every target | Rule 7.10; surface §76 |
a bare return;, and a path that ends the body, in a function with an optional result (change 0099) | the absent value, as TypeScript reads them; a function with no written result whose return; or fall-through stands beside a return with a value returns T | undefined, as TypeScript infers it (Rule 8.19) | Rule 7.10; surface §76 |
v ?? d | select(d, v.value, v.present), or an if when d writes (Rule 7.9); optional when d is | Rule 7.10; surface §76 |
o?.f, o?.xy, o?.[i] | an optional of the member, { value: o.value.f, present: o.present }; o?.m() and f?.() are refused | Rule 7.10; surface §76 |
v === undefined, v !== undefined | !v.present, v.present, in either operand order and through !; == and != the same (Rule 7.1) | Rule 7.10; surface §76 |
v == null, v != null on any optional | !v.present, v.present, with the same narrowing as a loose undefined test | Rule 7.10; surface §76; change 0098 D4 |
T | null | The same { value: T, present: bool } layout as T | undefined, with distinct logical identity | Rule 7.10; surface §76; change 0098 |
T | null | undefined | { value: T, present: bool, isNull: bool }; false isNull denotes undefined when absent | Rule 7.10; surface §76; change 0098 |
| An uninitialized nullable class field | Null for a null-only type, undefined when admitted; class zero initialization differs from JavaScript and from unassigned nullable locals | Rule 7.10; surface §26, §76; change 0098 D7 |
v === x with x a T; v === w with two optionals | v.present && v.value == x; equal flags and, where both are present, equal values, so two absent values are equal; componentwise through select on a vector | Rule 7.10; surface §76 |
a user type guard, an aliased condition (const ok = v !== undefined), typeof v === "undefined", switch (true) | not followed: a read of v after a type guard or an aliased condition is refused as un-narrowed (TS8003), typeof is refused (TS8099), each with the test to write, and switch (true) is refused as a switch over a bool (TS8003), where TypeScript narrows v | Rule 7.10; surface §76 |
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
a TypeScript reader expects the TypeScript meaning, so every place the two languages differ has to be written down where the reader will look.
출처
the rows above.
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
the emit goldens and the tests each row’s surface section names; the reference parameters by src/compiler/ts/attribute-types.test.ts; the tree order of a kernel function’s reduction by src/core/kernel-tree.test.ts on the CPU backends, and by the import journey on WebGPU.
커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
설명하는 절
커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.
- §10 Builtins, casts and
discard - §14 TypeScript shapes the parser already had
- §16 Object literals take the declared struct
- §17 What a
forloop may say, and what it is told - §18 A list as an array's initializer
- §26 Classes with methods, a constructor and static functions
- §31 A conditional on a struct or an array
- §38 What may differ by driver: the determinism report
- §49 The editor and the compiler agree
- §62 A name a target reserves
- §63 An array's methods
- §65 A loop that runs as a kernel
- §70 Objects, passed as JavaScript passes them
- §71 A chain of kernel calls in one dispatch:
fuse - §72 The common algorithms:
typeshade/algorithms - §76 Optional values:
T | undefined,T | nulland three states
함께 보기
소스
커밋 af54ddad5의 규칙 원문:
docs/language-design.md:625(설계 문서)reqs/rules/RULE-0702.md(추적 항목)