yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeAllow bit operators on enum types. (#3862)2c4f98103

master
1.3 KiB54 linesraw
1// enum-implicit-conversion.slang
2
3//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
4
5// Confirm that suitable error messages are
6// generated for code that relies on implicit
7// conversion of integers to/from `enum` types.
8
9enum Color
10{
11    Red,
12    Green,
13    Blue,
14    Alpha,
15}
16
17int foo(int  x) { return x * 16; }
18int foo(uint x) { return x * 256 * 16; }
19
20int bar(Color x) { return int(x) * 256; }
21int bar(int   x) { return x * 256 * 256; }
22int bar(uint  x) { return x * 256 * 256 * 16; }
23
24int baz(Color x) { return (int)x; }
25int test(int val)
26{
27    // Implicit conversion from `int` to `enum` isn't allowed.
28    // CHECK: ([[# @LINE+1]]): error
29    Color c = val;
30
31    // Implicit cast from enum to int types other than the tag type is not allowed.
32    // CHECK: ([[# @LINE+1]]): error
33    uint y = c;
34
35    // Call that expects implicit conversion from int to enum shouldn't be allowed.
36    // CHECK: ([[# @LINE+1]]): error
37    int z = baz(5);
38    
39    // CHECK-NOT: error
40    
41    // Call that has an explicit overload on `enum` type should succeed.
42    int zz = bar(c);
43    
44	Color cc = Color(val);
45    
46    // Implicit converion from `enum` to `int` is allowed.
47	int  x = c;
48
49    // Explicit converion is allowed.
50    int  xx = int(c);
51    uint yy = uint(c);
52
53    return x + y + z;
54}