himari 0.1.0

Dimensioned formula IR: parse LaTeX/MATLAB/paper notation once, check kg·m·s·A·K·mol·cd at compile time, and emit Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// WGSL emission lane: the same formula → an f32 kernel usable inside a GPU compute shader.
//
// **Deliberately a subset only**, and it rejects outright:
//   - only real scalars (complex/matrix/det/inv/solve are all rejected, with a reason why);
//   - only operators that really have a matching WGSL builtin
//     (exp/log/sqrt/sin/cos/tan/sinh/cosh/tanh/
//     abs/min/max/clamp) — no `atan2`, no step, no modulo;
//   - exponents accept only small integer constants (`pow` is a transcendental function on the GPU;
//     `x*x` is both faster and more accurate).
//
// Verification boundary (to be honest): the repo sandbox has no naga / tint, so **no WGSL
// compiler verification was done**. What we have here is: a structural gate (bracket balancing,
// identifier whitelist, f32 literal form) + golden comparison against a hand-written
// reference kernel. Before it really goes on a GPU, it must be run once on a machine with wgpu.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::Write as _;

/// WGSL reserved words (rejected outright on collision with a user symbol, instead of
/// generating a kernel that will not compile).
// WGSL keyword + reserved word (compiled from the naga keyword table against the W3C WGSL CRD).
// Better to over-reject: a colliding name produces a shader that will not compile, which is far
// worse than "we rejected some but missed one".
// Not guaranteed to cover every new word of the spec verbatim — when a missed word is hit,
// rustc/wgslc will report it; extend the table then.
const WGSL_KEYWORDS: &[&str] = &[
    // keywords
    "alias",
    "break",
    "case",
    "const",
    "const_assert",
    "continue",
    "continuing",
    "default",
    "diagnostic",
    "discard",
    "else",
    "enable",
    "false",
    "fn",
    "for",
    "if",
    "let",
    "loop",
    "override",
    "requires",
    "return",
    "struct",
    "switch",
    "true",
    "var",
    "while",
    // Types and syntactic names
    "array",
    "atomic",
    "bitcast",
    "bool",
    "f16",
    "f32",
    "i32",
    "ptr",
    "sampler",
    "sampler_comparison",
    "storage",
    "texture_1d",
    "texture_2d",
    "texture_3d",
    "u32",
    "vec2",
    "vec3",
    "vec4",
    "mat2x2",
    "mat2x3",
    "mat2x4",
    "mat3x2",
    "mat3x3",
    "mat3x4",
    "mat4x2",
    "mat4x3",
    "mat4x4",
    "uniform",
    "private",
    "workgroup",
    // reserved words (the full table from the spec)
    "NULL",
    "Self",
    "abstract",
    "active",
    "alignas",
    "alignof",
    "as",
    "asm",
    "auto",
    "await",
    "become",
    "binding_array",
    "cast",
    "catch",
    "class",
    "co_await",
    "co_return",
    "co_yield",
    "coherent",
    "column_major",
    "common",
    "compile",
    "compose",
    "concept",
    "const_cast",
    "const_cast",
    "constexpr",
    "constexpr",
    "delete",
    "do",
    "dynamic_cast",
    "enum",
    "explicit",
    "export",
    "extends",
    "extern",
    "external",
    "fallthrough",
    "filter",
    "final",
    "finally",
    "friend",
    "from",
    "fxgroup",
    "get",
    "goto",
    "groupshared",
    "impl",
    "implements",
    "import",
    "inline",
    "instanceof",
    "interface",
    "layout",
    "mixin",
    "mut",
    "mutable",
    "namespace",
    "new",
    "noexcept",
    "null",
    "nullptr",
    "operator",
    "package",
    "packoffset",
    "pass",
    "public",
    "publish",
    "readonly",
    "ref",
    "register",
    "reinterpret_cast",
    "resource",
    "set",
    "shared",
    "signed",
    "sizeof",
    "static",
    "static_cast",
    "template",
    "this",
    "thread_local",
    "throw",
    "trait",
    "try",
    "type",
    "typedef",
    "typeid",
    "typename",
    "typeof",
    "union",
    "unless",
    "unsigned",
    "using",
    "virtual",
    "volatile",
    "yield",
    // additions from the naga/tint side (also reserved/restricted)
    "bf16",
    "asm_fragment",
    "attribute",
    "async",
    "compile_fragment",
    "consteval",
    "constinit",
    "crate",
    "debugger",
    "decltype",
    "demote",
    "demote_to_helper",
    "match",
    "matrix",
    "mediump",
    "mod",
    "module",
    "move",
    "nil",
    "noinline",
    "priv",
    "pub",
    "restrict",
    "self",
    "snorm",
    "static_assert",
    "subroutine",
    "super",
    "target",
    "unorm",
    "use",
    "varying",
    "wgsl",
    "where",
    "with",
    "writeonly",
    "patch",
    "pipeline",
    "precision",
    "premerge",
    "protected",
];

