Remove arity-interrupting elaboration of module patterns#146
Merged
goldfirere merged 20 commits intoocaml-flambda:mainfrom Mar 15, 2023
Merged
Remove arity-interrupting elaboration of module patterns#146goldfirere merged 20 commits intoocaml-flambda:mainfrom
goldfirere merged 20 commits intoocaml-flambda:mainfrom
Conversation
e69579e to
5cd891e
Compare
goldfirere
reviewed
Mar 14, 2023
Contributor
goldfirere
left a comment
There was a problem hiding this comment.
This looks like it works well!
But I believe I found a few opportunities for simplification -- see line comments.
3cc3156 to
717265a
Compare
goldfirere
reviewed
Mar 15, 2023
"inner scope than the current level" doesn't typecheck
7f0563e to
bd29267
Compare
Co-authored-by: Richard Eisenberg <rae@richarde.dev>
goldfirere
approved these changes
Mar 15, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Remove the elaboration of module patterns, where typechecking would insert
let-moduleexpressions for each unpack. For example, this:used to be elaborated to this during typechecking:
The same went for match expressions (including
whenguards!) and let expressions (including the RHS when the let binding was recursive).Now, we treat the existing
Tpat_varconstruct in typedtree as unpacking a module pattern (as long aspat_extra = Tpat_unpack). To typecheck in the new scheme, we:The mechanism we use for this is similar to
gadt_equations_level, in that we wrap pattern typechecking its own level and store the elevated scope in a global ref that pattern typechecking refers to. (In particular, we repurpose theallow_modulesbool to anint option, where the int payload communicates the scope to enter the module identifier at.) Likegadt_equations_level, we only elevate the level if the pattern contains the relevant kind of subpattern (here, a module unpack).We want to gain confidence not only that this PR avoids scope escape, but that future constructs involving patterns avoid scope escape. Previously, the way this was accomplished was via the
type_unpacksfunction, which typechecked a body expression within the scope of a list of unpacked modules, and confirmed the modules' types didn't escape via the body's type. In this world, it was easy to see that scope escape was checked anywhere a module was used. Now, the responsibility is more diffuse. But we can still state the matter simply. The responsibility to check scope escape falls to whoever creates the scope such that pattern typechecking setsallow_modules = Modules_allowed { scope }. You can see this responsibility in action in both thetype_casesandtype_letfunctions. This is summarized in a comment by themodule_patterns_restrictiontype.type_unpacksnow doesn't do much at all. I clean it up in a stacked PR (ncik-roberts#1).I add a few tests that did not always exhibit the desired behavior over the course of my feature.