Skip to content

Commit

Permalink
Added special handling for assigning void function to a variable (req…
Browse files Browse the repository at this point in the history
…uired)
  • Loading branch information
loumadev committed Nov 28, 2023
1 parent 224e585 commit 2aae2d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/compiler/analyser/Analyser.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,15 @@ AnalyserResult __Analyser_analyseBlock(Analyser *analyser, BlockASTNode *block)
AnalyserResult result = Analyser_resolveExpressionType(analyser, declaratorNode->initializer, block->scope, declaration->type, &type);
if(!result.success) return result;

// This was requested by the assignment
if(declaration->type.type == TYPE_VOID) {
return AnalyserError(
RESULT_ERROR_SEMANTIC_INVALID_TYPE,
String_fromFormat("cannot use initializer for variable of type 'Void'"),
NULL
);
}

if(declaratorNode->pattern->type) {
declaratorNode->pattern->type->type = Analyser_TypeReferenceToValueType(declaratorNode->pattern->type);

Expand Down
3 changes: 2 additions & 1 deletion test/compiler/analyser/Analyser.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3298,7 +3298,8 @@ DESCRIBE(return_statement, "Analysis of a return statement") {
EXPECT_TRUE(parserResult.success);

analyserResult = Analyser_analyse(&analyser, (ProgramASTNode*)parserResult.node);
EXPECT_TRUE(analyserResult.success);
// EXPECT_TRUE(analyserResult.success);
EXPECT_FALSE(analyserResult.success);
} TEST_END();

TEST_BEGIN("Invalid assignment of the void expression to the variable") {
Expand Down

0 comments on commit 2aae2d2

Please sign in to comment.