Skip to content

Void-return function handling - #2933

Open
jbdoderlein wants to merge 4 commits into
usethesource:mainfrom
jbdoderlein:void-function-fail-init
Open

jbdoderlein wants to merge 4 commits into
usethesource:mainfrom
jbdoderlein:void-function-fail-init

Conversation

@jbdoderlein

Copy link
Copy Markdown
Contributor

This PR try to provide error for current interpreter and for the type checker to the use of void returning function where a value is expected, like variable initialization, keyword default field or string interpolation. This is exactly the error that happen in #2905 .

I tried to be exhaustive as possible on place where this could happen (one test for each in type checker), but maybe i forgot some case.

Interpreter

For the runtime, clear unexpected type is raised when a void would be used for initialization.
Example of current runtime behavior :

rascal>void f(int x){return;}
void (int): function(|prompt:///|(0,22,<1,0>,<1,22>))

rascal>a = f(5);
ok
rascal>a
|prompt:///|(0,1,<1,0>,<1,1>): Uninitialized variable: a
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UninitializedVariable|

rascal>int a = f(5);
ok
rascal>a
|prompt:///|(0,1,<1,0>,<1,1>): Uninitialized variable: a
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UninitializedVariable|

rascal>"<f(3)>"
java.lang.NullPointerException
(internal error)
        at $(|main:///_dollar_|)

java.lang.NullPointerException
        at org.rascalmpl.interpreter.StringTemplateConverter$Visitor$IndentingAppend.appendToString(StringTemplateConverter.java:128)
        at ....

New behavior :

rascal>void f(int x){return;}
void (int): function(|prompt:///|(0,22,<1,0>,<1,22>))

rascal>a = f(5);
|prompt:///|(4,5,<1,4>,<1,9>): Non-void type required
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/NonVoidTypeRequired|

rascal>int a = f(5);
|prompt:///|(0,13,<1,0>,<1,13>): Expected int, but got void
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UnexpectedType|

rascal>"<f(3)>"
|prompt:///|(2,4,<1,2>,<1,6>): Non-void type required
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/NonVoidTypeRequired|

Type checker

I added/changed some check for void to handle these case, especially the overloaded function case : if any overloaded function would return void, an error is reported.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 45%. Comparing base (2386b79) to head (2c7a255).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...calmpl/interpreter/result/ConstructorFunction.java 33% 2 Missing and 2 partials ⚠️
...ascalmpl/interpreter/result/ConstructorResult.java 0% 1 Missing and 1 partial ⚠️
...rc/org/rascalmpl/semantics/dynamic/Expression.java 0% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              main   #2933   +/-   ##
=======================================
  Coverage       45%     45%           
+ Complexity    6814    6812    -2     
=======================================
  Files          844     844           
  Lines        68823   68846   +23     
  Branches     10038   10049   +11     
=======================================
+ Hits         31429   31442   +13     
  Misses       35003   35003           
- Partials      2391    2401   +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant