diff options
| author | Yong He <yonghe@outlook.com> | 2024-09-10 08:13:52 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-10 08:13:52 -0700 |
| commit | 1c6e2413957007475e5145a1d837b05d88df06c2 (patch) | |
| tree | 95a53229e22afbf79f65b194aa23086c5dd411b7 /source | |
| parent | 936c22a9a938744eb43c310dd82c9c6944f79e87 (diff) | |
Disambiguate subscript decls by preferring the one with more accessors. (#5046)
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-check-overload.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
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<SubscriptDecl>()) + { + if (auto rightSubscriptDecl = right.declRef.as<SubscriptDecl>()) + { + auto leftAccessorCount = leftSubscriptDecl.getDecl()->getMembersOfType<AccessorDecl>().getCount(); + auto rightAccessorCount = rightSubscriptDecl.getDecl()->getMembersOfType<AccessorDecl>().getCount(); + auto decl1IsSubsetOfDecl2 = [=](SubscriptDecl* decl1, SubscriptDecl* decl2) + { + for (auto accessorDecl1 : decl1->getMembersOfType<AccessorDecl>()) + { + bool found = false; + for (auto accessorDecl2 : decl2->getMembersOfType<AccessorDecl>()) + { + 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. |
