🐞 Bugfix: Recursively check for return statemen tprescence #146

Closed
opened 2023-07-16 17:42:41 +01:00 by deavmi · 3 comments
Owner

What is this?

The code in parser:

/**
                 * 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;

                    // FIXME: Need a recursive search downwards
                    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");
                    }
                }

Needs to be recursive to look for the statement, example is:

module simple_function_recursion_factorial;

ubyte factorial(ubyte i)
{
    if(i == cast(ubyte)0)
    {
        return cast(ubyte)1;
    }
    else
    {
        return i*factorial(i-cast(ubyte)1);
    }
}
## What is this? The code in parser: ```d /** * 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; // FIXME: Need a recursive search downwards 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"); } } ``` Needs to be recursive to look for the statement, example is: ```d module simple_function_recursion_factorial; ubyte factorial(ubyte i) { if(i == cast(ubyte)0) { return cast(ubyte)1; } else { return i*factorial(i-cast(ubyte)1); } } ```
deavmi added the
parser
needsfix
labels 2023-07-16 17:43:15 +01:00
deavmi added this to the Parser project 2023-07-16 17:43:23 +01:00
deavmi self-assigned this 2023-07-16 17:43:27 +01:00
deavmi added this to the Basics milestone 2023-07-16 17:43:30 +01:00
deavmi started working 2023-07-16 17:44:16 +01:00
deavmi added the due date 2023-07-18 2023-07-16 17:44:23 +01:00
deavmi added reference bugfix/recursive_return_statement_check 2023-07-16 17:44:27 +01:00
deavmi stopped working 2023-07-16 17:50:34 +01:00
6 minutes 18 seconds
deavmi started working 2023-07-16 18:09:20 +01:00
Author
Owner

I have a fix for this as of commit ae5be967aac9b6b63981b9745dae4be066b50d59

I have a fix for this as of commit `ae5be967aac9b6b63981b9745dae4be066b50d59`
Author
Owner

Merging...

Merging...
Author
Owner

Merging...

CI/CD (unit tests thus far) have an error that is unrelated and expected.

> Merging... CI/CD (unit tests thus far) have an error that is unrelated and expected.
deavmi stopped working 2023-07-16 18:21:13 +01:00
11 minutes 53 seconds
deavmi reopened this issue 2023-07-16 18:21:35 +01:00
deavmi added a new dependency 2023-07-16 18:21:37 +01:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Total Time Spent: 18 minutes 11 seconds
deavmi
18 minutes 11 seconds
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

2023-07-18

Reference: tlang/tlang#146
No description provided.