@@ -283,35 +283,42 @@ let join_types ~env_at_fork envs_with_levels ~extra_lifted_consts_in_use_envs =
283283 Any such variable will be given type [Unknown] on a branch where it
284284 was not originally present.
285285 Iterating on [level.binding_times] instead of [level.defined_vars] ensures
286- consistency of binding time order in the branches and the result. *)
286+ consistency of binding time order in the branches and the result.
287+ In addition, this also aggregates the code age relations of the branches.
288+ *)
287289 let env_at_fork =
288- List. fold_left (fun env_at_fork (_ , _ , _ , level ) ->
289- Binding_time.Map. fold (fun _ vars env ->
290- Variable.Set. fold (fun var env ->
291- if Typing_env. mem env (Name. var var) then env
292- else
293- let kind = Variable.Map. find var level.defined_vars in
294- Typing_env. add_definition env
295- (Name_in_binding_pos. var
296- (Var_in_binding_pos. create var Name_mode. in_types))
297- kind)
298- vars
299- env)
300- level.binding_times
301- env_at_fork)
290+ List. fold_left (fun env_at_fork (env_at_use , _ , _ , level ) ->
291+ let env_with_variables =
292+ Binding_time.Map. fold (fun _ vars env ->
293+ Variable.Set. fold (fun var env ->
294+ if Typing_env. mem env (Name. var var) then env
295+ else
296+ let kind = Variable.Map. find var level.defined_vars in
297+ Typing_env. add_definition env
298+ (Name_in_binding_pos. var
299+ (Var_in_binding_pos. create var Name_mode. in_types))
300+ kind)
301+ vars
302+ env)
303+ level.binding_times
304+ env_at_fork
305+ in
306+ let code_age_relation =
307+ Code_age_relation. union (Typing_env. code_age_relation env_at_fork)
308+ (Typing_env. code_age_relation env_at_use)
309+ in
310+ Typing_env. with_code_age_relation env_with_variables code_age_relation)
302311 env_at_fork
303312 envs_with_levels
304313 in
305314 (* Now fold over the levels doing the actual join operation on equations. *)
306315 ListLabels. fold_left envs_with_levels
307- ~init: (env_at_fork, Name.Map. empty, true )
308- ~f: (fun (join_env , joined_types , is_first_join ) (env_at_use , _ , _ , t ) ->
309- let join_env =
310- Code_age_relation. union (Typing_env. code_age_relation join_env)
311- (Typing_env. code_age_relation env_at_use)
312- |> Typing_env. with_code_age_relation join_env
316+ ~init: (Name.Map. empty, true )
317+ ~f: (fun (joined_types , is_first_join ) (env_at_use , _ , _ , t ) ->
318+ let left_env =
319+ Typing_env. add_env_extension env_at_fork
320+ (Typing_env_extension. from_map joined_types)
313321 in
314- let next_join_env = ref join_env in
315322 let join_types name joined_ty use_ty =
316323 (* CR mshinwell for vlaviron: Looks like [Typing_env.mem] needs
317324 fixing with respect to names from other units with their
@@ -360,16 +367,11 @@ let join_types ~env_at_fork envs_with_levels ~extra_lifted_consts_in_use_envs =
360367 let expected_kind = Some (Type_grammar. kind use_ty) in
361368 Typing_env. find env_at_fork name expected_kind
362369 in
363- (* Recall: the order of environments matters for [join].
364- Also note that we use [env_at_fork] not [env_at_use] for
365- the right-hand environment. This is done because there may
366- be names in types in [env_at_fork] that are not defined in
367- [env_at_use] -- see the comment in [check_join_inputs]
368- below. *)
370+ (* Recall: the order of environments matters for [join]. *)
369371 let join_env =
370372 Join_env. create env_at_fork
371- ~left_env: join_env
372- ~right_env: env_at_fork
373+ ~left_env
374+ ~right_env: env_at_use
373375 in
374376 Type_grammar. join ~bound_name: name
375377 join_env left_ty use_ty
@@ -384,7 +386,7 @@ let join_types ~env_at_fork envs_with_levels ~extra_lifted_consts_in_use_envs =
384386 let right_ty = Typing_env. find env_at_fork name expected_kind in
385387 let join_env =
386388 Join_env. create env_at_fork
387- ~left_env: join_env
389+ ~left_env
388390 ~right_env: env_at_fork
389391 in
390392 Type_grammar. join ~bound_name: name
@@ -396,7 +398,7 @@ let join_types ~env_at_fork envs_with_levels ~extra_lifted_consts_in_use_envs =
396398 assert (not is_first_join);
397399 let join_env =
398400 Join_env. create env_at_fork
399- ~left_env: join_env
401+ ~left_env
400402 ~right_env: env_at_use
401403 in
402404 Type_grammar. join ~bound_name: name
@@ -405,15 +407,13 @@ let join_types ~env_at_fork envs_with_levels ~extra_lifted_consts_in_use_envs =
405407 in
406408 begin match joined_ty with
407409 | Known joined_ty ->
408- next_join_env :=
409- Typing_env. add_equation ! next_join_env name joined_ty;
410410 Some joined_ty
411411 | Unknown -> None
412412 end
413413 in
414414 let joined_types = Name.Map. merge join_types joined_types t.equations in
415- ! next_join_env, joined_types, false )
416- |> fun (_ , joined_types , _ ) ->
415+ joined_types, false )
416+ |> fun (joined_types , _ ) ->
417417 joined_types
418418
419419let construct_joined_level envs_with_levels ~env_at_fork ~allowed
0 commit comments