Skip to content

[#928] Impl producing compiler error when required return is missing#958

Open
evgTSV wants to merge 2 commits intoForNeVeR:mainfrom
evgTSV:return-checker
Open

[#928] Impl producing compiler error when required return is missing#958
evgTSV wants to merge 2 commits intoForNeVeR:mainfrom
evgTSV:return-checker

Conversation

@evgTSV
Copy link
Copy Markdown
Contributor

@evgTSV evgTSV commented Feb 18, 2026

Closes #928

Right now the compiler can handle missing returns. Moreover, it can evaluate the value of a conditional statement (if it's constant) to determine whether it's necessary or redundant in order to drop unreachable paths. As a result, codegen has become simpler.

Besides, I've reworked a sequence of statements in the do..while loop. This allowed for easier optimization of the generated code, since the statements are now in the proper order.

Also, there is a TODO: I assume we should evaluate NULL literals in the future.

… optimize codegen for constan values for if/else, while/do..while. Enhance do..while codegen. Improve the ConstantEvaluator. Add tests.
@ForNeVeR ForNeVeR requested a review from kant2002 February 18, 2026 23:32
Copy link
Copy Markdown
Collaborator

@kant2002 kant2002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I see that this PR can be split into separate things

  • Error/warnigns handling which is not present
  • Improved control flow analysis

I really-really like to have improved control flow analysis in separate PR, since error reporting does not care if it right or wrong. It just report error/warnings based on what component say it.

Error/warnings behavior is completely separate subject, and we can happiely fight tooth and nails about default value for a flag, while technical PR can be speed run.

In general my recommendations, if you need to refactor something to achieve you goal, separate refactor and actual functionality. that's easier to review, and speedrun each part. Sometimes refactorign alone easier to review, and I can just approve it without too much thinking.

BasicBlocks.Add(currentBlock);
}
BasicBlock nextBlock = new();
BasicBlock newBlock = new();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these changes? can you undo renaming. If you want refactoring, and names cleanup, please in separate PR.

var newBlock = new BasicBlock();
currentBlock.Targets.Add(newBlock);
currentBlock = newBlock;
bool alwaysTaken =
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should happens during lowering of If/While/do instead of in the flow graph. When you have constant value in the conditionalGoto that mean instead of creation of ConditionalGoto we can immidiately create Goto or Nop. No need to create ConditionalGoto and then discard it during BB generation. that's only complicate logic and make CGoto more complex then needed.

return ($"Expression {expression} cannot be evaluated as constant expression.", null);
}
}
catch (Exception e)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use exceptions in compilers, if something blow-up that's correctness error. It should never happens.

// Do nothing here.
}

int missing_return()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's default behaviour for most C compilers, no matter how much @ForNeVeR want to be strict about C23 (which I don't consult yet on this part) compatibility is more improtant even to marketing efforts. How can I say - "Just compile you old MUD" or something silly like that if by default it would be rejected.

Copy link
Copy Markdown
Owner

@ForNeVeR ForNeVeR Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it looks like missing return is currently UB. There's a proposal from @foxtran to make it better (N3483), but it isn't approved (yet).

So, let's make it an option and opt-in (means — off by default). I am okay with that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm.. actually, now, it is undefined behaviour, so compiler may generate compile time errors in this case.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It obviously may generate errors but that increase chances that people get tired configure our compiler. I very much worry about that

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It obviously may generate errors

Is there an error for code like:

if (true) return;

?

@kant2002
Copy link
Copy Markdown
Collaborator

Example of separate work items is #962 and #956 which individually independent. when pulled together it would be messy.

@ForNeVeR
Copy link
Copy Markdown
Owner

For the record, I am okay with minor refactoring and actual work in same PR, but at least in separate commits please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Produce compiler error when required return is missing

4 participants