From 2c4f9810327d58023e9ec44f579cd78adf56317b Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 1 Apr 2024 15:56:02 -0700 Subject: Allow bit operators on enum types. (#3862) * Allow bit operators on enum types. * Fix. --- tests/diagnostics/enum-implicit-conversion.slang | 30 ++++++++++++-------- .../enum-implicit-conversion.slang.expected | 26 ------------------ tests/language-feature/enums/enum-bit-ops.slang | 32 ++++++++++++++++++++++ .../ordinary-comment-hover-info.slang.expected.txt | 2 +- .../robustness-2.slang.expected.txt | 2 +- 5 files changed, 52 insertions(+), 40 deletions(-) delete mode 100644 tests/diagnostics/enum-implicit-conversion.slang.expected create mode 100644 tests/language-feature/enums/enum-bit-ops.slang (limited to 'tests') diff --git a/tests/diagnostics/enum-implicit-conversion.slang b/tests/diagnostics/enum-implicit-conversion.slang index fc4757f7e..51082183e 100644 --- a/tests/diagnostics/enum-implicit-conversion.slang +++ b/tests/diagnostics/enum-implicit-conversion.slang @@ -1,6 +1,6 @@ // enum-implicit-conversion.slang -//DIAGNOSTIC_TEST:SIMPLE: +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): // Confirm that suitable error messages are // generated for code that relies on implicit @@ -21,28 +21,34 @@ 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; - // TODO: explicit conversion to `enum` type should be allowed. -// Color cc = Color(val); - + // Implicit cast from enum to int types other than the tag type is not allowed. + // CHECK: ([[# @LINE+1]]): error + uint y = c; - // Implicit converion from `enum` to `int` isn't allowed. + // 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; - 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; } diff --git a/tests/diagnostics/enum-implicit-conversion.slang.expected b/tests/diagnostics/enum-implicit-conversion.slang.expected deleted file mode 100644 index 376dab7e6..000000000 --- a/tests/diagnostics/enum-implicit-conversion.slang.expected +++ /dev/null @@ -1,26 +0,0 @@ -result code = -1 -standard error = { -tests/diagnostics/enum-implicit-conversion.slang(27): error 30019: expected an expression of type 'Color', got 'int' - Color c = val; - ^~~ -tests/diagnostics/enum-implicit-conversion.slang(27): note: explicit conversion from 'int' to 'Color' is possible -tests/diagnostics/enum-implicit-conversion.slang(34): error 30019: expected an expression of type 'int', got 'Color' - int x = c; - ^ -tests/diagnostics/enum-implicit-conversion.slang(34): note: explicit conversion from 'Color' to 'int' is possible -tests/diagnostics/enum-implicit-conversion.slang(35): error 30019: expected an expression of type 'uint', got 'Color' - uint y = c; - ^ -tests/diagnostics/enum-implicit-conversion.slang(35): note: explicit conversion from 'Color' to 'uint' is possible -tests/diagnostics/enum-implicit-conversion.slang(42): error 39999: ambiguous call to 'foo' with arguments of type (Color) - int z = foo(c); - ^ -tests/diagnostics/enum-implicit-conversion.slang(18): note 39999: candidate: func foo(uint) -> int -int foo(uint x) { return x * 256 * 16; } - ^~~ -tests/diagnostics/enum-implicit-conversion.slang(17): note 39999: candidate: func foo(int) -> int -int foo(int x) { return x * 16; } - ^~~ -} -standard output = { -} diff --git a/tests/language-feature/enums/enum-bit-ops.slang b/tests/language-feature/enums/enum-bit-ops.slang new file mode 100644 index 000000000..abffe8dee --- /dev/null +++ b/tests/language-feature/enums/enum-bit-ops.slang @@ -0,0 +1,32 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj + +[Flags] +enum MyFlags +{ + Zero = 0, + BitOne, // = 1 + BitTwo, // = 2 + BitThree // = 4 +} + +bool test(MyFlags f1) +{ + return (f1 & MyFlags.BitTwo) != 0; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[numthreads(1, 1, 1)] +void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) +{ + int outVal = test(MyFlags.BitOne | MyFlags.BitTwo) ? 1 : 0; + // CHECK: 1 + outputBuffer[0] = outVal; + // CHECK: 4 + outputBuffer[1] = (int)MyFlags.BitThree; + // CHECK: 0 + outputBuffer[2] = test(MyFlags.BitOne | MyFlags.BitThree) ? 1 : 0; + // CHECK: 8 + outputBuffer[3] = MyFlags(8); +} diff --git a/tests/language-server/ordinary-comment-hover-info.slang.expected.txt b/tests/language-server/ordinary-comment-hover-info.slang.expected.txt index 72b83c696..8c3a10487 100644 --- a/tests/language-server/ordinary-comment-hover-info.slang.expected.txt +++ b/tests/language-server/ordinary-comment-hover-info.slang.expected.txt @@ -25,7 +25,7 @@ func main() -> void range: 19,3 - 19,8 content: ``` -E E.Green +E E.Green = 1 ``` #5 green color diff --git a/tests/language-server/robustness-2.slang.expected.txt b/tests/language-server/robustness-2.slang.expected.txt index 346f637c6..8e3b89c59 100644 --- a/tests/language-server/robustness-2.slang.expected.txt +++ b/tests/language-server/robustness-2.slang.expected.txt @@ -2,7 +2,7 @@ range: 14,4 - 14,7 content: ``` -Kind Kind.Foo +Kind Kind.Foo = 0 ``` HOVER:15,6 -- cgit v1.2.3