Debian Codegraph @

third_party/rust/naga/src/front/wgsl/tests.rs

raw
1 use alloc::format;
2
3 use super::parse_str;
4
5 #[test]
6 fn parse_comment() {
7 parse_str(
8 "//
9 ////
10 ///////////////////////////////////////////////////////// asda
11 //////////////////// dad ////////// /
12 /////////////////////////////////////////////////////////////////////////////////////////////////////
13 //
14 ",
15 )
16 .unwrap();
17 }
18
19 #[test]
20 fn parse_types() {
21 parse_str("const a : i32 = 2;").unwrap();
22 parse_str("const a : u64 = 2lu;").unwrap();
23 assert!(parse_str("const a : x32 = 2;").is_err());
24 parse_str("var t: texture_2d<f32>;").unwrap();
25 parse_str("var t: texture_cube_array<i32>;").unwrap();
26 parse_str("var t: texture_multisampled_2d<u32>;").unwrap();
27 parse_str("var t: texture_storage_1d<rgba8uint,write>;").unwrap();
28 parse_str("var t: texture_storage_3d<r32float,read>;").unwrap();
29 }
30
31 #[test]
32 fn parse_type_inference() {
33 parse_str(
34 "
35 fn foo() {
36 let a = 2u;
37 let b: u32 = a;
38 var x = 3.;
39 var y = vec2<f32>(1, 2);
40 }",
41 )
42 .unwrap();
43 assert!(parse_str(
44 "
45 fn foo() { let c : i32 = 2.0; }",
46 )
47 .is_err());
48 }
49
50 #[test]
51 fn parse_type_cast() {
52 parse_str(
53 "
54 const a : i32 = 2;
55 fn main() {
56 var x: f32 = f32(a);
57 x = f32(i32(a + 1) / 2);
58 }
59 ",
60 )
61 .unwrap();
62 parse_str(
63 "
64 fn main() {
65 let x: vec2<f32> = vec2<f32>(1.0, 2.0);
66 let y: vec2<u32> = vec2<u32>(x);
67 }
68 ",
69 )
70 .unwrap();
71 parse_str(
72 "
73 fn main() {
74 let x: vec2<f32> = vec2<f32>(0.0);
75 }
76 ",
77 )
78 .unwrap();
79 assert!(parse_str(
80 "
81 fn main() {
82 let x: vec2<i32> = vec2<i32>(0.0, 0.0);
83 }
84 ",
85 )
86 .is_err());
87 }
88
89 #[test]
90 fn parse_type_coercion() {
91 parse_str(
92 "
93 fn foo(bar: f32) {}
94 fn main() {
95 foo(0);
96 }
97 ",
98 )
99 .unwrap();
100 assert!(parse_str(
101 "
102 fn foo(bar: i32) {}
103 fn main() {
104 foo(0.0);
105 }
106 ",
107 )
108 .is_err());
109 }
110
111 #[test]
112 fn parse_struct() {
113 parse_str(
114 "
115 struct Foo { x: i32 }
116 struct Bar {
117 @size(16) x: vec2<i32>,
118 @align(16) y: f32,
119 @size(32) @align(128) z: vec3<f32>,
120 };
121 struct Empty {}
122 var<storage,read_write> s: Foo;
123 ",
124 )
125 .unwrap();
126 }
127
128 #[test]
129 fn parse_standard_fun() {
130 parse_str(
131 "
132 fn main() {
133 var x: i32 = min(max(1, 2), 3);
134 }
135 ",
136 )
137 .unwrap();
138 }
139
140 #[test]
141 fn parse_statement() {
142 parse_str(
143 "
144 fn main() {
145 ;
146 {}
147 {;}
148 }
149 ",
150 )
151 .unwrap();
152
153 parse_str(
154 "
155 fn foo() {}
156 fn bar() { foo(); }
157 ",
158 )
159 .unwrap();
160 }
161
162 #[test]
163 fn parse_if() {
164 parse_str(
165 "
166 fn main() {
167 if true {
168 discard;
169 } else {}
170 if 0 != 1 {}
171 if false {
172 return;
173 } else if true {
174 return;
175 } else {}
176 }
177 ",
178 )
179 .unwrap();
180 }
181
182 #[test]
183 fn parse_parentheses_if() {
184 parse_str(
185 "
186 fn main() {
187 if (true) {
188 discard;
189 } else {}
190 if (0 != 1) {}
191 if (false) {
192 return;
193 } else if (true) {
194 return;
195 } else {}
196 }
197 ",
198 )
199 .unwrap();
200 }
201
202 #[test]
203 fn parse_loop() {
204 parse_str(
205 "
206 fn main() {
207 var i: i32 = 0;
208 loop {
209 if i == 1 { break; }
210 continuing { i = 1; }
211 }
212 loop {
213 if i == 0 { continue; }
214 break;
215 }
216 }
217 ",
218 )
219 .unwrap();
220 parse_str(
221 "
222 fn main() {
223 var found: bool = false;
224 var i: i32 = 0;
225 while !found {
226 if i == 10 {
227 found = true;
228 }
229
230 i = i + 1;
231 }
232 }
233 ",
234 )
235 .unwrap();
236 parse_str(
237 "
238 fn main() {
239 while true {
240 break;
241 }
242 }
243 ",
244 )
245 .unwrap();
246 parse_str(
247 "
248 fn main() {
249 var a: i32 = 0;
250 for(var i: i32 = 0; i < 4; i = i + 1) {
251 a = a + 2;
252 }
253 }
254 ",
255 )
256 .unwrap();
257 parse_str(
258 "
259 fn main() {
260 for(;;) {
261 break;
262 }
263 }
264 ",
265 )
266 .unwrap();
267 }
268
269 #[test]
270 fn parse_switch() {
271 parse_str(
272 "
273 fn main() {
274 var pos: f32;
275 switch (3) {
276 case 0, 1: { pos = 0.0; }
277 case 2: { pos = 1.0; }
278 default: { pos = 3.0; }
279 }
280 }
281 ",
282 )
283 .unwrap();
284 }
285
286 #[test]
287 fn parse_switch_optional_colon_in_case() {
288 parse_str(
289 "
290 fn main() {
291 var pos: f32;
292 switch (3) {
293 case 0, 1 { pos = 0.0; }
294 case 2 { pos = 1.0; }
295 default { pos = 3.0; }
296 }
297 }
298 ",
299 )
300 .unwrap();
301 }
302
303 #[test]
304 fn parse_switch_default_in_case() {
305 parse_str(
306 "
307 fn main() {
308 var pos: f32;
309 switch (3) {
310 case 0, 1: { pos = 0.0; }
311 case 2: {}
312 case default, 3: { pos = 3.0; }
313 }
314 }
315 ",
316 )
317 .unwrap();
318 }
319
320 #[test]
321 fn parse_parentheses_switch() {
322 parse_str(
323 "
324 fn main() {
325 var pos: i32;
326 switch pos + 1 {
327 default: { pos = 3; }
328 }
329 }
330 ",
331 )
332 .unwrap();
333 }
334
335 #[test]
336 fn parse_texture_load() {
337 parse_str(
338 "
339 var t: texture_3d<u32>;
340 fn foo() {
341 let r: vec4<u32> = textureLoad(t, vec3<u32>(0u, 1u, 2u), 1);
342 }
343 ",
344 )
345 .unwrap();
346 parse_str(
347 "
348 var t: texture_multisampled_2d_array<i32>;
349 fn foo() {
350 let r: vec4<i32> = textureLoad(t, vec2<i32>(10, 20), 2, 3);
351 }
352 ",
353 )
354 .unwrap();
355 parse_str(
356 "
357 var t: texture_storage_1d_array<r32float,read>;
358 fn foo() {
359 let r: vec4<f32> = textureLoad(t, 10, 2);
360 }
361 ",
362 )
363 .unwrap();
364 }
365
366 #[test]
367 fn parse_texture_store() {
368 parse_str(
369 "
370 var t: texture_storage_2d<rgba8unorm,write>;
371 fn foo() {
372 textureStore(t, vec2<i32>(10, 20), vec4<f32>(0.0, 1.0, 2.0, 3.0));
373 }
374 ",
375 )
376 .unwrap();
377 }
378
379 #[test]
380 fn parse_texture_query() {
381 parse_str(
382 "
383 var t: texture_multisampled_2d_array<f32>;
384 fn foo() {
385 var dim: vec2<u32> = textureDimensions(t);
386 dim = textureDimensions(t, 0);
387 let layers: u32 = textureNumLayers(t);
388 let samples: u32 = textureNumSamples(t);
389 }
390 ",
391 )
392 .unwrap();
393 }
394
395 #[test]
396 fn parse_postfix() {
397 parse_str(
398 "fn foo() {
399 let x: f32 = vec4<f32>(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g;
400 let y: f32 = fract(vec2<f32>(0.5, x)).x;
401 }",
402 )
403 .unwrap();
404
405 let err = parse_str(
406 "fn foo() {
407 let v = mat4x4<f32>().x;
408 }",
409 )
410 .unwrap_err();
411 assert_eq!(err.message(), "invalid field accessor `x`");
412 }
413
414 #[test]
415 fn parse_expressions() {
416 parse_str("fn foo() {
417 let x: f32 = select(0.0, 1.0, true);
418 let y: vec2<f32> = select(vec2<f32>(1.0, 1.0), vec2<f32>(x, x), vec2<bool>(x < 0.5, x > 0.5));
419 let z: bool = !(0.0 == 1.0);
420 }").unwrap();
421 }
422
423 #[test]
424 fn parse_assignment_statements() {
425 parse_str(
426 "
427 struct Foo { x: i32 };
428
429 fn foo() {
430 var x: u32 = 0u;
431 x++;
432 x--;
433 x = 1u;
434 x += 1u;
435 var v: vec2<f32> = vec2<f32>(1.0, 1.0);
436 v[0] += 1.0;
437 (v)[0] += 1.0;
438 var s: Foo = Foo(0);
439 s.x -= 1;
440 (s.x) -= 1;
441 (s).x -= 1;
442 _ = 5u;
443 }",
444 )
445 .unwrap();
446
447 let error = parse_str(
448 "fn foo() {
449 x|x++;
450 }",
451 )
452 .unwrap_err();
453 assert_eq!(
454 error.message(),
455 "expected assignment or increment/decrement, found \"|\"",
456 );
457 }
458
459 #[test]
460 fn binary_expression_mixed_scalar_and_vector_operands() {
461 for (operand, expect_splat) in [
462 ('<', false),
463 ('>', false),
464 ('&', false),
465 ('|', false),
466 ('+', true),
467 ('-', true),
468 ('*', false),
469 ('/', true),
470 ('%', true),
471 ] {
472 let module = parse_str(&format!(
473 "
474 @fragment
475 fn main(@location(0) some_vec: vec3<f32>) -> @location(0) vec4<f32> {{
476 if (all(1.0 {operand} some_vec)) {{
477 return vec4(0.0);
478 }}
479 return vec4(1.0);
480 }}
481 "
482 ))
483 .unwrap();
484
485 let expressions = &&module.entry_points[0].function.expressions;
486
487 let found_expressions = expressions
488 .iter()
489 .filter(|&(_, e)| {
490 if let crate::Expression::Binary { left, .. } = *e {
491 matches!(
492 (expect_splat, &expressions[left]),
493 (false, &crate::Expression::Literal(crate::Literal::F32(..)))
494 | (true, &crate::Expression::Splat { .. })
495 )
496 } else {
497 false
498 }
499 })
500 .count();
501
502 assert_eq!(
503 found_expressions,
504 1,
505 "expected `{operand}` expression {} splat",
506 if expect_splat { "with" } else { "without" }
507 );
508 }
509
510 let module = parse_str(
511 "@fragment
512 fn main(mat: mat3x3<f32>) {
513 let vec = vec3<f32>(1.0, 1.0, 1.0);
514 let result = mat / vec;
515 }",
516 )
517 .unwrap();
518 let expressions = &&module.entry_points[0].function.expressions;
519 let found_splat = expressions.iter().any(|(_, e)| {
520 if let crate::Expression::Binary { left, .. } = *e {
521 matches!(&expressions[left], &crate::Expression::Splat { .. })
522 } else {
523 false
524 }
525 });
526 assert!(!found_splat, "'mat / vec' should not be splatted");
527 }
528
529 #[test]
530 fn parse_pointers() {
531 parse_str(
532 "fn foo(a: ptr<function, f32>) -> f32 { return *a; }
533 fn bar() {
534 var x: f32 = 1.0;
535 let px = &x;
536 let py = foo(px);
537 }",
538 )
539 .unwrap();
540 }
541
542 #[test]
543 fn parse_struct_instantiation() {
544 parse_str(
545 "
546 struct Foo {
547 a: f32,
548 b: vec3<f32>,
549 }
550
551 @fragment
552 fn fs_main() {
553 var foo: Foo = Foo(0.0, vec3<f32>(0.0, 1.0, 42.0));
554 }
555 ",
556 )
557 .unwrap();
558 }
559
560 #[test]
561 fn parse_array_length() {
562 parse_str(
563 "
564 struct Foo {
565 data: array<u32>
566 } // this is used as both input and output for convenience
567
568 @group(0) @binding(0)
569 var<storage> foo: Foo;
570
571 @group(0) @binding(1)
572 var<storage> bar: array<u32>;
573
574 fn baz() {
575 var x: u32 = arrayLength(foo.data);
576 var y: u32 = arrayLength(bar);
577 }
578 ",
579 )
580 .unwrap();
581 }
582
583 #[test]
584 fn parse_storage_buffers() {
585 parse_str(
586 "
587 @group(0) @binding(0)
588 var<storage> foo: array<u32>;
589 ",
590 )
591 .unwrap();
592 parse_str(
593 "
594 @group(0) @binding(0)
595 var<storage,read> foo: array<u32>;
596 ",
597 )
598 .unwrap();
599 parse_str(
600 "
601 @group(0) @binding(0)
602 var<storage,write> foo: array<u32>;
603 ",
604 )
605 .unwrap();
606 parse_str(
607 "
608 @group(0) @binding(0)
609 var<storage,read_write> foo: array<u32>;
610 ",
611 )
612 .unwrap();
613 }
614
615 #[test]
616 fn parse_alias() {
617 parse_str(
618 "
619 alias Vec4 = vec4<f32>;
620 ",
621 )
622 .unwrap();
623 }
624
625 #[test]
626 fn parse_texture_load_store_expecting_four_args() {
627 for (func, texture) in [
628 (
629 "textureStore",
630 "texture_storage_2d_array<rg11b10float, write>",
631 ),
632 ("textureLoad", "texture_2d_array<i32>"),
633 ] {
634 let error = parse_str(&format!(
635 "
636 @group(0) @binding(0) var tex_los_res: {texture};
637 @compute
638 @workgroup_size(1)
639 fn main(@builtin(global_invocation_id) id: vec3<u32>) {{
640 var color = vec4(1, 1, 1, 1);
641 {func}(tex_los_res, id, color);
642 }}
643 "
644 ))
645 .unwrap_err();
646 assert_eq!(
647 error.message(),
648 "wrong number of arguments: expected 4, found 3"
649 );
650 }
651 }
652
653 #[test]
654 fn parse_repeated_attributes() {
655 use crate::{
656 front::wgsl::{error::Error, Frontend},
657 Span,
658 };
659
660 let template_vs = "@vertex fn vs() -> __REPLACE__ vec4<f32> { return vec4<f32>(0.0); }";
661 let template_struct = "struct A { __REPLACE__ data: vec3<f32> }";
662 let template_resource = "__REPLACE__ var tex_los_res: texture_2d_array<i32>;";
663 let template_stage = "__REPLACE__ fn vs() -> vec4<f32> { return vec4<f32>(0.0); }";
664 for (attribute, template) in [
665 ("align(16)", template_struct),
666 ("binding(0)", template_resource),
667 ("builtin(position)", template_vs),
668 ("compute", template_stage),
669 ("fragment", template_stage),
670 ("group(0)", template_resource),
671 ("interpolate(flat)", template_vs),
672 ("invariant", template_vs),
673 ("location(0)", template_vs),
674 ("size(16)", template_struct),
675 ("vertex", template_stage),
676 ("early_depth_test(less_equal)", template_resource),
677 ("workgroup_size(1)", template_stage),
678 ] {
679 let shader = template.replace("__REPLACE__", &format!("@{attribute} @{attribute}"));
680 let name_length = attribute.rfind('(').unwrap_or(attribute.len()) as u32;
681 let span_start = shader.rfind(attribute).unwrap() as u32;
682 let span_end = span_start + name_length;
683 let expected_span = Span::new(span_start, span_end);
684
685 let result = Frontend::new().inner(&shader);
686 assert!(matches!(
687 *result.unwrap_err(),
688 Error::RepeatedAttribute(span) if span == expected_span
689 ));
690 }
691 }
692
693 #[test]
694 fn parse_missing_workgroup_size() {
695 use crate::{
696 front::wgsl::{error::Error, Frontend},
697 Span,
698 };
699
700 let shader = "@compute fn vs() -> vec4<f32> { return vec4<f32>(0.0); }";
701 let result = Frontend::new().inner(shader);
702 assert!(matches!(
703 *result.unwrap_err(),
704 Error::MissingWorkgroupSize(span) if span == Span::new(1, 8)
705 ));
706 }
707
708 mod diagnostic_filter {
709 use crate::front::wgsl::assert_parse_err;
710
711 #[test]
712 fn intended_global_directive() {
713 let shader = "@diagnostic(off, my.lint);";
714 assert_parse_err(
715 shader,
716 "\
717 error: `@diagnostic(…)` attribute(s) on semicolons are not supported
718 ┌─ wgsl:1:1
719 │
720 1 │ @diagnostic(off, my.lint);
721 │ ^^^^^^^^^^^^^^^^^^^^^^^^^
722 │
723 = note: `@diagnostic(…)` attributes are only permitted on `fn`s, some statements, and `switch`/`loop` bodies.
724 = note: If you meant to declare a diagnostic filter that applies to the entire module, move this line to the top of the file and remove the `@` symbol.
725
726 "
727 );
728 }
729
730 mod parse_sites_not_yet_supported {
731 use crate::front::wgsl::assert_parse_err;
732
733 #[test]
734 fn user_rules() {
735 let shader = "
736 fn myfunc() {
737 if (true) @diagnostic(off, my.lint) {
738 // ^^^^^^^^^^^^^^^^^^^^^^^^^ not yet supported, should report an error
739 }
740 }
741 ";
742 assert_parse_err(shader, "\
743 error: `@diagnostic(…)` attribute(s) not yet implemented
744 ┌─ wgsl:3:15
745 │
746 3 │ if (true) @diagnostic(off, my.lint) {
747 │ ^^^^^^^^^^^^^^^^^^^^^^^^^ can't use this on compound statements (yet)
748 │
749 = note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5320>, so they can prioritize it!
750
751 ");
752 }
753
754 #[test]
755 fn unknown_rules() {
756 let shader = "
757 fn myfunc() {
758 if (true) @diagnostic(off, wat_is_this) {
759 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ should emit a warning
760 }
761 }
762 ";
763 assert_parse_err(shader, "\
764 error: `@diagnostic(…)` attribute(s) not yet implemented
765 ┌─ wgsl:3:12
766 │
767 3 │ if (true) @diagnostic(off, wat_is_this) {
768 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't use this on compound statements (yet)
769 │
770 = note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5320>, so they can prioritize it!
771
772 ");
773 }
774 }
775
776 mod directive_conflict {
777 use crate::front::wgsl::assert_parse_err;
778
779 #[test]
780 fn user_rules() {
781 let shader = "
782 diagnostic(off, my.lint);
783 diagnostic(warning, my.lint);
784 ";
785 assert_parse_err(shader, "\
786 error: found conflicting `diagnostic(…)` rule(s)
787 ┌─ wgsl:2:1
788 │
789 2 │ diagnostic(off, my.lint);
790 │ ^^^^^^^^^^^^^^^^^^^^^^^^ first rule
791 3 │ diagnostic(warning, my.lint);
792 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second rule
793 │
794 = note: Multiple `diagnostic(…)` rules with the same rule name conflict unless they are directives and the severity is the same.
795 = note: You should delete the rule you don't want.
796
797 ");
798 }
799
800 #[test]
801 fn unknown_rules() {
802 let shader = "
803 diagnostic(off, wat_is_this);
804 diagnostic(warning, wat_is_this);
805 ";
806 assert_parse_err(shader, "\
807 error: found conflicting `diagnostic(…)` rule(s)
808 ┌─ wgsl:2:1
809 │
810 2 │ diagnostic(off, wat_is_this);
811 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first rule
812 3 │ diagnostic(warning, wat_is_this);
813 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second rule
814 │
815 = note: Multiple `diagnostic(…)` rules with the same rule name conflict unless they are directives and the severity is the same.
816 = note: You should delete the rule you don't want.
817
818 ");
819 }
820 }
821
822 mod attribute_conflict {
823 use crate::front::wgsl::assert_parse_err;
824
825 #[test]
826 fn user_rules() {
827 let shader = "
828 diagnostic(off, my.lint);
829 diagnostic(warning, my.lint);
830 ";
831 assert_parse_err(shader, "\
832 error: found conflicting `diagnostic(…)` rule(s)
833 ┌─ wgsl:2:1
834 │
835 2 │ diagnostic(off, my.lint);
836 │ ^^^^^^^^^^^^^^^^^^^^^^^^ first rule
837 3 │ diagnostic(warning, my.lint);
838 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second rule
839 │
840 = note: Multiple `diagnostic(…)` rules with the same rule name conflict unless they are directives and the severity is the same.
841 = note: You should delete the rule you don't want.
842
843 ");
844 }
845
846 #[test]
847 fn unknown_rules() {
848 let shader = "
849 diagnostic(off, wat_is_this);
850 diagnostic(warning, wat_is_this);
851 ";
852 assert_parse_err(shader, "\
853 error: found conflicting `diagnostic(…)` rule(s)
854 ┌─ wgsl:2:1
855 │
856 2 │ diagnostic(off, wat_is_this);
857 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first rule
858 3 │ diagnostic(warning, wat_is_this);
859 │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ second rule
860 │
861 = note: Multiple `diagnostic(…)` rules with the same rule name conflict unless they are directives and the severity is the same.
862 = note: You should delete the rule you don't want.
863
864 ");
865 }
866 }
867 }