diff --git a/include/NZSL/Lang/ErrorList.hpp b/include/NZSL/Lang/ErrorList.hpp index a7ab1fe..0798fe9 100644 --- a/include/NZSL/Lang/ErrorList.hpp +++ b/include/NZSL/Lang/ErrorList.hpp @@ -100,8 +100,9 @@ NZSL_SHADERLANG_COMPILER_ERROR(FunctionCallUnexpectedEntryFunction, "{} is an en NZSL_SHADERLANG_COMPILER_ERROR(FunctionCallUnmatchingParameterCount, "function {} expects {} parameter(s), but got {}", std::string, std::uint32_t, std::uint32_t) NZSL_SHADERLANG_COMPILER_ERROR(FunctionCallUnmatchingParameterType, "function {} parameter #{} type mismatch (expected {}, got {})", std::string, std::uint32_t, std::string, std::string) NZSL_SHADERLANG_COMPILER_ERROR(FunctionDeclarationInsideFunction, "a function cannot be defined inside another function") -NZSL_SHADERLANG_COMPILER_ERROR(FunctionReturnStatementWithAValue, "return-statement with a value, in function returning no value") -NZSL_SHADERLANG_COMPILER_ERROR(FunctionReturnStatementWithNoValue, "return-statement with no value, in function returning {}", std::string) +NZSL_SHADERLANG_COMPILER_ERROR(FunctionReturnWithAValue, "return with a value, in function returning no value") +NZSL_SHADERLANG_COMPILER_ERROR(FunctionReturnWithNoValue, "return with no value, in function returning {}", std::string) +NZSL_SHADERLANG_COMPILER_ERROR(FunctionReturnUnmatchingTypes, "return expression type ({}) must match function return expression type ({})", std::string, std::string) NZSL_SHADERLANG_COMPILER_ERROR(IdentifierAlreadyUsed, "identifier {} is already used", std::string) NZSL_SHADERLANG_COMPILER_ERROR(ImportIdentifierAlreadyPresent, "{} identifier was already imported", std::string) NZSL_SHADERLANG_COMPILER_ERROR(ImportIdentifierNotFound, "identifier {} not found in module {}", std::string, std::string) diff --git a/src/NZSL/Ast/SanitizeVisitor.cpp b/src/NZSL/Ast/SanitizeVisitor.cpp index c1791da..ab7fd8a 100644 --- a/src/NZSL/Ast/SanitizeVisitor.cpp +++ b/src/NZSL/Ast/SanitizeVisitor.cpp @@ -2799,7 +2799,6 @@ NAZARA_WARNING_POP() StatementPtr SanitizeVisitor::Clone(ReturnStatement& node) { auto clone = Nz::StaticUniquePointerCast(Cloner::Clone(node)); - Validate(*clone); return clone; @@ -4259,26 +4258,28 @@ NAZARA_WARNING_POP() if (!function->node->returnType.IsResultingValue()) return ValidationResult::Unresolved; - const bool functionHasNoReturnType = std::holds_alternative(function->node->returnType.GetResultingValue()); + const ExpressionType& functionReturnType = function->node->returnType.GetResultingValue(); + bool functionHasNoReturnType = std::holds_alternative(functionReturnType); if (!node.returnExpr) { if (!functionHasNoReturnType) - throw CompilerFunctionReturnStatementWithNoValueError{ node.sourceLocation, ToString(function->node->returnType.GetResultingValue(), node.sourceLocation) }; + throw CompilerFunctionReturnWithNoValueError{ node.sourceLocation, ToString(functionReturnType, node.sourceLocation) }; //If node doesn't have an expression and function doesn't have return type, then we can directly validate return ValidationResult::Validated; } if (functionHasNoReturnType) - throw CompilerFunctionReturnStatementWithAValueError{ node.sourceLocation }; + throw CompilerFunctionReturnWithAValueError{ node.sourceLocation }; const ExpressionType* returnTypeOpt = GetExpressionType(MandatoryExpr(node.returnExpr, node.sourceLocation)); if (!returnTypeOpt) return ValidationResult::Unresolved; ExpressionType returnType = ResolveType(*returnTypeOpt, true, node.sourceLocation); - TypeMustMatch(returnType, function->node->returnType.GetResultingValue(), node.sourceLocation); + if (returnType != ResolveAlias(functionReturnType)) + throw CompilerFunctionReturnUnmatchingTypesError{ node.sourceLocation, ToString(returnType, node.sourceLocation), ToString(functionReturnType, node.sourceLocation) }; return ValidationResult::Validated; } diff --git a/tests/src/Tests/ErrorsTests.cpp b/tests/src/Tests/ErrorsTests.cpp index ce49e86..f68e4d7 100644 --- a/tests/src/Tests/ErrorsTests.cpp +++ b/tests/src/Tests/ErrorsTests.cpp @@ -718,7 +718,7 @@ fn test() -> i32 { return 42.666; } -)"), "(7,2 -> 15): CUnmatchingTypes error: left expression type (f32) doesn't match right expression type (i32)"); +)"), "(7,2 -> 15): CFunctionReturnUnmatchingTypes error: return expression type (f32) must match function return expression type (i32)"); CHECK_THROWS_WITH(Compile(R"( [nzsl_version("1.0")] @@ -728,7 +728,7 @@ fn test() -> i32 { return; } -)"), "(7,2 -> 8): CFunctionReturnStatementWithNoValue error: return-statement with no value, in function returning i32"); +)"), "(7,2 -> 8): CFunctionReturnWithNoValue error: return with no value, in function returning i32"); CHECK_THROWS_WITH(Compile(R"( [nzsl_version("1.0")] @@ -738,7 +738,7 @@ fn test() { return 10; } -)"), "(7,2 -> 11): CFunctionReturnStatementWithAValue error: return-statement with a value, in function returning no value"); +)"), "(7,2 -> 11): CFunctionReturnWithAValue error: return with a value, in function returning no value"); } /************************************************************************/