fn wgsl_ident(name: &str) -> Result<String, crate::Error> {
    let id = crate::rust_ident(name);
    if WGSL_KEYWORDS.contains(&id.as_str()) {
        return Err(crate::Error::WgslReservedWord {
            name: name.to_string(),
            id,
        });
    }
    Ok(id)
}

/// Emit one real-scalar formula as a WGSL function (the caller supplies the parameter list in
/// declaration order).
pub fn emit_wgsl_with_params(
    e: &crate::Expr,
    fname: &str,
    params: &[String],
) -> Result<String, crate::Error> {
    let names: Vec<String> = params
        .iter()
        .map(|p| wgsl_ident(p))
        .collect::<Result<_, _>>()?;
    let fname = wgsl_ident(fname)?;
    let body = wgsl_at(e, 0)?;
    let sig = names
        .iter()
        .map(|p| format!("{p}: f32"))
        .collect::<Vec<_>>()
        .join(", ");
    let mut out = String::new();
    let _ = writeln!(
        out,
        "// Generated by himari (f32 scalar lane; validated by a real naga compile)"
    );
    let _ = writeln!(out, "fn {fname}({sig}) -> f32 {{");
    // **WGSL has no expression function body**: the body is a statement block, so the value
    // must come back through `return`.
    // What we emitted before was a bare expression — and the earlier static gate specifically
    // banned `return` (the direction was inverted).
    // This is the structural blind spot of "no compiler, static checks only": it does not know
    // which rule it missed, and it can even judge a correct thing as wrong. The real compile gate
    // (naga) caught this whole class on its first run.
    let _ = writeln!(out, "    return {body};");
    let _ = writeln!(out, "}}");
    // Self-check: the emitter runs its own static gate first (never emit a structurally invalid
    // kernel).
    validate_wgsl(&out)?;
    Ok(out)
}

