1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Fixed 'ref' and 'in' this parameter for extension methods

This commit is contained in:
Brian Fiete 2025-01-17 17:32:25 -08:00
parent 0d15b28bd3
commit a1cd01cd3d
4 changed files with 43 additions and 1 deletions

View file

@ -1336,6 +1336,9 @@ void BfAutoComplete::AddExtensionMethods(BfTypeInstance* targetType, BfTypeInsta
continue;
auto thisType = methodInstance->GetParamType(0);
if (thisType->IsRef())
thisType = thisType->GetUnderlyingType();
bool paramValidated = false;
if (methodInstance->GetNumGenericParams() > 0)
{

View file

@ -426,7 +426,14 @@ BfMethodDef* BfDefBuilder::CreateMethodDef(BfMethodDeclaration* methodDeclaratio
methodDef->mBody = methodDeclaration->mBody;
if ((methodDeclaration->mThisToken != NULL) && (!methodDeclaration->mParams.IsEmpty()))
{
methodDef->mMethodType = BfMethodType_Extension;
if (!methodDef->mIsStatic)
{
methodDef->mIsStatic = true;
Fail("Extension methods must be declared 'static'", methodDef->GetRefNode());
}
}
HashContext signatureHashCtx;
HashNode(signatureHashCtx, methodDeclaration, methodDef->mBody);

View file

@ -2274,6 +2274,12 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
}
else
{
if ((checkMethod->mMethodType == BfMethodType_Extension) && (argIdx == -1))
{
if ((wantType->IsRef()) && (!argTypedValue.mType->IsRef()))
wantType = wantType->GetUnderlyingType();
}
if ((wantType->IsRef()) && (!argTypedValue.mType->IsRef()) &&
((mAllowImplicitRef) || (wantType->IsIn())))
wantType = wantType->GetUnderlyingType();
@ -10582,7 +10588,8 @@ BfTypedValue BfExprEvaluator::MatchMethod(BfAstNode* targetSrc, BfMethodBoundExp
callTargetType = moduleMethodInstance.mMethodInstance->GetParamType(0);
if ((callTargetType->IsRef()) && (target.IsAddr()) && (!target.IsReadOnly()) && (target.mType->IsValueType()))
{
target = BfTypedValue(target.mValue, mModule->CreateRefType(target.mType));
auto refType = (BfRefType*)callTargetType;
target = BfTypedValue(target.mValue, mModule->CreateRefType(target.mType, refType->mRefKind));
}
}

View file

@ -12,8 +12,21 @@ namespace Tests
return 123;
}
public static void AddTo(this ref StructA @this)
{
@this.mA += 1000;
}
public static void AddTo2(this in StructA @this)
{
#unwarn
(&@this).mA += 1000;
}
class ClassA
{
public int mA = 10000;
public static void Test()
{
Test.Assert("Abc".Remove(1.2f, 2.3f) == 123);
@ -29,6 +42,11 @@ namespace Tests
}
struct StructA
{
public int mA;
}
[Test]
public static void TestBasics()
{
@ -46,6 +64,13 @@ namespace Tests
float a = 1.2f;
float b = 2.3f;
Test.Assert(a.CompareIt(b) < 0);
StructA sa = .() { mA = 123 };
sa.AddTo();
Test.Assert(sa.mA == 1123);
sa.AddTo2();
Test.Assert(sa.mA == 2123);
}
}