diff options
| author | Copilot <198982749+Copilot@users.noreply.github.com> | 2025-07-18 23:01:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-18 23:01:55 +0000 |
| commit | 4d514f792fc590e28579a184be8890613d0fdf9e (patch) | |
| tree | 5d069f7896ee05a51a3745bc34b3c4e1893efcfc /source | |
| parent | 3df9fe8a8a1aa01db2651eacd1d8bec50d0ee90c (diff) | |
Fix enum array indexing by adding implicit conversion support (#7815)
* Initial plan
* Fix enum array indexing by adding implicit conversion support
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
* Update enum array indexing test to support GPU backends
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 10 | ||||
| -rw-r--r-- | source/slang/slang-check-expr.cpp | 9 | ||||
| -rw-r--r-- | source/slang/slang-check-impl.h | 6 |
3 files changed, 19 insertions, 6 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 1dc230dae..806912abe 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -8485,6 +8485,16 @@ bool SemanticsVisitor::isScalarIntegerType(Type* type) return isIntegerBaseType(baseType) || baseType == BaseType::Bool; } +Type* SemanticsVisitor::getMatchingIntType(Type* type) +{ + if (isScalarIntegerType(type)) + return type; + if (auto enumTypeDecl = isDeclRefTypeOf<EnumDecl>(type)) + if (enumTypeDecl.getDecl()->tagType) + return getMatchingIntType(enumTypeDecl.getDecl()->tagType); + return m_astBuilder->getIntType(); +} + bool SemanticsVisitor::isHalfType(Type* type) { auto basicType = as<BasicExpressionType>(type); diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 41c3bd510..2891c316f 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -2490,13 +2490,10 @@ Expr* SemanticsVisitor::CheckSimpleSubscriptExpr(IndexExpr* subscriptExpr, Type* { expr = CheckExpr(expr); } - auto indexExpr = subscriptExpr->indexExprs[0]; + auto& indexExpr = subscriptExpr->indexExprs[0]; - if (!isScalarIntegerType(indexExpr->type.type)) - { - getSink()->diagnose(indexExpr, Diagnostics::subscriptIndexNonInteger); - return CreateErrorExpr(subscriptExpr); - } + auto intTargetType = getMatchingIntType(indexExpr->type.type); + indexExpr = coerce(CoercionSite::Argument, intTargetType, indexExpr, getSink()); subscriptExpr->type = QualType(elementType); diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h index 86d0b42fe..5c6be2665 100644 --- a/source/slang/slang-check-impl.h +++ b/source/slang/slang-check-impl.h @@ -2139,6 +2139,12 @@ public: /// Is `type` a scalar integer type. bool isScalarIntegerType(Type* type); + // This function is used to get the best integer type that matches the given type. + // If `type` is already an integer type, return it as is. + // If `type` is a enum type, return the tag type if it exists. + // Otherwise, return the 32-bit signed integer type. + Type* getMatchingIntType(Type* type); + /// Is `type` a scalar half type. bool isHalfType(Type* type); |