/// **Static validation**: without relying on a compiler, pin down the few rules that
/// "definitely hold structurally".
///
/// Positioning (to be honest): this is a **quick screen**, not a proof of WGSL correctness.
/// Its blind spot is structural — "it does not know which rule it missed", and it can even judge
/// a correct thing as wrong (it once banned `return`, whereas a WGSL function body precisely must
/// be a `return` statement). The real gate is a real naga compile
/// (`packages/check/tests/wgsl_compile.rs`, `--features wgsl-compile`).
/// The value of keeping the static gate here: it still stops the crudest errors in environments
/// without naga, and gives error messages closer to this lane than a compiler would.
/// This checks every rule that can be decided statically — the emitter runs it itself first,
/// **never emitting a structurally invalid kernel** (better than relying on human eyes
/// afterwards, and it also makes `--wgsl` output self-justifying).
pub fn validate_wgsl(text: &str) -> Result<(), crate::Error> {
    // 1) Bracket balancing (scan char by char, no string handling — there are no strings here in
    //    WGSL)
    let mut depth: i32 = 0;
    for (i, c) in text.char_indices() {
        match c {
            '(' | '{' | '[' => depth += 1,
            ')' | '}' | ']' => {
                depth -= 1;
                if depth < 0 {
                    return Err(crate::Error::NotEmittable(format!(
                        "WGSL structure error: an extra `)` at byte {i}"
                    )));
                }
            }
            _ => {}
        }
    }
    if depth != 0 {
        return Err(crate::Error::NotEmittable(format!(
            "WGSL structure error: parentheses unbalanced by {depth}"
        )));
    }
    // 2) WGSL has no `^` / `**` operator (powers must be written as multiplication or pow)
    if text.contains('^') || text.contains("**") {
        return Err(crate::Error::NotEmittable(
            "WGSL structure error: `^` or `**` encountered (WGSL has no power operator)"
                .to_string(),
        ));
    }
    // 3) Every float literal must carry the `f` suffix.
    //    **This is a style convention of this lane, not a WGSL requirement**: naga was measured to
    //    accept `return 2.5;` (abstract-float settles to f32 by context). The reason to insist on
    //    `f` is explicitness, independence from context, and avoiding being inferred to another
    //    width in an intermediate position whose type is not yet determined.
    let bytes: Vec<char> = text.chars().collect();
    let mut i = 0usize;
    while i < bytes.len() {
        if bytes[i].is_ascii_digit() {
            // Treat it as a literal start only when the preceding char is not an identifier char
            let prev = if i == 0 { ' ' } else { bytes[i - 1] };
            if prev.is_alphanumeric() || prev == '_' {
                i += 1;
                continue;
            }
            let start = i;
            while i < bytes.len() && (bytes[i].is_ascii_digit() || bytes[i] == '.') {
                i += 1;
            }
            let mut end = i;
            if end < bytes.len() && (bytes[end] == 'e' || bytes[end] == 'E') {
                end += 1;
                if end < bytes.len() && (bytes[end] == '+' || bytes[end] == '-') {
                    end += 1;
                }
                while end < bytes.len() && bytes[end].is_ascii_digit() {
                    end += 1;
                }
            }
            let is_float = bytes[start..end].contains(&'.') || end > i;
            if is_float && (end >= bytes.len() || bytes[end] != 'f') {
                let lit: String = bytes[start..end].iter().collect();
                return Err(crate::Error::NotEmittable(format!("WGSL structure error: float literal `{lit}` lacks the `f` suffix (it would be inferred as f64)")));
            }
            i = end;
            continue;
        }
        i += 1;
    }
    // 4) `inff` / `NaN` must not appear (this lane's counterpart of the non-finite constant gate
    //    in the Rust lane)
    for bad in ["inff", "NaNf", "inf ", "-inff"] {
        if text.contains(bad) {
            return Err(crate::Error::NotEmittable(format!(
                "WGSL structure error: non-finite constant `{bad}`"
            )));
        }
    }
    // 5) The function signature must be `-> f32` (this lane only promises f32 scalars)
    let mut fns = 0usize;
    let mut returns = 0usize;
    for line in text.lines() {
        let t = line.trim();
        if t.starts_with("fn ") {
            fns += 1;
            if !t.contains("-> f32") {
                return Err(crate::Error::NotEmittable(format!(
                    "WGSL structure error: the function signature is not `-> f32`: {t}"
                )));
            }
        }
        // 6) The function body must be a statement block: this lane emits exactly one
        //    `return <expr>;`.
        //    (WGSL has no expression function body — we once banned return the other way round,
        //    which was wrong.)
        if t.starts_with("return ") {
            returns += 1;
            if !t.ends_with(';') {
                return Err(crate::Error::NotEmittable(
                    "WGSL structure error: a `return` statement must end with `;`".to_string(),
                ));
            }
        }
    }
    if returns != 1 {
        return Err(crate::Error::NotEmittable(format!(
            "WGSL structure error: expected exactly one `return` statement, found {returns}"
        )));
    }
    if fns == 0 {
        return Err(crate::Error::NotEmittable(
            "WGSL structure error: no `fn` definition".to_string(),
        ));
    }
    Ok(())
}

