scc1: a when whose branches all return is not recognized as returning
Nobody has claimed this yet.
- Dominant language
- Macaulay2
- Stars
- 435
- Forks
- 297
- Avg merge
- 4d 20h
- Merged PRs (30d)
- 11
Description
This issue was triaged from [`bugs/dan/0.5-SafeC-bugs`](https://github.com/Macaulay2/M2/blob/388c1ff0ce30d83751dea7bc7eac77fdc1305dd7/bugs/dan/0.5-SafeC-bugs), one of the 857 files removed from the pre-GitHub `bugs/` tree by [`d2c8d27826`](https://github.com/Macaulay2/M2/commit/d2c8d27826) and catalogued in [#36](https://github.com/Macaulay2/M2/issues/36). **The commentary below was written by Claude (Claude Opus 5, via Claude Code)**, not by @d-torrance, whose account posted it -- please weigh it accordingly.
### The original file, verbatim
```text
This gives a type mismatch:
yyy := {+ i:int };
zzz := {+ j:int };
uuu := zzz or yyy or null ;
bbb := yyy(1);
ccc := uuu(bbb);
fff(k:uuu):int := when k is yyy do 1 is zzz do 2 is null do 3;
f():int := (
when ccc
is yyy do return 1
is zzz do return 2
is null do return 3;
);
It seems that "when" should do the same thing "if" does, and report whether all of its branches return.
```
### Where it stands today
Reproduces exactly under a current `scc1`: the file's lines give `error: type mismatch` at line 8 —
the `f():int := ( when ... )` whose every branch returns.
### The distinguishing detail
Line 6 of the same file, the *expression* form of an equivalent `when`, compiles:
```
fff(k:uuu):int := when k is yyy do 1 is zzz do 2 is null do 3; -- fine
```
so it is specifically the **statement form with `return` in all branches** that `scc1` fails to
recognize as returning. The file's own diagnosis is right: `when` should do what `if` does and report
whether all of its branches return.
### Why it matters
The workaround is to restructure the function so the `when` is an expression, which is not always
natural — and the error message names a type mismatch, giving no hint that the problem is flow analysis
rather than types. Anyone editing `.d` code hits this and has no way to know what they did wrong.
Adjacent: **#4525** is the other `scc1` `when` defect (a fully-covered `when` emits its `else` as
unreachable code), so the two are worth fixing together.
`open` · disposition `issue` · source of truth: [`bug-triage/catalog.tsv`](https://github.com/d-torrance/M2/blob/bug-triage/bug-triage/catalog.tsv)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the shown .d example with the current scc1, comparing the statement-form when with the equivalent expression form. Trace return and flow analysis for when, alongside related issue #4525; done means an all-returning statement-form when compiles without the type-mismatch error and has regression coverage.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100