mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-22 09:38:01 +02:00
Improving auto-implemented properties
This commit is contained in:
parent
66aeb0a302
commit
664078557f
7 changed files with 212 additions and 24 deletions
|
@ -3349,11 +3349,11 @@ BfTypedValue BfExprEvaluator::LookupField(BfAstNode* targetSrc, BfTypedValue tar
|
|||
auto field = nextField;
|
||||
nextField = nextField->mNextWithSameName;
|
||||
|
||||
if ((!isFailurePass) && (!mModule->CheckProtection(protectionCheckFlags, curCheckType, field->mDeclaringType->mProject, field->mProtection, startCheckType)))
|
||||
if (((flags & BfLookupFieldFlag_IgnoreProtection) == 0) && (!isFailurePass) &&
|
||||
(!mModule->CheckProtection(protectionCheckFlags, curCheckType, field->mDeclaringType->mProject, field->mProtection, startCheckType)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
bool isResolvingFields = curCheckType->mResolvingConstField || curCheckType->mResolvingVarField;
|
||||
|
||||
|
@ -3855,6 +3855,37 @@ BfTypedValue BfExprEvaluator::LookupField(BfAstNode* targetSrc, BfTypedValue tar
|
|||
}
|
||||
}
|
||||
|
||||
// Check for direct auto-property access
|
||||
if (startCheckType == mModule->mCurTypeInstance)
|
||||
{
|
||||
if (auto propertyDeclaration = BfNodeDynCast<BfPropertyDeclaration>(mPropDef->mFieldDeclaration))
|
||||
{
|
||||
if (curCheckType->mTypeDef->HasAutoProperty(propertyDeclaration))
|
||||
{
|
||||
bool hasSetter = GetPropertyMethodDef(mPropDef, BfMethodType_PropertySetter, BfCheckedKind_NotSet) != NULL;
|
||||
auto autoFieldName = curCheckType->mTypeDef->GetAutoPropertyName(propertyDeclaration);
|
||||
auto result = LookupField(targetSrc, target, autoFieldName, BfLookupFieldFlag_IgnoreProtection);
|
||||
if (result)
|
||||
{
|
||||
if (!hasSetter)
|
||||
{
|
||||
if (((mModule->mCurMethodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)) &&
|
||||
(startCheckType == mModule->mCurTypeInstance))
|
||||
{
|
||||
// Allow writing inside ctor
|
||||
}
|
||||
else
|
||||
result.MakeReadOnly();
|
||||
}
|
||||
mPropDef = NULL;
|
||||
mPropSrc = NULL;
|
||||
mOrigPropTarget = NULL;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SetAndRestoreValue<BfTypedValue> prevResult(mResult, target);
|
||||
CheckResultForReading(mResult);
|
||||
return BfTypedValue();
|
||||
|
@ -14502,6 +14533,15 @@ bool BfExprEvaluator::CheckModifyResult(BfTypedValue typedVal, BfAstNode* refNod
|
|||
mModule->TypeToString(mResultFieldInstance->mOwner).c_str(), mResultFieldInstance->GetFieldDef()->mName.c_str(),
|
||||
mModule->MethodToString(mModule->mCurMethodInstance).c_str()), refNode);
|
||||
}
|
||||
else if (auto propertyDeclaration = BfNodeDynCast<BfPropertyDeclaration>(mResultFieldInstance->GetFieldDef()->mFieldDeclaration))
|
||||
{
|
||||
String propNam;
|
||||
if (propertyDeclaration->mNameNode != NULL)
|
||||
propertyDeclaration->mNameNode->ToString(propNam);
|
||||
|
||||
error = mModule->Fail(StrFormat("Cannot %s auto-implemented property '%s.%s' without set accessor", modifyType,
|
||||
mModule->TypeToString(mResultFieldInstance->mOwner).c_str(), propNam.c_str()), refNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
error = mModule->Fail(StrFormat("Cannot %s field '%s.%s' within struct method '%s'. Consider adding 'mut' specifier to this method.", modifyType,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue