From 2a2f50fd63281e0eba5da16e955d0afc7634e9cd Mon Sep 17 00:00:00 2001 From: "T. Foley" Date: Wed, 26 May 2021 06:00:26 -0700 Subject: Fix a bug for enumerations with explicit "tag type" (#1856) The basic bug here was that `enum` types with an explicit tag type: enum Color : int32_t { ... } would have an `InheritanceDecl` implying that `Color` inherits from `int32_t`. The problem is that this is *not* actually an inheritance relationship, since a `Color` needs to be explicitly cast to/from an `int32_t`. Various parts of the compiler currently treat this case like real inheritance, and as a result the operations taht would apply to an `int32_t` end up applying to a `Color` as well. This particularly leads to an ambiguity between applying the `==` operator, because it has overloads for both the `__EnumType` and `__Builtin{something}` interfaces. The fix here is to explicitly exclude the `InheritanceDecl` that represents an enumeration tag type when considering declared subtype relationships. A more complete version of this fix would need to go through all places in the code where `InheritanceDecl`s are used and make sure that any places using them for true inheritnace relationships ignore those that represent an enumeration tag type. (An alternative option would be to use a distinct kind of `Decl` to represent the tag-type relationship, perhaps even going so far as to modifying the type of the relevant AST node as part of semantic checking) This change includes a regression test for the way this bug surfaced in user code. Co-authored-by: jsmall-nvidia --- .../enums/enum-tag-type-compare.slang | 47 ++++++++++++++++++++++ .../enums/enum-tag-type-compare.slang.expected.txt | 4 ++ 2 files changed, 51 insertions(+) create mode 100644 tests/language-feature/enums/enum-tag-type-compare.slang create mode 100644 tests/language-feature/enums/enum-tag-type-compare.slang.expected.txt (limited to 'tests') diff --git a/tests/language-feature/enums/enum-tag-type-compare.slang b/tests/language-feature/enums/enum-tag-type-compare.slang new file mode 100644 index 000000000..ce88860ee --- /dev/null +++ b/tests/language-feature/enums/enum-tag-type-compare.slang @@ -0,0 +1,47 @@ +// enum-tag-type-compare.slang + +// Test comparisons for `enum`s with explicit tag types + +//TEST(compute):COMPARE_COMPUTE: + +enum class MaterialType : uint32_t +{ + A, + B, + C, + D, +} + +struct Material +{ + MaterialType getType() { return MaterialType(_type); } + + uint _type; +} + +uint test(uint val) +{ + Material m = { val }; + + MaterialType b = MaterialType.B; + + int result = 0x100000; + if(m.getType() == b) result += 0x10; + if(m.getType() != b) result += 0x100; + if((uint)m.getType() == (uint)b) result += 0x1000; + if((uint)m.getType() != (uint)b) result += 0x10000; + + return result; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + uint inVal = tid; + uint outVal = test(inVal); + outputBuffer[tid] = outVal; +} diff --git a/tests/language-feature/enums/enum-tag-type-compare.slang.expected.txt b/tests/language-feature/enums/enum-tag-type-compare.slang.expected.txt new file mode 100644 index 000000000..2d91bbbdd --- /dev/null +++ b/tests/language-feature/enums/enum-tag-type-compare.slang.expected.txt @@ -0,0 +1,4 @@ +110100 +101010 +110100 +110100 -- cgit v1.2.3