Commit b60f021
Fix unbalanced if as expression: always yields a value (unit when false)
Previously `if cond { val }` without an else branch was stack-neutral
(net-zero), which meant `return if cond { val }` would pop the wrong
stack slot. The fix has two parts:
1. `compile_if` (no-else): emit a Jump/else-path that pushes unit so
the expression always leaves exactly one value on the stack.
2. Parser `block()`: wrap non-last, non-Statement block items in
`to_statement()` so `Expression::Statement` handles value cleanup
as designed, rather than leaking the if's result onto the stack.
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>1 parent 53c6616 commit b60f021
4 files changed
Lines changed: 35 additions & 5 deletions
File tree
- compiler_tests/tests
- ndc_parser/src
- ndc_vm/src
- tests/programs/998_not_desired
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
40 | 51 | | |
41 | 52 | | |
42 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1225 | 1225 | | |
1226 | 1226 | | |
1227 | 1227 | | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
1228 | 1243 | | |
1229 | 1244 | | |
1230 | 1245 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
511 | 511 | | |
512 | 512 | | |
513 | 513 | | |
| 514 | + | |
514 | 515 | | |
515 | 516 | | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
516 | 520 | | |
517 | 521 | | |
518 | 522 | | |
| |||
898 | 902 | | |
899 | 903 | | |
900 | 904 | | |
901 | | - | |
902 | 905 | | |
903 | 906 | | |
904 | 907 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
0 commit comments