Macaulay2 / Macaulay2/M2

scc1 emits the else clause of an exhaustive when as unreachable code

Open
#4,525 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bugs directory Interpreter
Dominant language
Macaulay2
Stars
435
Forks
297
Avg merge
4d 20h
Merged PRs (30d)
11

Description

This issue was triaged from [`bugs/dan/0-when`](https://github.com/Macaulay2/M2/blob/388c1ff0ce30d83751dea7bc7eac77fdc1305dd7/bugs/dan/0-when), 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
A := {+ x:int };
B := {+ y:int };
C := A or B;
f(x:C):int := when x is A do 1 is B do 2 else 3; -- oops! This needs to be fixed.
```

### Where it stands today

`scc1` still miscompiles it, and the failure is silent dead code rather than a diagnostic.

With every case of the union covered:

```
A := {+ x:int };
B := {+ y:int };
C := A or B;
f(x:C):int := when x is A do 1 is B do 2 else 3;
```

`chk.c:783-794` labels only the *uncovered* types, so with `A` and `B` both handled there is no label
left for the `else` body — and it is emitted inside the switch with no case label at all. The result is
unreachable code that nothing diagnoses.

### Why it is worth fixing rather than avoiding

An `else` that cannot run is exactly the situation where a compiler should say so. As it stands, a `.d`
author who writes a defensive `else` gets no warning that it is inert, and a later change that *adds* a
member to the union will silently start routing through the newly-reachable branch — so the bug is
latent rather than merely cosmetic.

Adjacent: **#4542** is the other `when` defect in `scc1` (a statement-form `when` whose branches all
return is not recognized as returning), so the two are worth looking at 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the exhaustive `when` example from the issue, then inspect `chk.c:783-794`, where `scc1` labels uncovered union types. Trace how the `else` body is emitted when every type is covered; done means the compiler no longer silently emits that body as unlabeled unreachable code and the behavior is checked against the example.

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
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.