@@ -552,11 +552,15 @@ type with_info =
552
552
| With_modsubst of Longident .t loc * Path .t * Types .module_declaration
553
553
| With_modtype of Typedtree .module_type
554
554
| With_modtypesubst of Typedtree .module_type
555
+ | With_type_package of Typedtree .core_type
556
+ (* Package with type constraints only use this last case. Normal module
557
+ with constraints never use it. *)
555
558
556
559
let merge_constraint initial_env loc sg lid constr =
557
560
let destructive_substitution =
558
561
match constr with
559
- | With_type _ | With_module _ | With_modtype _ -> false
562
+ | With_type _ | With_type_package _ | With_module _
563
+ | With_modtype _ -> false
560
564
| With_typesubst _ | With_modsubst _ | With_modtypesubst _ -> true
561
565
in
562
566
let real_ids = ref [] in
@@ -634,7 +638,7 @@ let merge_constraint initial_env loc sg lid constr =
634
638
in
635
639
return ~ghosts
636
640
~replace_by: (Some (Sig_type (id, newdecl, rs, priv)))
637
- (Pident id, lid, Twith_type tdecl)
641
+ (Pident id, lid, Some ( Twith_type tdecl) )
638
642
| Sig_type (id, sig_decl, rs, priv) , [s],
639
643
(With_type sdecl | With_typesubst sdecl as constr)
640
644
when Ident. name id = s ->
@@ -651,12 +655,47 @@ let merge_constraint initial_env loc sg lid constr =
651
655
With_type _ ->
652
656
return ~ghosts
653
657
~replace_by: (Some (Sig_type (id, newdecl, rs, priv)))
654
- (Pident id, lid, Twith_type tdecl)
658
+ (Pident id, lid, Some ( Twith_type tdecl) )
655
659
| (* With_typesubst *) _ ->
656
660
real_ids := [Pident id];
657
661
return ~ghosts ~replace_by: None
658
- (Pident id, lid, Twith_typesubst tdecl)
662
+ (Pident id, lid, Some ( Twith_typesubst tdecl) )
659
663
end
664
+ | Sig_type (id, sig_decl, rs, priv), [s], With_type_package cty
665
+ when Ident. name id = s ->
666
+ begin match sig_decl.type_manifest with
667
+ | None -> ()
668
+ | Some ty ->
669
+ raise (Error (loc, outer_sig_env, With_package_manifest (lid.txt, ty)))
670
+ end ;
671
+ let tdecl =
672
+ Typedecl. transl_package_constraint ~loc cty.ctyp_type
673
+ in
674
+ (* Here we constrain the jkind of "with type" manifest by the jkind from
675
+ the declaration from the original signature. Note that this is also
676
+ checked in [check_type_decl], but there it is check, not constrain,
677
+ which we need here to deal with type variables in package constraints
678
+ (see tests in [typing-modules/package_constraint.ml]). *)
679
+ begin match
680
+ Ctype. constrain_decl_jkind initial_env tdecl sig_decl.type_jkind
681
+ with
682
+ | Ok _ -> ()
683
+ | Error v ->
684
+ (* This is morally part of the below [check_type_decl], so we give the
685
+ same error that would be given there for good error messages. *)
686
+ let err =
687
+ Includemod.Error. In_Type_declaration (
688
+ id, Type_declarations
689
+ {got= tdecl;
690
+ expected= sig_decl;
691
+ symptom= Includecore. Jkind v})
692
+ in
693
+ raise Includemod. (Error (initial_env, err))
694
+ end ;
695
+ check_type_decl outer_sig_env sg_for_env loc id None tdecl sig_decl;
696
+ let tdecl = { tdecl with type_manifest = None } in
697
+ return ~ghosts ~replace_by: (Some (Sig_type (id, tdecl, rs, priv)))
698
+ (Pident id, lid, None )
660
699
| Sig_modtype (id, mtd, priv), [s],
661
700
(With_modtype mty | With_modtypesubst mty)
662
701
when Ident. name id = s ->
@@ -678,15 +717,16 @@ let merge_constraint initial_env loc sg lid constr =
678
717
in
679
718
return
680
719
~replace_by: (Some (Sig_modtype (id, mtd', priv)))
681
- (Pident id, lid, Twith_modtype mty)
720
+ (Pident id, lid, Some ( Twith_modtype mty) )
682
721
else begin
683
722
let path = Pident id in
684
723
real_ids := [path];
685
724
begin match mty.mty_type with
686
725
| Mty_ident _ -> ()
687
726
| mty -> unpackable_modtype := Some mty
688
727
end ;
689
- return ~replace_by: None (Pident id, lid, Twith_modtypesubst mty)
728
+ return ~replace_by: None
729
+ (Pident id, lid, Some (Twith_modtypesubst mty))
690
730
end
691
731
| Sig_module (id, pres, md, rs, priv), [s],
692
732
With_module {lid= lid'; md= md'; path; remove_aliases}
@@ -700,7 +740,7 @@ let merge_constraint initial_env loc sg lid constr =
700
740
newmd.md_type md.md_type);
701
741
return
702
742
~replace_by: (Some (Sig_module (id, pres, newmd, rs, priv)))
703
- (Pident id, lid, Twith_module (path, lid'))
743
+ (Pident id, lid, Some ( Twith_module (path, lid') ))
704
744
| Sig_module (id, _, md, _rs, _), [s], With_modsubst (lid',path,md')
705
745
when Ident. name id = s ->
706
746
let sig_env = Env. add_signature sg_for_env outer_sig_env in
@@ -709,7 +749,8 @@ let merge_constraint initial_env loc sg lid constr =
709
749
(Includemod. strengthened_module_decl ~loc ~mark: Mark_both
710
750
~aliasable sig_env md' path md);
711
751
real_ids := [Pident id];
712
- return ~replace_by: None (Pident id, lid, Twith_modsubst (path, lid'))
752
+ return ~replace_by: None
753
+ (Pident id, lid, Some (Twith_modsubst (path, lid')))
713
754
| Sig_module (id, _, md, rs, priv) as item, s :: namelist, constr
714
755
when Ident. name id = s ->
715
756
let sig_env = Env. add_signature sg_for_env outer_sig_env in
@@ -743,7 +784,7 @@ let merge_constraint initial_env loc sg lid constr =
743
784
check_usage_after_substitution ~loc ~lid initial_env ! real_ids
744
785
! unpackable_modtype sg;
745
786
let sub = match tcstr with
746
- | (_ , _ , Twith_typesubst tdecl ) ->
787
+ | (_ , _ , Some ( Twith_typesubst tdecl ) ) ->
747
788
let how_to_extend_subst =
748
789
let sdecl =
749
790
match constr with
@@ -768,7 +809,7 @@ let merge_constraint initial_env loc sg lid constr =
768
809
let sub = Subst. change_locs Subst. identity loc in
769
810
let sub = List. fold_left how_to_extend_subst sub ! real_ids in
770
811
Some sub
771
- | (_ , _ , Twith_modsubst (real_path , _ )) ->
812
+ | (_ , _ , Some ( Twith_modsubst (real_path , _ ) )) ->
772
813
let sub = Subst. change_locs Subst. identity loc in
773
814
let sub =
774
815
List. fold_left
@@ -777,7 +818,7 @@ let merge_constraint initial_env loc sg lid constr =
777
818
! real_ids
778
819
in
779
820
Some sub
780
- | (_ , _ , Twith_modtypesubst tmty ) ->
821
+ | (_ , _ , Some ( Twith_modtypesubst tmty ) ) ->
781
822
let add s p = Subst. add_modtype_path p tmty.mty_type s in
782
823
let sub = Subst. change_locs Subst. identity loc in
783
824
let sub = List. fold_left add sub ! real_ids in
@@ -805,90 +846,15 @@ let merge_constraint initial_env loc sg lid constr =
805
846
with Includemod. Error explanation ->
806
847
raise(Error (loc, initial_env, With_mismatch (lid.txt, explanation)))
807
848
808
- (* A simplified version of [merge_constraint] for the case of packages. Package
809
- constraints are much simpler - they must be "with type" constraints for types
810
- with no parameters and can only update abstract types. *)
811
849
let merge_package_constraint initial_env loc sg lid cty =
812
- let rec patch_item namelist outer_sig_env sg_for_env ~ghosts item =
813
- let return replace_by =
814
- Some (() , {Signature_group. ghosts; replace_by})
815
- in
816
- match item, namelist with
817
- | Sig_type (id, sig_decl, rs, priv) , [s]
818
- when Ident. name id = s ->
819
- begin match sig_decl.type_manifest with
820
- | None -> ()
821
- | Some ty ->
822
- raise (Error (loc, outer_sig_env, With_package_manifest (lid.txt, ty)))
823
- end ;
824
- let new_sig_decl =
825
- Typedecl. transl_package_constraint ~loc cty.ctyp_type
826
- in
827
- (* Here we constrain the jkind of "with type" manifest by the jkind from
828
- the declaration from the original signature. Note that this is also
829
- checked in [check_type_decl], but there it is check, not constrain,
830
- which we need here to deal with type variables in package constraints
831
- (see tests in [typing-modules/package_constraint.ml]). *)
832
- begin match
833
- Ctype. constrain_decl_jkind initial_env new_sig_decl
834
- sig_decl.type_jkind
835
- with
836
- | Ok _ -> ()
837
- | Error v ->
838
- (* This is morally part of the below [check_type_decl], so we give the
839
- same error that would be given there for good error messages. *)
840
- let err =
841
- Includemod.Error. In_Type_declaration (id,
842
- Type_declarations
843
- {got= new_sig_decl;
844
- expected= sig_decl;
845
- symptom= Includecore. Jkind v})
846
- in
847
- raise Includemod. (Error (initial_env, err))
848
- end ;
849
- check_type_decl outer_sig_env sg_for_env loc id None
850
- new_sig_decl sig_decl;
851
- let new_sig_decl = { new_sig_decl with type_manifest = None } in
852
- return (Some (Sig_type (id, new_sig_decl, rs, priv)))
853
- | Sig_module (id, _, md, rs, priv) as item, s :: namelist
854
- when Ident. name id = s ->
855
- let sig_env = Env. add_signature sg_for_env outer_sig_env in
856
- let sg = extract_sig sig_env loc md.md_type in
857
- let (() , newsg) = merge_signature sig_env sg namelist in
858
- let item =
859
- match md.md_type with
860
- Mty_alias _ ->
861
- (* A module alias cannot be refined, so keep it
862
- and just check that the constraint is correct *)
863
- item
864
- | _ ->
865
- let newmd = {md with md_type = Mty_signature newsg} in
866
- Sig_module (id, Mp_present , newmd, rs, priv)
867
- in
868
- return (Some item)
869
- | _ -> None
870
- and merge_signature env sg namelist =
871
- match
872
- Signature_group. replace_in_place (patch_item namelist env sg) sg
873
- with
874
- | Some (x ,sg ) -> x, sg
875
- | None -> raise(Error (loc, env, With_no_component lid.txt))
876
- in
877
- try
878
- let names = Longident. flatten lid.txt in
879
- let (tcstr, sg) = merge_signature initial_env sg names in
880
- check_well_formed_module initial_env loc " this instantiated signature"
881
- (Mty_signature sg);
882
- (tcstr, sg)
883
- with Includemod. Error explanation ->
884
- raise(Error (loc, initial_env, With_mismatch (lid.txt, explanation)))
850
+ let _, s = merge_constraint initial_env loc sg lid (With_type_package cty) in
851
+ s
885
852
886
853
let check_package_with_type_constraints loc env mty constraints =
887
854
let sg = extract_sig env loc mty in
888
855
let sg =
889
856
List. fold_left
890
- (fun sg (lid , cty ) ->
891
- snd (merge_package_constraint env loc sg lid cty))
857
+ (fun sg (lid , cty ) -> merge_package_constraint env loc sg lid cty)
892
858
sg constraints
893
859
in
894
860
let scope = Ctype. create_scope () in
@@ -1602,8 +1568,10 @@ and transl_with ~loc env remove_aliases (rev_tcstrs,sg) constr =
1602
1568
let mty = transl_modtype env smty in
1603
1569
l, With_modtypesubst mty
1604
1570
in
1605
- let (tcstr, sg) = merge_constraint env loc sg lid with_info in
1606
- (tcstr :: rev_tcstrs, sg)
1571
+ let ((path, lid, tcstr), sg) = merge_constraint env loc sg lid with_info in
1572
+ (* Only package with constraints result in None here. *)
1573
+ let tcstr = Option. get tcstr in
1574
+ ((path, lid, tcstr) :: rev_tcstrs, sg)
1607
1575
1608
1576
1609
1577
@@ -3782,8 +3750,8 @@ let report_error ~loc _env = function
3782
3750
(Path. name p) Printtyp. modtype mty
3783
3751
| With_package_manifest (lid , ty ) ->
3784
3752
Location. errorf ~loc
3785
- " @[ In the constrained signature, type %a is defined to be %a.@ \
3786
- Package `with' constraints may only be used on abstract types.@] "
3753
+ " In the constrained signature, type %a is defined to be %a.@ \
3754
+ Package `with' constraints may only be used on abstract types."
3787
3755
longident lid
3788
3756
Printtyp. type_expr ty
3789
3757
| Repeated_name (kind , name ) ->
0 commit comments