summaryrefslogtreecommitdiffstats
path: root/tests/diagnostics/enum-implicit-conversion.slang
blob: 51082183efe71491aa1cfe23ee9222e4cea14744 (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
49
50
51
52
53
54
// enum-implicit-conversion.slang

//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):

// 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 baz(Color x) { return (int)x; }
int test(int val)
{
    // Implicit conversion from `int` to `enum` isn't allowed.
    // CHECK: ([[# @LINE+1]]): error
    Color c = val;

    // Implicit cast from enum to int types other than the tag type is not allowed.
    // CHECK: ([[# @LINE+1]]): error
    uint y = c;

    // Call that expects implicit conversion from int to enum shouldn't be allowed.
    // CHECK: ([[# @LINE+1]]): error
    int z = baz(5);
    
    // CHECK-NOT: error
    
    // Call that has an explicit overload on `enum` type should succeed.
    int zz = bar(c);
    
	Color cc = Color(val);
    
    // Implicit converion from `enum` to `int` is allowed.
	int  x = c;

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

    return x + y + z;
}