/// Literal: WGSL needs the `f` suffix (`1.0f`) to know it is f32.
fn lit(v: f64) -> String {
    if super::math::fract(v) == 0.0 && v.abs() < 1e15 {
        format!("{v:.1}f")
    } else {
        format!("{v}f")
    }
}

fn wrap(s: String, p: u8, min: u8) -> String {
    if p < min {
        format!("({s})")
    } else {
        s
    }
}

/// Emit with precedence: the same precedence table as the Rust side, so the two sides can be
/// compared item by item.
fn wgsl_at(e: &crate::Expr, min: u8) -> Result<String, crate::Error> {
    let (text, p) = wgsl_raw(e)?;
    Ok(wrap(text, p, min))
}

/// Returns (text, precedence).
fn wgsl_raw(e: &crate::Expr) -> Result<(String, u8), crate::Error> {
    use crate::Expr as E;
    Ok(match e {
        E::Num(v) => (lit(*v), 9),
        // `j` is the reserved imaginary unit: the GPU lane has real numbers only, so reject it
        // explicitly (never silently treat it as a variable)
        E::Var(n) if n == "j" => {
            return Err(crate::Error::UnsupportedOperation(
                "the imaginary unit `j` is not in the WGSL lane (f32 reals only): use the CPU/complex lane".to_string(),
            ))
        }
        E::Var(n) => (wgsl_ident(n)?, 9),
        E::Neg(a) => (format!("-{}", wgsl_at(a, 5)?), 4),
        E::Add(a, b) => (format!("{} + {}", wgsl_at(a, 2)?, wgsl_at(b, 3)?), 2),
        E::Sub(a, b) => (format!("{} - {}", wgsl_at(a, 2)?, wgsl_at(b, 3)?), 2),
        E::Mul(a, b) => (format!("{} * {}", wgsl_at(a, 3)?, wgsl_at(b, 4)?), 3),
        E::Div(a, b) => (format!("{} / {}", wgsl_at(a, 3)?, wgsl_at(b, 4)?), 3),
        // Powers: small integers expand to multiplication (on the GPU pow is a transcendental
        // function; multiplication is both fast and accurate)
        E::Pow(a, b) => match b.as_ref() {
            E::Num(n) if *n == 2.0 => {
                let x = wgsl_at(a, 4)?;
                (format!("{x} * {x}"), 3)
            }
            E::Num(n) if *n == 3.0 => {
                let x = wgsl_at(a, 4)?;
                (format!("{x} * {x} * {x}"), 3)
            }
            E::Num(n) if *n == 0.5 => (format!("sqrt({})", wgsl_at(a, 0)?), 9),
            E::Num(n) if *n == -1.0 => (format!("1.0f / {}", wgsl_at(a, 4)?), 3),
            _ => {
                return Err(crate::Error::UnsupportedOperation("the WGSL lane supports only powers 2 and 3, square roots and -1 (write other exponents in a hand-written shader)".to_string()))
            }
        },
        E::Call(name, args) => {
            let mut vals = Vec::with_capacity(args.len());
            for a in args {
                vals.push(wgsl_at(a, 0)?);
            }
            let mapped = match name.as_str() {
                // Same name, same semantics
                "exp" | "sin" | "cos" | "tan" | "sinh" | "cosh" | "tanh" | "sqrt" | "abs"
                | "min" | "max" | "clamp" => name.clone(),
                // ln/log in this repo are both natural log → WGSL's log
                "ln" | "log" => "log".to_string(),
                other => {
                    return Err(crate::Error::UnsupportedOperation(format!("the WGSL lane does not support the function `{other}` (supported: exp/log/sqrt/sin/cos/tan/sinh/cosh/tanh/abs/min/max/clamp)")))
                }
            };
            (format!("{mapped}({})", vals.join(", ")), 9)
        }
        E::Mat(_) | E::MatPattern(_) => {
            return Err(crate::Error::UnsupportedOperation("the WGSL lane does not emit matrices (assemble them on the CPU side or write the shader by hand)".to_string()))
        }
        E::Transpose(_) => return Err(crate::Error::UnsupportedOperation("the WGSL lane does not support transpose".to_string())),
        E::Deriv(..) | E::Grad(_) | E::Laplacian(_) => {
            // Catalogued: the GPU lane cannot lower a symbolic differential operator.
            return Err(crate::Error::NotEmittable(
                "symbolic differential operators must be differentiated before WGSL emission"
                    .to_string(),
            ));
        }
        E::Sum { .. } | E::Integral { .. } => {
            return Err(crate::Error::UnsupportedOperation("sums and integrals need explicit loops in the WGSL lane: expand them first or write the shader by hand".to_string()))
        }
        E::Indexed { .. } => {
            return Err(crate::Error::UnsupportedOperation("indexed symbols need a tensor context (the WGSL lane accepts scalar formulas only)".to_string()))
        }
    })
}

