@@ -459,8 +459,7 @@ let merge_constraint initial_env remove_aliases loc sg constr =
459459 | Pwith_typesubst _ | Pwith_modsubst _ -> true
460460 in
461461 let real_ids = ref [] in
462- let sig_env = Env. add_signature sg initial_env in
463- let rec merge sg namelist row_id =
462+ let rec merge sig_env sg namelist row_id =
464463 match (sg, namelist, constr) with
465464 ([] , _ , _ ) ->
466465 raise(Error (loc, sig_env, With_no_component lid.txt))
@@ -513,30 +512,26 @@ let merge_constraint initial_env remove_aliases loc sg constr =
513512 Sig_type (id_row, decl_row, rs', priv)
514513 :: Sig_type (id, newdecl, rs, priv)
515514 :: rem
516- | (Sig_type (id, sig_decl, rs, priv) :: rem , [s], Pwith_type (_, sdecl))
515+ | (Sig_type (id, sig_decl, rs, priv) :: rem , [s],
516+ (Pwith_type (_, sdecl) | Pwith_typesubst (_, sdecl) as constr))
517517 when Ident. name id = s ->
518518 let tdecl =
519519 Typedecl. transl_with_constraint id None
520520 ~sig_env ~sig_decl ~outer_env: initial_env sdecl in
521- let newdecl = tdecl.typ_type in
522- let loc = sdecl.ptype_loc in
521+ let newdecl = tdecl.typ_type and loc = sdecl.ptype_loc in
523522 check_type_decl sig_env loc id row_id newdecl sig_decl rs rem;
524- (Pident id, lid, Twith_type tdecl),
525- Sig_type (id, newdecl, rs, priv) :: rem
523+ begin match constr with
524+ Pwith_type _ ->
525+ (Pident id, lid, Twith_type tdecl),
526+ Sig_type (id, newdecl, rs, priv) :: rem
527+ | (* Pwith_typesubst *) _ ->
528+ real_ids := [Pident id];
529+ (Pident id, lid, Twith_typesubst tdecl),
530+ update_rec_next rs rem
531+ end
526532 | (Sig_type (id, _, _, _) :: rem, [s], (Pwith_type _ | Pwith_typesubst _))
527533 when Ident. name id = s ^ " #row" ->
528- merge rem namelist (Some id)
529- | (Sig_type (id, decl, rs, _priv) :: rem, [s], Pwith_typesubst (_, sdecl))
530- when Ident. name id = s ->
531- (* Check as for a normal with constraint, but discard definition *)
532- let tdecl =
533- Typedecl. transl_with_constraint id None
534- ~sig_env ~sig_decl: decl ~outer_env: initial_env sdecl in
535- let newdecl = tdecl.typ_type in
536- check_type_decl sig_env sdecl.ptype_loc id row_id newdecl decl rs rem;
537- real_ids := [Pident id];
538- (Pident id, lid, Twith_typesubst tdecl),
539- update_rec_next rs rem
534+ merge sig_env rem namelist (Some id)
540535 | (Sig_module (id, pres, md, rs, priv) :: rem, [s], Pwith_module (_, lid'))
541536 when Ident. name id = s ->
542537 let path, md' = Env. lookup_module ~loc lid'.txt initial_env in
@@ -557,34 +552,35 @@ let merge_constraint initial_env remove_aliases loc sg constr =
557552 real_ids := [Pident id];
558553 (Pident id, lid, Twith_modsubst (path, lid')),
559554 update_rec_next rs rem
560- | (Sig_module (id, _, ({md_type = Mty_alias _} as md), _, _) as item :: rem,
561- s :: namelist, (Pwith_module _ | Pwith_type _))
555+ | (Sig_module (id, _, md, rs, priv) as item :: rem, s :: namelist, constr)
562556 when Ident. name id = s ->
563- let ((path, _, tcstr), _) =
564- merge (extract_sig sig_env loc md.md_type) namelist None
565- in
557+ let sg = extract_sig sig_env loc md.md_type in
558+ let ((path, _, tcstr), newsg) = merge_signature sig_env sg namelist in
566559 let path = path_concat id path in
567560 real_ids := path :: ! real_ids;
568- (path, lid, tcstr), item :: rem
569- | (Sig_module (id, _, md, rs, priv) :: rem, s :: namelist, _)
570- when Ident. name id = s ->
571- let ((path, _path_loc, tcstr), newsg) =
572- merge (extract_sig sig_env loc md.md_type) namelist None
561+ let item =
562+ match md.md_type, constr with
563+ Mty_alias _ , (Pwith_module _ | Pwith_type _ ) ->
564+ (* A module alias cannot be refined, so keep it
565+ and just check that the constraint is correct *)
566+ item
567+ | _ ->
568+ let newmd = {md with md_type = Mty_signature newsg} in
569+ Sig_module (id, Mp_present , newmd, rs, priv)
573570 in
574- let path = path_concat id path in
575- real_ids := path :: ! real_ids;
576- let newmd = {md with md_type= Mty_signature newsg} in
577- let item = Sig_module (id, Mp_present , newmd, rs, priv) in
578571 (path, lid, tcstr),
579572 item :: rem
580573 | (item :: rem , _ , _ ) ->
581- let (cstr, items) = merge rem namelist row_id
574+ let (cstr, items) = merge sig_env rem namelist row_id
582575 in
583576 cstr, item :: items
577+ and merge_signature env sg namelist =
578+ let sig_env = Env. add_signature sg env in
579+ merge sig_env sg namelist None
584580 in
585581 try
586582 let names = Longident. flatten lid.txt in
587- let (tcstr, sg) = merge sg names None in
583+ let (tcstr, sg) = merge_signature initial_env sg names in
588584 if destructive_substitution then (
589585 match List. rev ! real_ids with
590586 | [] -> assert false
0 commit comments