From 1c6e2413957007475e5145a1d837b05d88df06c2 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 10 Sep 2024 08:13:52 -0700 Subject: Disambiguate subscript decls by preferring the one with more accessors. (#5046) --- source/slang/slang-check-overload.cpp | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'source') diff --git a/source/slang/slang-check-overload.cpp b/source/slang/slang-check-overload.cpp index 9b3d6baff..3b36b270b 100644 --- a/source/slang/slang-check-overload.cpp +++ b/source/slang/slang-check-overload.cpp @@ -1199,6 +1199,8 @@ namespace Slang return parent; } + // Returns -1 if left is preferred, 1 if right is preferred, and 0 if they are equal. + // int SemanticsVisitor::CompareLookupResultItems( LookupResultItem const& left, LookupResultItem const& right) @@ -1283,6 +1285,45 @@ namespace Slang return -1; } + // If both are subscript decls, prefer the one that provides more + // accessors. + if (auto leftSubscriptDecl = left.declRef.as()) + { + if (auto rightSubscriptDecl = right.declRef.as()) + { + auto leftAccessorCount = leftSubscriptDecl.getDecl()->getMembersOfType().getCount(); + auto rightAccessorCount = rightSubscriptDecl.getDecl()->getMembersOfType().getCount(); + auto decl1IsSubsetOfDecl2 = [=](SubscriptDecl* decl1, SubscriptDecl* decl2) + { + for (auto accessorDecl1 : decl1->getMembersOfType()) + { + bool found = false; + for (auto accessorDecl2 : decl2->getMembersOfType()) + { + if (accessorDecl1->astNodeType == accessorDecl2->astNodeType) + { + found = true; + break; + } + } + if (!found) + return false; + } + return true; + }; + if (leftAccessorCount > rightAccessorCount + && decl1IsSubsetOfDecl2(rightSubscriptDecl.getDecl(), leftSubscriptDecl.getDecl())) + { + return -1; + } + else if (rightAccessorCount > leftAccessorCount + && decl1IsSubsetOfDecl2(leftSubscriptDecl.getDecl(), rightSubscriptDecl.getDecl())) + { + return 1; + } + } + } + // TODO: We should generalize above rules such that in a tie a declaration // A::m is better than B::m when all other factors are equal and // A inherits from B. -- cgit v1.2.3