#[cfg(test)]
mod tests {
    use crate::wgsl::validate_wgsl;
    use crate::{add, div, mul, neg, num, pow, var, Expr};

    #[test]
    fn validate_wgsl_pins_every_structure_rule() {
        // H-2: each emitter self-check gets a kernel text that violates exactly that rule —
        // before these pins, reverting a rule left every emission test green (only the staging
        // gate noticed the reverted construction, not its broken logic).
        let pin = |kernel: &str, fragment: &str| {
            let err = validate_wgsl(kernel).expect_err(kernel);
            assert_eq!(err.code(), "not-emittable", "{err:?}");
            let text = err.render(crate::i18n::Locale::En);
            assert!(text.contains(fragment), "{kernel:?} -> {text}");
        };
        // 1) an extra closing bracket
        pin("fn f() -> f32 { return 1.0f; })", "an extra `)`");
        // 1b) unbalanced opening bracket
        pin("fn f() -> f32 { return 1.0f;", "unbalanced");
        // 2) WGSL has no power operator
        pin("fn f() -> f32 { return 2.0f ^ 3.0f; }", "power operator");
        // 3) a float literal without the `f` suffix (plain and exponent forms)
        pin("fn f() -> f32 { return 2.5; }", "lacks the `f` suffix");
        pin("fn f() -> f32 { return 1e5; }", "lacks the `f` suffix");
        // 4) non-finite constants
        pin("fn f() -> f32 { return inff; }", "non-finite constant");
        // 5) the signature must promise `-> f32`
        pin("fn f() -> f64 { return 1.0f; }", "not `-> f32`");
        // 6) a `return` must end with `;` (the check is per-line: the statement starts a line)
        pin("fn f() -> f32 {\n    return 1.0f\n}", "must end with `;`");
        // 6b) exactly one `return` statement
        pin(
            "fn f() -> f32 { return 1.0f; return 2.0f; }",
            "exactly one `return`",
        );
        // 7) a `fn` definition must exist
        pin("return 1.0f;", "no `fn` definition");
    }

    use alloc::string::{String, ToString};
    use alloc::vec;
    use alloc::vec::Vec;

    fn two_refs() -> Vec<String> {
        vec!["a".to_string(), "b".to_string()]
    }

    #[test]
    fn scalar_kernel_has_f32_literals_and_signature() {
        // f = 0.5·a·b + 1
        let e = add(mul(mul(num(0.5), var("a")), var("b")), num(1.0));
        let w = super::emit_wgsl_with_params(&e, "f", &two_refs()).unwrap();
        assert!(w.contains("fn f(a: f32, b: f32) -> f32 {"), "{w}");
        assert!(w.contains("0.5f"), "{w}");
        assert!(w.contains("1.0f"), "{w}");
        // Brackets balanced (the minimum requirement of the structural gate)
        assert_eq!(w.matches('(').count(), w.matches(')').count(), "{w}");
    }

