Skip to content

Commit

Permalink
Make some changes on return type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
NiiRoZz authored and SirLynix committed Nov 30, 2024
1 parent 71ed300 commit b6d797a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
5 changes: 3 additions & 2 deletions include/NZSL/Lang/ErrorList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/NZSL/Ast/SanitizeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2799,7 +2799,6 @@ NAZARA_WARNING_POP()
StatementPtr SanitizeVisitor::Clone(ReturnStatement& node)
{
auto clone = Nz::StaticUniquePointerCast<ReturnStatement>(Cloner::Clone(node));

Validate(*clone);

return clone;
Expand Down Expand Up @@ -4259,26 +4258,28 @@ NAZARA_WARNING_POP()
if (!function->node->returnType.IsResultingValue())
return ValidationResult::Unresolved;

const bool functionHasNoReturnType = std::holds_alternative<NoType>(function->node->returnType.GetResultingValue());
const ExpressionType& functionReturnType = function->node->returnType.GetResultingValue();
bool functionHasNoReturnType = std::holds_alternative<NoType>(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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/Tests/ErrorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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")]
Expand All @@ -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");
}

/************************************************************************/
Expand Down

0 comments on commit b6d797a

Please sign in to comment.