Merge branch 'vardec_varass_dependency' into feature/universal_coercion

This commit is contained in:
Tristan B. Velloza Kildaire 2023-07-16 18:28:28 +02:00
commit a144e544f9
2 changed files with 77 additions and 3 deletions

View File

@ -1600,6 +1600,30 @@ public final class Parser
/* Will consume the `}` (or `;` if wantsBody-false) */
funcDefPair pair = parseFuncDef(wantsBody);
/**
* If this function definition has a body (i.e. `wantsBody == true`)
* and if the return type is non-void, THEN ensure we have a `ReturnStmt`
* (return statement)
*/
if(wantsBody && type != "void")
{
bool hasReturn;
foreach(Statement stmt; pair.bodyStatements)
{
if(cast(ReturnStmt)stmt)
{
hasReturn = true;
break;
}
}
// Error if no return statement exists
if(!hasReturn)
{
expect("Function '"~identifier~"' declared with return type does not contain a return statement");
}
}
generated = new Function(identifier, type, pair.bodyStatements, pair.params);
import std.stdio;
@ -2736,6 +2760,8 @@ int thing()
{
int discardExpr = function(&j);
int** l;
return 0;
}
`;
LexerInterface currentLexer = new BasicLexer(sourceCode);
@ -3015,4 +3041,52 @@ void function()
{
assert(false);
}
}
/**
* Function test case
*
* Test: A function of a non-void return type
* must have a return statement
*/
unittest
{
string sourceCode = `
module myModule;
int wrongFunction()
{
}
`;
LexerInterface currentLexer = new BasicLexer(sourceCode);
try
{
(cast(BasicLexer)currentLexer).performLex();
assert(true);
}
catch(LexerException e)
{
assert(false);
}
Parser parser = new Parser(currentLexer);
try
{
Module modulle = parser.parse();
assert(false);
}
catch(ParserException e)
{
// TODO: Try make errors specific enough to yank them out
assert(true);
}
catch(TError)
{
assert(false);
}
}

View File

@ -5,15 +5,15 @@ j = 2+func(j,test());
int func(int x1, byte x2)
{
return 1;
}
byte t2()
{
return 1;
}
byte test()
{
return 1;
}