    #[test]
    fn powers_expand_to_multiplication() {
        let e = pow(var("a"), num(3.0));
        let w = super::emit_wgsl_with_params(&e, "cube", &["a".to_string()]).unwrap();
        assert!(
            w.contains("a * a * a"),
            "a cube must expand into multiplications: {w}"
        );
        // Other exponents are rejected explicitly (on the GPU pow is a transcendental function)
        let bad = pow(var("a"), num(4.5));
        assert!(super::emit_wgsl_with_params(&bad, "p", &["a".to_string()]).is_err());
    }

    #[test]
    fn function_name_mapping_and_rejections() {
        // ln → log (WGSL calls the natural logarithm log)
        let e = Expr::Call("ln".into(), vec![var("a")]);
        let w = super::emit_wgsl_with_params(&e, "l", &["a".to_string()]).unwrap();
        assert!(w.contains("log(a)"), "{w}");
        // Unsupported function: report an explicit error
        let bad = Expr::Call("arg".into(), vec![var("a")]);
        assert!(super::emit_wgsl_with_params(&bad, "x", &["a".to_string()]).is_err());
        // Matrix/complex/operator are rejected outright
        let mat = Expr::Mat(vec![vec![num(1.0)]]);
        assert!(super::emit_wgsl_with_params(&mat, "m", &[]).is_err());
    }

    #[test]
    fn keyword_collisions_are_rejected() {
        // `var` is a WGSL keyword: rename or hand-write, never generate a kernel that will not
        // compile
        let e = add(var("var"), num(1.0));
        let err = super::emit_wgsl_with_params(&e, "f", &["var".to_string()]).unwrap_err();
        // Catalogued: the typed variant carries both identifiers, the stable code is exposed,
        // and the English rendering names the collision.
        assert_eq!(err.code(), "wgsl-reserved-word", "{err}");
        match &err {
            crate::Error::WgslReservedWord { name, id } => {
                assert_eq!(name, "var");
                assert_eq!(id, "var");
            }
            other => panic!("expected WgslReservedWord, got {other:?}"),
        }
        let en = err.render(crate::i18n::Locale::En);
        assert!(en.contains("reserved word") && en.contains("`var`"), "{en}");
    }

    #[test]
    fn reserved_word_rendering_keeps_the_two_identifiers_in_order() {
        // `{0}` is the symbol, `{1}` the reserved word: distinct values make a swapped
        // template visible (the collision test above uses `var` for both).
        let e = crate::Error::WgslReservedWord {
            name: "x".into(),
            id: "fn".into(),
        };
        assert_eq!(e.arguments(), vec!["x".to_string(), "fn".to_string()]);
        assert_eq!(
            e.render(crate::i18n::Locale::En),
            "symbol `x` collides with the WGSL reserved word `fn`: rename it or write the shader by hand"
        );
    }

    #[test]
    fn precedence_matches_the_rust_lane() {
        // (a + b) * c must keep the parentheses, otherwise the meaning changes
        let e = mul(add(var("a"), var("b")), var("c"));
        let w =
            super::emit_wgsl_with_params(&e, "f", &["a".into(), "b".into(), "c".into()]).unwrap();
        assert!(w.contains("(a + b) * c"), "{w}");
        // a - (b - c) likewise
        let e = crate::sub(var("a"), crate::sub(var("b"), var("c")));
        let w =
            super::emit_wgsl_with_params(&e, "g", &["a".into(), "b".into(), "c".into()]).unwrap();
        assert!(w.contains("a - (b - c)"), "{w}");
        // The right operand of a division
        let e = div(var("a"), add(var("b"), var("c")));
        let w =
            super::emit_wgsl_with_params(&e, "h", &["a".into(), "b".into(), "c".into()]).unwrap();
        assert!(w.contains("a / (b + c)"), "{w}");
        // Negation
        let e = neg(mul(var("a"), var("b")));
        let w = super::emit_wgsl_with_params(&e, "n", &two_refs()).unwrap();
        assert!(w.contains("-(a * b)"), "{w}");
    }
}