Pattern match failure should be caught at compile-time

Bug #408411 reported by Matt Giuca
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mars
Fix Released
Wishlist
Matt Giuca

Bug Description

If a switch statement does not cover all possibilities (ie. have a case for each alternative of the type, or have a wildcard case), then the program should be rejected.

Currently a pattern match failure error is thrown at runtime. See the XXX in interpret.m under exec_cases.

Tags: language

Related branches

Matt Giuca (mgiuca)
Changed in mars:
assignee: nobody → Matt Giuca (mgiuca)
importance: Undecided → Wishlist
status: New → Confirmed
Matt Giuca (mgiuca)
Changed in mars:
status: Confirmed → Triaged
Matt Giuca (mgiuca)
tags: added: language
Revision history for this message
Matt Giuca (mgiuca) wrote :

This is a requirement of the language spec:

"It must be statically provable that all possible values are handled by at least one case statement, or the compiler MUST reject the program. This can be proven either by having a wildcard pattern (underscore or variable), or by matching all alternatives of a user-defined data type.

The current implementation does not obey this last rule. Currently, this check is made at runtime, not compile time. If no case of a switch statement matches, the program will terminate with a runtime error, “pattern match failure”."

Revision history for this message
Matt Giuca (mgiuca) wrote :

Before fixing this, perform switch factoring:
(Output: Warnings and replaces statements).
1. If there are any cases after the first any/var, eliminate them and give a warning.
2. Add a default case with a "pass" statement, if there is none, for consistency. This ensures exactly one path is taken (similar to how we add an "else: pass" on an if statement without one).
3. Recursively factor all cases.
4. Combine any cases with the same spine.

The resulting switch construct (e.g., in the CFG representation) could therefore be simpler, not requiring recursive patterns. Also, it may be best if it doesn't have the ability to bind variables, and instead the bodies of the case blocks can pull variables out of user-defined types.

Now it is trivial to produce an error if not all cases are handled.

Revision history for this message
Matt Giuca (mgiuca) wrote :

The above strategy for switch factoring is vastly simplistic. A full algorithm is documented in the official docs (Switch factoring algorithm) in the switchfactor branch (bound to this bug).

Matt Giuca (mgiuca)
Changed in mars:
status: Triaged → In Progress
Revision history for this message
Matt Giuca (mgiuca) wrote :

Fix committed to branch switchfactor. Needs merging into trunk.

Changed in mars:
milestone: none → 0.3
Revision history for this message
Matt Giuca (mgiuca) wrote :

Fixed in trunk r952.

Changed in mars:
status: In Progress → Fix Committed
Matt Giuca (mgiuca)
Changed in mars:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.