summaryrefslogtreecommitdiffstats
path: root/tests/diagnostics/enum-implicit-conversion.slang
blob: fc4757f7e40142bbe163ee5f3968a7245346d6f3 (plain)
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
// enum-implicit-conversion.slang

//DIAGNOSTIC_TEST:SIMPLE:

// Confirm that suitable error messages are
// generated for code that relies on implicit
// conversion of integers to/from `enum` types.

enum Color
{
    Red,
    Green,
    Blue,
    Alpha,
}

int foo(int  x) { return x * 16; }
int foo(uint x) { return x * 256 * 16; }

int bar(Color x) { return int(x) * 256; }
int bar(int   x) { return x * 256 * 256; }
int bar(uint  x) { return x * 256 * 256 * 16; }

int test(int val)
{
    // Implicit conversion from `int` to `enum` isn't allowed.
    Color c = val;

    // TODO: explicit conversion to `enum` type should be allowed.
//	Color cc = Color(val);


    // Implicit converion from `enum` to `int` isn't allowed.
	int  x = c;
    uint y = c;

    // Explicit converion is allowed.
    int  xx = int(c);
    uint yy = uint(c);

    // Call that expects implicit conversion should fail.
    int z = foo(c);

    // Call that has an explicit overload on `enum` type should succeed.
    int zz = bar(c);

    return x + y + z;
}