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 --- source/slang/slang-check-conformance.cpp | 14 ++++++++++++++ source/slang/slang-syntax.h | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/slang/slang-check-conformance.cpp b/source/slang/slang-check-conformance.cpp index 174d4be89..b95df520d 100644 --- a/source/slang/slang-check-conformance.cpp +++ b/source/slang/slang-check-conformance.cpp @@ -211,6 +211,20 @@ namespace Slang auto inheritedType = getBaseType(m_astBuilder, inheritanceDeclRef); + + // There's one annoying corner case where something that *looks* like an inheritnace + // declaration isn't actually one, and that is when an `enum` type includes an explicit + // declaration of its "tag type." + // + if (auto enumDeclRef = declRef.as()) + { + if (inheritedType->equals(getTagType(m_astBuilder, enumDeclRef))) + { + return; + } + } + + // We need to ensure that the witness that gets created // is a composite one, reflecting lookup through // the inheritance declaration. diff --git a/source/slang/slang-syntax.h b/source/slang/slang-syntax.h index 9589f00fd..a23ad224e 100644 --- a/source/slang/slang-syntax.h +++ b/source/slang/slang-syntax.h @@ -136,7 +136,10 @@ namespace Slang return getMembersOfType(declRef, filterStyle); } - + inline Type* getTagType(ASTBuilder* astBuilder, DeclRef const& declRef) + { + return declRef.substitute(astBuilder, declRef.getDecl()->tagType); + } inline Type* getBaseType(ASTBuilder* astBuilder, DeclRef const& declRef) { -- cgit v1.2.3