summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-check-expr.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-08-07 08:10:02 -0700
committerGitHub <noreply@github.com>2025-08-07 15:10:02 +0000
commit7cd8130e1a3dbcca8746e0577fb8df3bf2975bf8 (patch)
tree6da2b411da34039c3d0ec0e06fadd0e13f8f4842 /source/slang/slang-check-expr.cpp
parent67a96920674d628f615532a302504544a45e8187 (diff)
Support `expand` on concrete tuple values. (#8106)
Closes #8061. Along with the fix, also enhanced coercion/overload resolution to filter candidates based on the target type, allowing `tests\language-feature\higher-order-functions\overloaded.slang` to pass.
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
-rw-r--r--source/slang/slang-check-expr.cpp30
1 files changed, 23 insertions, 7 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index 425548518..a874eaf43 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -1128,7 +1128,9 @@ LookupResult SemanticsVisitor::filterLookupResultByCheckedOptionalAndDiagnose(
return result;
}
-LookupResult SemanticsVisitor::resolveOverloadedLookup(LookupResult const& inResult)
+LookupResult SemanticsVisitor::resolveOverloadedLookup(
+ LookupResult const& inResult,
+ Type* targetType)
{
// If the result isn't actually overloaded, it is fine as-is
if (!inResult.isValid())
@@ -1140,6 +1142,15 @@ LookupResult SemanticsVisitor::resolveOverloadedLookup(LookupResult const& inRes
List<LookupResultItem> items;
for (auto item : inResult.items)
{
+ // First we check if the item is coercible to targetType.
+ // And skip if it doesn't.
+ if (targetType)
+ {
+ auto declType = GetTypeForDeclRef(item.declRef, SourceLoc());
+ if (!canCoerce(targetType, declType, nullptr, nullptr))
+ continue;
+ }
+
// For each item we consider adding, we will compare it
// to those items we've already added.
//
@@ -1232,6 +1243,7 @@ void SemanticsVisitor::diagnoseAmbiguousReference(Expr* expr)
Expr* SemanticsVisitor::_resolveOverloadedExprImpl(
OverloadedExpr* overloadedExpr,
LookupMask mask,
+ Type* targetType,
DiagnosticSink* diagSink)
{
auto lookupResult = overloadedExpr->lookupResult2;
@@ -1246,7 +1258,7 @@ Expr* SemanticsVisitor::_resolveOverloadedExprImpl(
lookupResult = refineLookup(lookupResult, mask);
// Try to filter out overload candidates based on which ones are "better" than one another.
- lookupResult = resolveOverloadedLookup(lookupResult);
+ lookupResult = resolveOverloadedLookup(lookupResult, targetType);
if (!lookupResult.isValid())
{
@@ -1296,6 +1308,7 @@ Expr* SemanticsVisitor::_resolveOverloadedExprImpl(
Expr* SemanticsVisitor::maybeResolveOverloadedExpr(
Expr* expr,
LookupMask mask,
+ Type* targetType,
DiagnosticSink* diagSink)
{
if (IsErrorExpr(expr))
@@ -1303,7 +1316,7 @@ Expr* SemanticsVisitor::maybeResolveOverloadedExpr(
if (auto overloadedExpr = as<OverloadedExpr>(expr))
{
- return _resolveOverloadedExprImpl(overloadedExpr, mask, diagSink);
+ return _resolveOverloadedExprImpl(overloadedExpr, mask, targetType, diagSink);
}
else
{
@@ -1311,9 +1324,12 @@ Expr* SemanticsVisitor::maybeResolveOverloadedExpr(
}
}
-Expr* SemanticsVisitor::resolveOverloadedExpr(OverloadedExpr* overloadedExpr, LookupMask mask)
+Expr* SemanticsVisitor::resolveOverloadedExpr(
+ OverloadedExpr* overloadedExpr,
+ Type* targetType,
+ LookupMask mask)
{
- return _resolveOverloadedExprImpl(overloadedExpr, mask, getSink());
+ return _resolveOverloadedExprImpl(overloadedExpr, mask, targetType, getSink());
}
Type* SemanticsVisitor::tryGetDifferentialType(ASTBuilder* builder, Type* type)
@@ -1364,7 +1380,7 @@ Type* SemanticsVisitor::tryGetDifferentialType(ASTBuilder* builder, Type* type)
Slang::LookupMask::type,
Slang::LookupOptions::None);
- diffTypeLookupResult = resolveOverloadedLookup(diffTypeLookupResult);
+ diffTypeLookupResult = resolveOverloadedLookup(diffTypeLookupResult, nullptr);
if (!diffTypeLookupResult.isValid())
{
@@ -4450,7 +4466,7 @@ Expr* SemanticsExprVisitor::visitEachExpr(EachExpr* expr)
{
goto error;
}
- if (!declRefType->getDeclRef().as<GenericTypePackParamDecl>())
+ if (!declRefType->getDeclRef().as<GenericTypePackParamDecl>() && !as<TupleType>(baseType))
{
goto error;
}