From e64e421febe979a1c0da23b6044452d593c17e0c Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 12 Jun 2025 06:19:41 +0200 Subject: [PATCH] Improved method selection for extension methods with generic 'this' --- IDEHelper/Compiler/BfExprEvaluator.cpp | 13 ++++++++++--- IDEHelper/Tests/src/Extensions.bf | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index cbf87c90..ade73916 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -889,7 +889,7 @@ void BfMethodMatcher::CompareMethods(BfMethodInstance* prevMethodInstance, BfTyp bool paramWasConstExpr = false; bool prevParamWasConstExpr = false; - + bool paramWasUnspecialized = paramType->IsUnspecializedType(); if ((genericArgumentsSubstitute != NULL) && (paramWasUnspecialized)) { @@ -952,9 +952,16 @@ void BfMethodMatcher::CompareMethods(BfMethodInstance* prevMethodInstance, BfTyp else if ((wasArgDeferred) && (paramType != prevParamType) && (paramType == arg.mType) && (paramType == resolvedArg->mBestBoundType)) isBetter = true; else if ((wasArgDeferred) && (paramType != prevParamType) && (prevParamType == arg.mType) && (prevParamType == resolvedArg->mBestBoundType)) - isWorse = true; + isWorse = true; + else if ((argIdx == -1) && (anyIsExtension)) + { + if ((newMethodInstance->mMethodDef->mMethodType == BfMethodType_Extension) && (wasGenericParam) && (!prevWasGenericParam)) + isWorse = true; + else if ((prevMethodInstance->mMethodDef->mMethodType == BfMethodType_Extension) && (prevWasGenericParam) && (!wasGenericParam)) + isBetter = true; + } else - { + { bool canCastFromCurToPrev = mModule->CanCast(mModule->GetFakeTypedValue(paramType), prevParamType, implicitCastFlags); bool canCastFromPrevToCur = mModule->CanCast(mModule->GetFakeTypedValue(prevParamType), paramType, implicitCastFlags); diff --git a/IDEHelper/Tests/src/Extensions.bf b/IDEHelper/Tests/src/Extensions.bf index 696660cf..4ffa7599 100644 --- a/IDEHelper/Tests/src/Extensions.bf +++ b/IDEHelper/Tests/src/Extensions.bf @@ -319,6 +319,17 @@ namespace Tests Test.Assert((ms.mVal == 1) && (ms.mVal2 == 111) && (ms.mVal3 == 222)); ms = .(1, 2); Test.Assert((ms.mVal == 1) && (ms.mVal2 == 2) && (ms.mVal3 == 222)); + + List intList = scope .(); + for (int32 i < 100) + intList.Add(i); + Span intSpan = intList; + + var span = intSpan.ToRawData(); + Test.Assert(span.Length == 400); + + span = ((int32)123).ToRawData(); + Test.Assert(span.Length == 4); } [Test] @@ -430,3 +441,12 @@ namespace Tests } } } + +static +{ + public static Span ToRawData(this T self) where T : struct + { + #unwarn + return .((uint8*)&self, sizeof(T)); + } +} \ No newline at end of file