Repository navigation
Fix missing check of illegal recursive module when using module type substitutions - #13829
Conversation
|
If you target trunk with this, you'll get nicer error messages (#13646) |
ContextTo give a bit more context, this PR addresses the module-type substitutions ( module rec X : sig module type A module X' : A end
with module type A = sig type t end
= ...
and Y : sig type u = X.X'.t end = ...
(* Error: The module X.X' is abstract, it cannot have any components *)This PR
Remaining issueWhile it fixes the cyclicity bug and (marginally) improve the expressivity, it can lead to accepting invalid substitutions during the approximation phase that are then caught when typechecking the signatures: module rec X : sig module type A = sig type t end end
with module type A = sig type t = int end During approximation, we merge : |
|
From a distance it is not clear why |
* Changed `approx_modtype` to not ignore module type substitutions but instead approximate and merge them * Change with_info to have `merge_constraint` either build the typed tree or not, when used for approximated signatures (for which the typed tree will not be built)
e4e0d7c to
1384135
Compare
* Factor out the with_modtype and approx_with_modtype cases with a new function `patch_modtype_item` * Updated error messages on tests when rebasing on trunk
|
I've rebased the pr on trunk. |
| let initial_sig = extract_sig env sbody.pmty_loc approx_body in | ||
| (* then, the constraints are approximated and merged *) | ||
| Mty_signature (List.fold_left | ||
| (approx_constraint env) initial_sig constraints) |
There was a problem hiding this comment.
I think you can document that this is equivalent for well-typed substitution to merging the constraint then approximating since inlining the declaration then approximating the body is equivalent to approximating the declaration body then inlining it in all contexts (module type t = < >, include < >, me : < >) in which a module type path may appear.
There was a problem hiding this comment.
Added as comments
| [%%expect {| | ||
| module rec X : sig module X' : sig type t end end | ||
| and Y : sig type u = X.X'.t end | ||
| |}] |
There was a problem hiding this comment.
You are missing a case for a signature replacement
module type S = sig
module type T = sig
val x: int
val y: int
end
module X:T
end
module rec M: S with module type T := sig val x: int val y:int end =
struct
module X= struct let x = 0 let y = 1 end
end(which currently fails)
and you should add a test for the corner case where we have a ill-typed substitution but with a well-typed approximated substitution.
There was a problem hiding this comment.
Added the corresponding test (and destructive/deep variants)
|
I've added the missing tests:
Writing the latter lead me to uncover a regression introduced by this PR (which I believe is benign): merging concrete (non-abstract) module types induce an equivalence check which might fail when the original definition in the body was not approximated (but the constraint was). It is the case when using named module types, which are not approximated. The following code (added as a test) fails: module type Test = sig
module type S = sig module type A = sig type t = int end end
module rec X : S with module type A = sig type t = int end
endThe constraint is approximated into I have the following questions :
|
|
I would propose option
Indeed, when the bootstrapping the recursive module signature we care about constructing a skeleton that contain all types, module types and modules declaration and doesn't contain cross-references to other modules. When constraining a module type, this is achieved with both the LHS and RHS. And if the two module type approximations were in fact not equivalent, the inclusion check at the next round will fail. |
The equivalence check between a module type constraint and the original definition is ignored during approximation, as we are just interested in the namespace.
|
|
||
|
|
||
| (* Defining module types before the recursive module prevents them from being | ||
| approximated and can (erroneously) make the constraint ill-formed *) |
There was a problem hiding this comment.
The comment needs to be updated.
There was a problem hiding this comment.
Updated the comment and the test to show that approximation allows for ill-formed merging of constraints, before the ill-formedness is caught by the typechecking phase
| the approximation in the environment). | ||
|
|
||
| 2. It can erroneously fail because the constraint has been approximated | ||
| but the body has not (named module types escape approximation). |
There was a problem hiding this comment.
The new comment above disabling the equivalence check should probably be merged here.
There was a problem hiding this comment.
I merged the two comments
- Added a test to ensure that approximation allows for ill-formed modtype constraints (but the ill-formedness is caught during the typechecking phase) - Added the missing With_modtype case for the mechanism that preserves aliases when substituting a deep field
| module X' : A | ||
| end with module type A = sig type t = int end) | ||
| and Y : sig type u = X.X'.t end | ||
| end |
There was a problem hiding this comment.
This is the same test as the one above?
There was a problem hiding this comment.
Indeed, it's removed now
| non-recursive module types that can be used as an under-approximation | ||
| of the name-spaces for the typechecking phase, the equivalence check is | ||
| disabled, allowing for ill-formed constraints to be merged. It is | ||
| believed to be harmless, because the ill-formedness is caught when |
There was a problem hiding this comment.
The code is ready in my opinion, but the comments should be less overly cautious.
There was a problem hiding this comment.
Updated to "should be harmless"
|
One test need to be updated due to a recent improvement of the error location for subcomponent of Longident paths. |
…substitutions (backport ocaml/ocaml#13829)
…substitutions (backport ocaml/ocaml#13829)
…substitutions (backport ocaml/ocaml#13829)
…substitutions (backport ocaml/ocaml#13829) (#4241)
…substitutions (backport ocaml/ocaml#13829) (#4241)
This PR fixes #13172 by resolving module type substitutions, approximating the result and merging them with the main signature. It prevent accepting invalid cyclic signature that previously threw the typechecker into an infinite loop later on.
This used to create an infinite loop:
Now, it correctly fails with: