From 733f7ec9836357372c71e01b8ba92fdaad5463ca Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Mon, 18 Jan 2021 16:01:22 -0800 Subject: [PATCH] Fixed missing generic type validation errors with elemented types --- IDEHelper/Compiler/BfModule.cpp | 2 +- IDEHelper/Compiler/BfModule.h | 3 +- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 89 ++++++++++++++++-------- 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 1faf125f..5ff8fad8 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -6543,7 +6543,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, true); - BfMethodFlags methodFlags = moduleMethodInstance.mMethodInstance->GetMethodFlags(); + BfMethodFlags methodFlags = defaultMethod->GetMethodFlags(); int customAttrIdx = _HandleCustomAttrs(methodCustomAttributes); diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index 0d2c350b..934769a5 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -1808,6 +1808,7 @@ public: BfTypeInstance* GetBaseType(BfTypeInstance* typeInst); void HandleTypeGenericParamRef(BfAstNode* refNode, BfTypeDef* typeDef, int typeGenericParamIdx); void HandleMethodGenericParamRef(BfAstNode* refNode, BfTypeDef* typeDef, BfMethodDef* methodDef, int typeGenericParamIdx); + bool ResolveTypeResult_Validate(BfTypeReference* typeRef, BfType* resolvedTypeRef); BfType* ResolveTypeResult(BfTypeReference* typeRef, BfType* resolvedTypeRef, BfPopulateType populateType, BfResolveTypeRefFlags resolveFlags); void ShowAmbiguousTypeError(BfAstNode* refNode, BfTypeDef* typeDef, BfTypeDef* otherTypeDef); void ShowGenericArgCountError(BfTypeReference* typeRef, int wantedGenericParams); @@ -1820,7 +1821,7 @@ public: void CheckTypeRefFixit(BfAstNode* typeRef, const char* appendName = NULL); void CheckIdentifierFixit(BfAstNode* node); void TypeRefNotFound(BfTypeReference* typeRef, const char* appendName = NULL); - bool ValidateTypeWildcard(BfTypeReference* typeRef, bool isAttributeRef); + bool ValidateTypeWildcard(BfTypeReference* typeRef, bool isAttributeRef); BfType* ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0, int numGenericArgs = 0); BfType* ResolveTypeRefAllowUnboundGenerics(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, bool resolveGenericParam = true); BfType* ResolveTypeRef(BfAstNode* astNode, const BfSizedArray* genericArgs, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0); diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 38b53de7..d8ff4c52 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -7695,6 +7695,63 @@ BfGenericParamInstance* BfModule::GetGenericParamInstance(BfGenericParamType* ty return GetGenericTypeParamInstance(type->mGenericParamIdx); } +bool BfModule::ResolveTypeResult_Validate(BfTypeReference* typeRef, BfType* resolvedTypeRef) +{ + if ((typeRef == NULL) || (resolvedTypeRef == NULL)) + return true; + + BfTypeInstance* genericTypeInstance = resolvedTypeRef->ToGenericTypeInstance(); + + if ((genericTypeInstance != NULL) && (genericTypeInstance != mCurTypeInstance)) + { + bool doValidate = (genericTypeInstance->mGenericTypeInfo->mHadValidateErrors) || + (!genericTypeInstance->mGenericTypeInfo->mValidatedGenericConstraints) || + (genericTypeInstance->mGenericTypeInfo->mIsUnspecializedVariation); + if ((mCurMethodInstance != NULL) && (mCurMethodInstance->IsOrInUnspecializedVariation())) + doValidate = false; + if (mCurTypeInstance != NULL) + { + if (mCurTypeInstance->IsUnspecializedTypeVariation()) + doValidate = false; + if (auto curGenericTypeInstance = mCurTypeInstance->ToGenericTypeInstance()) + { + if ((curGenericTypeInstance->mDependencyMap.mMinDependDepth > 32) && + (genericTypeInstance->mDependencyMap.mMinDependDepth > 32)) + { + Fail(StrFormat("Generic type dependency depth exceeded for type '{}'", TypeToString(genericTypeInstance).c_str()), typeRef); + return false; + } + + if (curGenericTypeInstance->mGenericTypeInfo->mHadValidateErrors) + doValidate = false; + } + if ((mContext->mCurTypeState != NULL) && (mContext->mCurTypeState->mCurBaseTypeRef != NULL) && (!mContext->mCurTypeState->mTypeInstance->IsTypeAlias())) // We validate constraints for base types later + doValidate = false; + } + + if (doValidate) + ValidateGenericConstraints(typeRef, genericTypeInstance, false); + } + + if (auto genericInstanceTypeRef = BfNodeDynCastExact(typeRef)) + { + if (genericTypeInstance != NULL) + { + auto genericTypeInfo = genericTypeInstance->GetGenericTypeInfo(); + for (int argIdx = 0; argIdx < (int)genericInstanceTypeRef->mGenericArguments.size(); argIdx++) + { + ResolveTypeResult_Validate(genericInstanceTypeRef->mGenericArguments[argIdx], genericTypeInfo->mTypeGenericArguments[argIdx]); + } + } + } + else if (auto elementedTypeRef = BfNodeDynCast(typeRef)) + { + return ResolveTypeResult_Validate(elementedTypeRef, resolvedTypeRef->GetUnderlyingType()); + } + + return true; +} + BfType* BfModule::ResolveTypeResult(BfTypeReference* typeRef, BfType* resolvedTypeRef, BfPopulateType populateType, BfResolveTypeRefFlags resolveFlags) { if ((mCompiler->mIsResolveOnly) && (!IsInSpecializedSection())) @@ -7946,36 +8003,8 @@ BfType* BfModule::ResolveTypeResult(BfTypeReference* typeRef, BfType* resolvedTy populateModule->PopulateType(resolvedTypeRef, populateType); - if ((genericTypeInstance != NULL) && (genericTypeInstance != mCurTypeInstance) && (populateType > BfPopulateType_Identity)) - { - bool doValidate = (genericTypeInstance->mGenericTypeInfo->mHadValidateErrors) || - (!genericTypeInstance->mGenericTypeInfo->mValidatedGenericConstraints) || - (genericTypeInstance->mGenericTypeInfo->mIsUnspecializedVariation); - if ((mCurMethodInstance != NULL) && (mCurMethodInstance->IsOrInUnspecializedVariation())) - doValidate = false; - if (mCurTypeInstance != NULL) - { - if (mCurTypeInstance->IsUnspecializedTypeVariation()) - doValidate = false; - if (auto curGenericTypeInstance = mCurTypeInstance->ToGenericTypeInstance()) - { - if ((curGenericTypeInstance->mDependencyMap.mMinDependDepth > 32) && - (genericTypeInstance->mDependencyMap.mMinDependDepth > 32)) - { - Fail(StrFormat("Generic type dependency depth exceeded for type '{}'", TypeToString(genericTypeInstance).c_str()), typeRef); - return NULL; - } - - if (curGenericTypeInstance->mGenericTypeInfo->mHadValidateErrors) - doValidate = false; - } - if ((mContext->mCurTypeState != NULL) && (mContext->mCurTypeState->mCurBaseTypeRef != NULL) && (!mContext->mCurTypeState->mTypeInstance->IsTypeAlias())) // We validate constraints for base types later - doValidate = false; - } - - if (doValidate) - ValidateGenericConstraints(typeRef, genericTypeInstance, false); - } + if ((populateType > BfPopulateType_Identity) && (!ResolveTypeResult_Validate(typeRef, resolvedTypeRef))) + return NULL; if (populateType != BfPopulateType_IdentityNoRemapAlias) {