-
Notifications
You must be signed in to change notification settings - Fork 40
Fix compiled constructor patterns (close #521) #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LPTK
merged 6 commits into
hkust-taco:hkmc2
from
chengluyu:issue-521-data-constructor-patterns
Jun 30, 2026
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec8d0cb
Fix compiled data constructor patterns
chengluyu fcb9e85
Avoid field result records in match-only patterns
chengluyu cd14313
Merge branch 'hkmc2' into issue-521-data-constructor-patterns
LPTK c031c7b
Update a comment
chengluyu 8024a56
Address review comments
chengluyu f65bee0
Merge branch 'hkmc2' into issue-521-data-constructor-patterns
LPTK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
hkmc2/shared/src/test/mlscript/ucs/patterns/CompiledClassPatterns.mls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| :js | ||
|
|
||
| open annotations | ||
|
|
||
| data class Add(val lhs, val rhs) | ||
|
|
||
| :sir | ||
| case Add(0, 0) then 1 | ||
| //│ ———————————————| Lowered IR |——————————————————————————————————————————————————————————————————————— | ||
| //│ let lambda; | ||
| //│ @private | ||
| //│ define lambda as fun lambda⁰(caseScrut) { | ||
| //│ let arg$Add$0$, arg$Add$1$; | ||
| //│ match caseScrut | ||
| //│ Add⁰ => | ||
| //│ set arg$Add$0$ = caseScrut.lhs⁰; | ||
| //│ set arg$Add$1$ = caseScrut.rhs⁰; | ||
| //│ match arg$Add$0$ | ||
| //│ 0 => | ||
| //│ match arg$Add$1$ | ||
| //│ 0 => | ||
| //│ return 1 | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ }; | ||
| //│ return lambda⁰ | ||
| //│ —————————————————| Output |————————————————————————————————————————————————————————————————————————— | ||
| //│ = fun | ||
|
|
||
| :sir | ||
| case @compile Add(0, 0) then 1 | ||
| //│ ———————————————| Lowered IR |——————————————————————————————————————————————————————————————————————— | ||
| //│ let lambda; | ||
| //│ @private | ||
| //│ define lambda as fun lambda³(caseScrut) { | ||
| //│ let matcher__Addʿ0ꓹ0ʾ$, matcher__0$, matchSuccess, lambda1, lambda2; | ||
| //│ define lambda1 as fun lambda¹(input) { | ||
| //│ let p_1$, p_1$1, tmp, tmp1; | ||
| //│ match input | ||
| //│ 0 => | ||
| //│ set tmp = true; | ||
| //│ set p_1$ = tmp; | ||
| //│ return p_1$ | ||
| //│ else | ||
| //│ set tmp1 = false; | ||
| //│ set p_1$1 = tmp1; | ||
| //│ return p_1$1 | ||
| //│ end | ||
| //│ }; | ||
| //│ set matcher__0$ = lambda¹; | ||
| //│ define lambda2 as fun lambda²(input) { | ||
| //│ let lhs, rhs, lhs1, rhs1, p_0$, result1$, result1$1, p_0$1, tmp, tmp1; | ||
| //│ match input | ||
| //│ Add⁰ => | ||
| //│ set lhs = input.lhs⁰; | ||
| //│ set rhs = input.rhs⁰; | ||
| //│ set lhs1 = matcher__0$(lhs); | ||
| //│ set rhs1 = matcher__0$(rhs); | ||
| //│ set result1$1 = lhs1; | ||
| //│ match result1$1 | ||
| //│ true => | ||
| //│ set result1$ = rhs1; | ||
| //│ match result1$ | ||
| //│ true => | ||
| //│ set tmp = true; | ||
| //│ end | ||
| //│ else | ||
| //│ set tmp = false; | ||
| //│ end | ||
| //│ end | ||
| //│ else | ||
| //│ set tmp = false; | ||
| //│ end | ||
| //│ set p_0$ = tmp; | ||
| //│ return p_0$ | ||
| //│ else | ||
| //│ set tmp1 = false; | ||
| //│ set p_0$1 = tmp1; | ||
| //│ return p_0$1 | ||
| //│ end | ||
| //│ }; | ||
| //│ set matcher__Addʿ0ꓹ0ʾ$ = lambda²; | ||
| //│ set matchSuccess = matcher__Addʿ0ꓹ0ʾ$(caseScrut); | ||
| //│ match matchSuccess | ||
| //│ true => | ||
| //│ return 1 | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ }; | ||
| //│ return lambda³ | ||
| //│ —————————————————| Output |————————————————————————————————————————————————————————————————————————— | ||
| //│ = fun | ||
|
|
||
| data class Box(val value) | ||
|
|
||
| :soir | ||
|
chengluyu marked this conversation as resolved.
|
||
| case @compile (Box(0) | Box(1)) then 1 | ||
| //│ ——————————————| Optimized IR |—————————————————————————————————————————————————————————————————————— | ||
| //│ let lambda; | ||
| //│ @private | ||
| //│ define lambda as fun lambda⁴(caseScrut) { | ||
| //│ let value, result1$, result2$; | ||
| //│ match caseScrut | ||
| //│ Box⁰ => | ||
| //│ let inlinedVal; | ||
| //│ set value = caseScrut.value⁰; | ||
| //│ match value | ||
| //│ 0 => | ||
| //│ set inlinedVal = { "p_1": true, "p_2": false }; | ||
| //│ end | ||
| //│ 1 => | ||
| //│ set inlinedVal = { "p_1": false, "p_2": true }; | ||
| //│ end | ||
| //│ else | ||
| //│ set inlinedVal = { "p_1": false, "p_2": false }; | ||
| //│ end | ||
| //│ set result1$ = inlinedVal.p_1﹖; | ||
| //│ match result1$ | ||
| //│ true => | ||
| //│ return 1 | ||
| //│ else | ||
| //│ set result2$ = inlinedVal.p_2﹖; | ||
| //│ match result2$ | ||
| //│ true => | ||
| //│ return 1 | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ end | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ }; | ||
| //│ return lambda⁴ | ||
| //│ —————————————————| Output |————————————————————————————————————————————————————————————————————————— | ||
| //│ = fun | ||
|
|
||
| :soir | ||
|
chengluyu marked this conversation as resolved.
Outdated
|
||
| case @compile (Box(0) & Box(0 | 1)) then 1 | ||
| //│ ——————————————| Optimized IR |—————————————————————————————————————————————————————————————————————— | ||
| //│ let lambda; | ||
| //│ @private | ||
| //│ define lambda as fun lambda⁵(caseScrut) { | ||
| //│ let value, result1$, result2$; | ||
| //│ match caseScrut | ||
| //│ Box⁰ => | ||
| //│ let inlinedVal; | ||
| //│ set value = caseScrut.value⁰; | ||
| //│ match value | ||
| //│ 0 => | ||
| //│ set inlinedVal = { "p_1": true, "p_2": true }; | ||
| //│ end | ||
| //│ 1 => | ||
| //│ set inlinedVal = { "p_1": false, "p_2": true }; | ||
| //│ end | ||
| //│ else | ||
| //│ set inlinedVal = { "p_1": false, "p_2": false }; | ||
| //│ end | ||
| //│ set result1$ = inlinedVal.p_1﹖; | ||
| //│ match result1$ | ||
| //│ true => | ||
| //│ set result2$ = inlinedVal.p_2﹖; | ||
| //│ match result2$ | ||
| //│ true => | ||
| //│ return 1 | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ else | ||
| //│ throw new globalThis⁰.Error⁰("match error") | ||
| //│ end | ||
| //│ }; | ||
| //│ return lambda⁵ | ||
| //│ —————————————————| Output |————————————————————————————————————————————————————————————————————————— | ||
| //│ = fun | ||
|
|
||
| class Add2(val x, val y, z) | ||
|
|
||
| :e | ||
| case @compile (Add2(0, _, _) | Add2(_, 1, _)) then 0 | ||
| //│ ╔══[COMPILATION ERROR] Parameter `z` is not accessible. | ||
| //│ ║ l.188: class Add2(val x, val y, z) | ||
| //│ ╙── ^ | ||
| //│ ╔══[COMPILATION ERROR] Parameter `z` is not accessible. | ||
| //│ ║ l.188: class Add2(val x, val y, z) | ||
| //│ ╙── ^ | ||
| //│ = fun | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.