Fix issue when inlining complex constants in pattern matching. #6471
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The compiler's back-end has optimisations to inline complex constants. This can interfere with pattern matching compilation. In the tests, a person can be a Student or a Teacher. Because of inlining, the value one pattern matches on is known to be of type Teacher. However, the compilation of pattern matching still generates code for both student and teacher. The (dead) code for student is generated, but the inlined value has type teacher. This means that an attempt is made to access non-existent field "status" of teacher. Notice one could even rename "status" to "age" in Student, and the filed would exist, but just be of unexpected type. So the constant age for Teacher is interpreted as a status value (the second field of the inline record). The compiler optimisation expects to find a valid constant for status, while it finds the age. This PR catches this situation and does not try to follow a specific branch of the "status" cases but reverts to the general case.