From 35a81b7bbe651e18ab34e2404b9ab88e7660f1db Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 8 Jun 2022 10:57:30 -0700 Subject: [PATCH] Fixed indexer method selection --- IDEHelper/Compiler/BfExprEvaluator.cpp | 66 ++++++++++++-------------- IDEHelper/Tests/src/Indexers.bf | 14 ++++++ 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 498b73a6..b623d8f4 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -20981,16 +20981,14 @@ void BfExprEvaluator::Visit(BfIndexerExpression* indexerExpr) { bool isFailurePass = pass == 1; + BfPropertyDef* foundProp = NULL; + BfTypeInstance* foundPropTypeInst = NULL; + auto curCheckType = startCheckTypeInst; while (curCheckType != NULL) { BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None; - BfPropertyDef* foundProp = NULL; - BfTypeInstance* foundPropTypeInst = NULL; - - int matchedIndexCount = 0; - curCheckType->mTypeDef->PopulateMemberSets(); BfMemberSetEntry* entry; @@ -21042,46 +21040,44 @@ void BfExprEvaluator::Visit(BfIndexerExpression* indexerExpr) methodMatcher.mCheckedKind = checkedKind; methodMatcher.mTarget = target; - methodMatcher.CheckMethod(startCheckTypeInst, curCheckType, checkMethod, false); + bool hadMatch = methodMatcher.CheckMethod(startCheckTypeInst, curCheckType, checkMethod, false); - if ((methodMatcher.mBestMethodDef == checkMethod) || (methodMatcher.mBackupMethodDef == checkMethod)) + if ((hadMatch) || (methodMatcher.mBackupMethodDef == checkMethod)) { foundPropTypeInst = curCheckType; foundProp = prop; - matchedIndexCount = (int)checkMethod->mParams.size(); } } } - if (foundProp != NULL) - { - - mPropSrc = indexerExpr->mOpenBracket; - mPropDef = foundProp; - if (foundProp->mIsStatic) - { - mPropTarget = BfTypedValue(curCheckType); - } - else - { - if (target.mType != foundPropTypeInst) - mPropTarget = mModule->Cast(indexerExpr->mTarget, target, foundPropTypeInst); - else - mPropTarget = target; - } - mOrigPropTarget = mPropTarget; - if (isInlined) - mPropGetMethodFlags = (BfGetMethodInstanceFlags)(mPropGetMethodFlags | BfGetMethodInstanceFlag_ForceInline); - mPropCheckedKind = checkedKind; - - if ((target.IsBase()) && (mPropDef->IsVirtual())) - mPropDefBypassVirtual = true; - - return; - } - curCheckType = curCheckType->mBaseType; } + + if (foundProp != NULL) + { + mPropSrc = indexerExpr->mOpenBracket; + mPropDef = foundProp; + if (foundProp->mIsStatic) + { + mPropTarget = BfTypedValue(curCheckType); + } + else + { + if (target.mType != foundPropTypeInst) + mPropTarget = mModule->Cast(indexerExpr->mTarget, target, foundPropTypeInst); + else + mPropTarget = target; + } + mOrigPropTarget = mPropTarget; + if (isInlined) + mPropGetMethodFlags = (BfGetMethodInstanceFlags)(mPropGetMethodFlags | BfGetMethodInstanceFlag_ForceInline); + mPropCheckedKind = checkedKind; + + if ((target.IsBase()) && (mPropDef->IsVirtual())) + mPropDefBypassVirtual = true; + + return; + } } mModule->Fail("Unable to find indexer property", indexerExpr->mTarget); diff --git a/IDEHelper/Tests/src/Indexers.bf b/IDEHelper/Tests/src/Indexers.bf index 978867d3..12d5abe6 100644 --- a/IDEHelper/Tests/src/Indexers.bf +++ b/IDEHelper/Tests/src/Indexers.bf @@ -76,6 +76,17 @@ namespace Tests } } + class Foo + { + public virtual int this[int i] => i; + } + + class Bar : Foo + { + public override int this[int i] => i*i; + public Span this[Range r] => default; + } + [Test] public static void TestBasics() { @@ -102,6 +113,9 @@ namespace Tests int a = sa[3, 4, 5]; Test.Assert(a == 12); Test.Assert(sa.mA == 9024); + + Bar bar = scope .(); + Test.Assert(bar[3] == 9); } } }