Skip to content

fix(compiler): Correct exhaustive check on record adt with when pattern#2355

Merged
ospencer merged 4 commits intograin-lang:mainfrom
spotandjake:spotandjake/issue_2311
Mar 28, 2026
Merged

fix(compiler): Correct exhaustive check on record adt with when pattern#2355
ospencer merged 4 commits intograin-lang:mainfrom
spotandjake:spotandjake/issue_2311

Conversation

@spotandjake
Copy link
Copy Markdown
Member

This pr corrects exhaustive checking when inlined records are mixed with when clauses, like in the example below:

enum T {
  A{ n: Number, },
  B{ n: Number, },
}
match (B{ n: 1 }) {
  A{ n } => void,
  B{ n: 0 } when false => void,
}

Previously, we were not providing a warning, which was due to a small bug in Conv, where we didn't propagate any Any pattern. This handles the edge case correctly, allowing a warning to show up. However, I think we may want to take a deeper look into pattern matching semantics eventually, as the Conv logic feels fragile in general.

Closes: #2311

Comment thread compiler/src/typed/parmatch.re Outdated
@@ -2153,6 +2153,11 @@ module Conv = {
};
Hashtbl.add(constrs, id, cstr);
switch (lst) {
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.

Let's change this to match on both the values as well as cstr_inlined, explicitly matching on the Some(_) and None cases.

In the Some case, for the constructor values, if the list is empty, we should fail. If the list contains more than one item, we should fail. We then need to handle all of the cases that are valid. Is it just Any? Feels like Or and Alias are valid here too.

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.

Instead of just handling all the casees I refactored this a tiny bit so that it's more explicit what type a constructor is.

@spotandjake spotandjake force-pushed the spotandjake/issue_2311 branch from 8a179c0 to 4b98be1 Compare February 11, 2026 19:00
@ospencer ospencer added this pull request to the merge queue Mar 28, 2026
Merged via the queue into grain-lang:main with commit 5e372e5 Mar 28, 2026
12 checks passed
@github-actions github-actions Bot mentioned this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exhaustiveness failure on record variants combined with when patterns.

2 participants