Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue when inlining complex constants in pattern matching. #6471

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

cristianoc
Copy link
Collaborator

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.

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.
Copy link
Collaborator

@zth zth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! ⭐

@cristianoc cristianoc merged commit 124f1ad into master Nov 3, 2023
7 checks passed
@cristianoc cristianoc deleted the inline-pattern-matching branch November 3, 2023 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants