Skip to content

Fix missing check of illegal recursive module when using module type substitutions - #13829

Merged
Octachron merged 18 commits into
ocaml:trunkfrom
clementblaudeau:recsig-substitution-cyclic-check
Mar 26, 2025
Merged

Octachron merged 18 commits into
ocaml:trunkfrom
clementblaudeau:recsig-substitution-cyclic-check

Conversation

@clementblaudeau

Copy link
Copy Markdown
Contributor

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:

module rec X : (sig module type A end with module type A = X.A) 
  = struct module type A end

Now, it correctly fails with:

Error: Illegal recursive module reference

@smuenzel

Copy link
Copy Markdown
Member

If you target trunk with this, you'll get nicer error messages (#13646)

@clementblaudeau

Copy link
Copy Markdown
Contributor Author

Context

To give a bit more context, this PR addresses the module-type substitutions (with module type T = ..., with module type T := ...) in the signatures of recursive modules. Before, those were ignored during the approximation phase of the typing of recursive modules. It lead to accepting cyclic definition that threw the typechecker into an infinite loop (#13172). It was also making such code fail to typecheck:

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

  1. When dealing with a definition of the form module rec X : S with module type A = S', this PR approximates S, then S', then merge the results (replace occurrences of A by the approximation of S'). We assume that approximating and merging commute, ie that it is equivalent to merging S and S' and approximating the result afterwards (which is not easy to implement for technical reasons).

  2. Besides, this PR refactor the merge_constraint function, as it is used in three different contexts : (1) for merging during typechecking, (2) for merging of package types, (3) for merging during approximation, and the last two cases do not require the building of a typed tree. The previous function is renamed into merge_constraint_aux, and now returns a merge_result. Three functions merge_constraint, merge_package_constraint and merge_constraint_approx are provided to serve as interface. They enclose the pattern match on the merge_result, which should contain a typed tree in normal mode and none in other cases -- they fail (fatal error) if the result is not as expected.

Remaining issue

While 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 : sig module type A = sig type t end with sig type t end, which will succeed. Therefore, the typechecking phase of the recursive signatures is reached, and the environment contains a module X with the signature sig module type A = sig type t end. However, at this point, the invalid substitution is caught (type t = int is not equivalent to type t) and the example is rejected. I don't know if it could be exploited further into a serious bug.

@gasche

gasche commented Mar 3, 2025

Copy link
Copy Markdown
Member

From a distance it is not clear why Approx_with_modtype{,subst} are added to the existing type of with constraints (with_info in trunk), instead of forming a separate type just for them (with_approx_info?). This would make the change less invasive (and possibly a bit simpler). Maybe it would force you to duplicate code, which could be moved to auxiliary functions. Have you tried that approach?

@clementblaudeau
clementblaudeau changed the base branch from 5.3 to trunk March 3, 2025 16:06
@clementblaudeau
clementblaudeau changed the base branch from trunk to 5.3 March 3, 2025 16:07
* 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)
@clementblaudeau
clementblaudeau force-pushed the recsig-substitution-cyclic-check branch from e4e0d7c to 1384135 Compare March 3, 2025 16:14
@clementblaudeau
clementblaudeau changed the base branch from 5.3 to trunk March 3, 2025 16:14
* 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
@clementblaudeau

Copy link
Copy Markdown
Contributor Author

I've rebased the pr on trunk.
As a compromise, I've factor out the common code between the with_modtype cases and approx_with_modtype. The next step is to do a cleaner refactoring of merge_constraint, splitting it into separate functions for each kind of constraint, as they only share the recursive case (when going into submodules). It believe that it might be better to put such refactoring a separate PR ?

Comment thread typing/typemod.ml
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added as comments

[%%expect {|
module rec X : sig module X' : sig type t end end
and Y : sig type u = X.X'.t end
|}]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the corresponding test (and destructive/deep variants)

@clementblaudeau

Copy link
Copy Markdown
Contributor Author

I've added the missing tests:

  1. concrete signatures replaced by concrete signatures (both destructive and non-destructive)
  2. deep constraints (in submodules)
  3. corner-case where approximation makes a ill-formed constraint temporary valid (but is then caught by the re-typecheck)

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
end

The constraint is approximated into sig type t end, not a valid replacement of the original definition sig type t = int end which was not approximated. This example is however somewhat contrived: it only occurs when substituting an original signature that was well-formed outside of the recursive knot. Note that merging and then approximating (the inverse order of this PR) is not doable here (besides technical difficulties): the equivalence check is not defined on ill-formed signatures, and there is not way to check wellformedness of constraints before the end of the approximation phase.

I have the following questions :

  1. This regression could be (a) fixed by ignoring the constraint if the merging fails (fallback on previous behavior), (b) fixed by ignoring module type constraints on concrete module types (as they should not allow for more typechecking, nor throw the typechecker into infinite loops), (c) fixed by forcing the approximation of the body even when it is a named module type (which is technically painful) or (d) not fixed.
  2. Should the interaction of constraints and approximation be documented in the manual ?

@Octachron

Copy link
Copy Markdown
Member

I would propose option e:

  • ignore the inclusion check in the approximation mode

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 *)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comment needs to be updated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Comment thread typing/typemod.ml Outdated
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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new comment above disabling the equivalence check should probably be merged here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the same test as the one above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it's removed now

Comment thread typing/typemod.ml Outdated
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code is ready in my opinion, but the comments should be less overly cautious.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to "should be harmless"

@Octachron

Copy link
Copy Markdown
Member

One test need to be updated due to a recent improvement of the error location for subcomponent of Longident paths.

Comment thread Changes Resolved
@Octachron
Octachron merged commit 6b08d8f into ocaml:trunk Mar 26, 2025
rtjoa added a commit to oxcaml/oxcaml that referenced this pull request Jun 30, 2025
rtjoa added a commit to oxcaml/oxcaml that referenced this pull request Jul 2, 2025
rtjoa added a commit to oxcaml/oxcaml that referenced this pull request Jul 3, 2025
ccasin pushed a commit to oxcaml/oxcaml that referenced this pull request Jul 5, 2025
ccasin pushed a commit to oxcaml/oxcaml that referenced this pull request Jul 5, 2025
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.

Module type substitutions in signature of recursive modules are not checked for cyclicity

4 participants