Skip to content

Fix self-referential variable initialization and pattern match - #3028

Merged
PaulKlint merged 19 commits into
mainfrom
fix/self-referential-pattern-local
Dec 3, 2026
Merged

PaulKlint merged 19 commits into
mainfrom
fix/self-referential-pattern-local

Conversation

@toinehartman

@toinehartman toinehartman commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

In int i = i and i := i, the right-hand-side i would be marked as a use of the definition on the left-hand-side, while that is incorrect. This PR fixes that.

After initially trying to solve this with modified scoping and an extension to typepal (usethesource/typepal#71), which turned out to be quite invasive across the type-checker, we opted for a more local approach. the result is this PR. Most of the changes consist of tests, and I pointed out the actual fix in comments.

Integration test: https://github.com/usethesource/rascal-core-big-tests/actions/runs/46883603008

Closes #2264.
Closes #2783.

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46%. Comparing base (b27d646) to head (fd67dd7).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##              main   #3028   +/-   ##
=======================================
  Coverage       46%     46%           
- Complexity    6933    6938    +5     
=======================================
  Files          851     851           
  Lines        69340   69340           
  Branches     10105   10105           
=======================================
+ Hits         32164   32180   +16     
+ Misses       34740   34729   -11     
+ Partials      2436    2431    -5     

☔ 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.

@toinehartman
toinehartman force-pushed the fix/self-referential-pattern-local branch from 0c0f6d8 to c32da79 Compare December 2, 2026 12:20
@toinehartman
toinehartman force-pushed the fix/self-referential-pattern-local branch from c32da79 to 1242ea9 Compare December 2, 2026 12:40
@toinehartman
toinehartman force-pushed the fix/self-referential-pattern-local branch from 1242ea9 to 97a9a7f Compare December 2, 2026 13:09
toinehartman and others added 2 commits December 2, 2026 16:39
Co-authored-by: Sung-Shik Jongmans <sung-shik.jongmans@swat.engineering>
@toinehartman toinehartman changed the title Fix self-referential pattern match Fix self-referential variable initialization and pattern match Dec 2, 2026
c.report(error(initial, "Right-hand side of assignment does not always have a value"));
}
}
storeDisallowUseOfDef(var.name, initial, c);

@toinehartman toinehartman Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Prohibit int i = i; in functions

c.pop(patternContainer);

for (p <- patternVars) {
storeDisallowUseOfDef(p, expression, c);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Prohibit i := i

@toinehartman
toinehartman marked this pull request as ready for review December 2, 2026 16:05
// println("foundUseDefs:");
// for(<u, d> <- foundUseDefs){
// println("<readFile(u)>:<u> ==\> <d>");
// }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Disabled debug printing

if(names[v]?) occ = names[v]; else throw "Name <v> not found in tree";
if(!occ[def]?) throw "Missing define <def> for name <v>";
if(!occ[def]?) {
println("Missing define <def> for name <v>");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Print all failure reasons and return false, instead of throwing on the first assertion

c.report(error(initial, "Right-hand side of assignment does not always have a value"));
}
}
storeDisallowUseOfDef(var.name, initial, c);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Prohibit int i = i at module level

@PaulKlint PaulKlint left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this down-to-earth approach. It is less sophisticated than the previous solution but it gets the job done in a much simpler and less invasive way.

One concern is that we never cleanup this information, see comment. We should at least discuss this.

@PaulKlint PaulKlint left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks ok

@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@PaulKlint
PaulKlint merged commit 659726d into main Dec 3, 2026
9 checks passed
@PaulKlint
PaulKlint deleted the fix/self-referential-pattern-local branch December 3, 2026 22:41
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.

Typechecker doesn't report error for self-referential variable definition Variable in pattern match RHS marked as use of declaration in LHS

2 participants