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

Fixed new/delete constraint checking

This commit is contained in:
Brian Fiete 2021-10-31 10:04:04 -07:00
parent a17f57f4c6
commit e5d70947d9

View file

@ -7675,24 +7675,6 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
return false; return false;
} }
if ((genericParamInst->mGenericParamFlags & BfGenericParamFlag_New) &&
((checkGenericParamFlags & (BfGenericParamFlag_New | BfGenericParamFlag_Var)) == 0))
{
if ((!ignoreErrors) && (PreFail()))
*errorOut = Fail(StrFormat("Must add 'where %s : new' constraint in order to use type as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
return false;
}
if ((genericParamInst->mGenericParamFlags & BfGenericParamFlag_Delete) &&
((checkGenericParamFlags & (BfGenericParamFlag_Delete | BfGenericParamFlag_Var)) == 0))
{
if ((!ignoreErrors) && (PreFail()))
*errorOut = Fail(StrFormat("Must add 'where %s : delete' constraint in order to use type as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
return false;
}
if ((genericParamInst->mGenericParamFlags & BfGenericParamFlag_Const) != 0) if ((genericParamInst->mGenericParamFlags & BfGenericParamFlag_Const) != 0)
{ {
if (((checkGenericParamFlags & BfGenericParamFlag_Const) == 0) && (!checkArgType->IsConstExprValue())) if (((checkGenericParamFlags & BfGenericParamFlag_Const) == 0) && (!checkArgType->IsConstExprValue()))
@ -7721,14 +7703,20 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
canDelete = true; canDelete = true;
else if (checkArgType->IsObjectOrInterface()) else if (checkArgType->IsObjectOrInterface())
canDelete = true; canDelete = true;
else if ((checkGenericParamFlags & BfGenericParamFlag_Delete) != 0) else if ((checkGenericParamFlags & (BfGenericParamFlag_Delete | BfGenericParamFlag_Var)) != 0)
canDelete = true; canDelete = true;
if (!canDelete) if (!canDelete)
{ {
if ((!ignoreErrors) && (PreFail())) if ((!ignoreErrors) && (PreFail()))
{
if (checkArgType->IsGenericParam())
*errorOut = Fail(StrFormat("Must add 'where %s : delete' constraint in order to use type as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
else
*errorOut = Fail(StrFormat("The type '%s' must be a deletable type in order to use it as parameter '%s' for '%s'", *errorOut = Fail(StrFormat("The type '%s' must be a deletable type in order to use it as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef); TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
}
return false; return false;
} }
} }
@ -7749,6 +7737,19 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
bool hadProtected = false; bool hadProtected = false;
while (checkMethodDef != NULL) while (checkMethodDef != NULL)
{ {
if (!checkMethodDef->mParams.IsEmpty())
{
auto firstParam = checkMethodDef->mParams[0];
if ((firstParam->mParamDeclaration != NULL) && (firstParam->mParamDeclaration->mInitializer != NULL))
{
// Allow all-default params
}
else
{
checkMethodDef = checkMethodDef->mNextWithSameName;
continue;
}
}
if (checkMethodDef->mProtection == BfProtection_Public) if (checkMethodDef->mProtection == BfProtection_Public)
{ {
canAlloc = true; canAlloc = true;
@ -7763,6 +7764,10 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
canAlloc = TypeIsSubTypeOf(mCurTypeInstance, checkTypeInst, false); canAlloc = TypeIsSubTypeOf(mCurTypeInstance, checkTypeInst, false);
} }
} }
else if (checkArgType->IsGenericParam())
{
canAlloc = (checkGenericParamFlags & (BfGenericParamFlag_New | BfGenericParamFlag_Var)) != 0;
}
else else
{ {
// Any primitive types and stuff can be allocated // Any primitive types and stuff can be allocated
@ -7772,8 +7777,14 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
if (!canAlloc) if (!canAlloc)
{ {
if ((!ignoreErrors) && (PreFail())) if ((!ignoreErrors) && (PreFail()))
{
if (checkArgType->IsGenericParam())
*errorOut = Fail(StrFormat("Must add 'where %s : new' constraint in order to use type as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
else
*errorOut = Fail(StrFormat("The type '%s' must have an accessible default constructor in order to use it as parameter '%s' for '%s'", *errorOut = Fail(StrFormat("The type '%s' must have an accessible default constructor in order to use it as parameter '%s' for '%s'",
TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef); TypeToString(origCheckArgType).c_str(), genericParamInst->GetName().c_str(), GenericParamSourceToString(genericParamSource).c_str()), checkArgTypeRef);
}
return false; return false;
} }
} }