Skip to content

Commit c0dfa82

Browse files
committed
Fix potential bug with caused by delaying the addition of aliases
1 parent 44fc26d commit c0dfa82

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

middle_end/flambda/types/env/typing_env.rec.ml

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ module Cached : sig
4242
-> Type_grammar.t
4343
-> Binding_time.t
4444
-> Name_mode.t
45-
-> new_aliases:Aliases.t
4645
-> t
4746

4847
val replace_variable_binding
4948
: t
5049
-> Variable.t
5150
-> Type_grammar.t
52-
-> new_aliases:Aliases.t
5351
-> t
5452

53+
val with_aliases : t -> aliases:Aliases.t -> t
54+
5555
val add_symbol_projection : t -> Variable.t -> Symbol_projection.t -> t
5656

5757
val find_symbol_projection : t -> Variable.t -> Symbol_projection.t option
@@ -125,27 +125,30 @@ end = struct
125125
(used to be add-or-replace), the [names_to_types] map addition was a
126126
major source of allocation. *)
127127

128-
let add_or_replace_binding t (name : Name.t) ty binding_time name_mode ~new_aliases =
128+
let add_or_replace_binding t (name : Name.t) ty binding_time name_mode =
129129
let names_to_types =
130130
Name.Map.add name (ty, binding_time, name_mode) t.names_to_types
131131
in
132132
{ names_to_types;
133-
aliases = new_aliases;
133+
aliases = t.aliases;
134134
symbol_projections = t.symbol_projections;
135135
}
136136

137-
let replace_variable_binding t var ty ~new_aliases =
137+
let replace_variable_binding t var ty =
138138
let names_to_types =
139139
Name.Map.replace (Name.var var)
140140
(function (_old_ty, binding_time, name_mode) ->
141141
ty, binding_time, name_mode)
142142
t.names_to_types
143143
in
144144
{ names_to_types;
145-
aliases = new_aliases;
145+
aliases = t.aliases;
146146
symbol_projections = t.symbol_projections;
147147
}
148148

149+
let with_aliases t ~aliases =
150+
{ t with aliases; }
151+
149152
let add_symbol_projection t var proj =
150153
let symbol_projections = Variable.Map.add var proj t.symbol_projections in
151154
{ t with symbol_projections; }
@@ -263,6 +266,12 @@ module One_level = struct
263266
let level t = t.level
264267
let just_after_level t = t.just_after_level
265268

269+
let with_aliases t ~aliases =
270+
let just_after_level =
271+
Cached.with_aliases t.just_after_level ~aliases
272+
in
273+
{ t with just_after_level; }
274+
266275
let is_empty t = Typing_env_level.is_empty t.level
267276

268277
(*
@@ -737,6 +746,12 @@ let with_current_level_and_next_binding_time t ~current_level
737746
invariant t;
738747
t
739748

749+
let with_aliases t ~aliases =
750+
let current_level =
751+
One_level.with_aliases t.current_level ~aliases
752+
in
753+
with_current_level t ~current_level
754+
740755
let cached t = One_level.just_after_level t.current_level
741756

742757
let add_variable_definition t var kind name_mode =
@@ -766,7 +781,6 @@ let add_variable_definition t var kind name_mode =
766781
Cached.add_or_replace_binding (cached t)
767782
name (Type_grammar.unknown kind)
768783
t.next_binding_time name_mode
769-
~new_aliases:(aliases t)
770784
in
771785
let current_level =
772786
One_level.create (current_scope t) level ~just_after_level
@@ -869,7 +883,7 @@ let invariant_for_new_equation t name ty =
869883
end
870884
end
871885

872-
let rec add_equation0 t aliases name ty =
886+
let rec add_equation0 t name ty =
873887
if !Clflags.Flambda.Debug.concrete_types_only_on_canonicals then begin
874888
let is_concrete =
875889
match Type_grammar.get_alias_exn ty with
@@ -905,20 +919,18 @@ let rec add_equation0 t aliases name ty =
905919
then
906920
Cached.replace_variable_binding
907921
(One_level.just_after_level t.current_level)
908-
var ty ~new_aliases:aliases
922+
var ty
909923
else
910924
Cached.add_or_replace_binding
911925
(One_level.just_after_level t.current_level)
912926
name ty Binding_time.imported_variables Name_mode.in_types
913-
~new_aliases:aliases
914927
in
915928
just_after_level, t.closure_env)
916929
~symbol:(fun _ ->
917930
let just_after_level =
918931
Cached.add_or_replace_binding
919932
(One_level.just_after_level t.current_level)
920933
name ty Binding_time.symbols Name_mode.normal
921-
~new_aliases:aliases
922934
in
923935
let closure_env =
924936
match t.closure_env with
@@ -987,15 +999,15 @@ and add_equation t name ty =
987999
end)
9881000
~const:(fun _ -> ())
9891001
end;
990-
let aliases, simple, rec_info, t, ty =
1002+
let simple, rec_info, t, ty =
9911003
let aliases = aliases t in
9921004
match Type_grammar.get_alias_exn ty with
9931005
| exception Not_found ->
9941006
(* Equations giving concrete types may only be added to the canonical
9951007
element as known by the alias tracker (the actual canonical, ignoring
9961008
any name modes). *)
9971009
let canonical = Aliases.get_canonical_ignoring_name_mode aliases name in
998-
aliases, canonical, None, t, ty
1010+
canonical, None, t, ty
9991011
| alias_of ->
10001012
let alias = Simple.name name in
10011013
let kind = Type_grammar.kind ty in
@@ -1008,10 +1020,11 @@ and add_equation t name ty =
10081020
Aliases.add aliases alias binding_time_and_mode_alias
10091021
alias_of binding_time_and_mode_alias_of
10101022
in
1023+
let t = with_aliases t ~aliases in
10111024
let ty =
10121025
Type_grammar.alias_type_of kind canonical_element
10131026
in
1014-
aliases, alias_of, rec_info, t, ty
1027+
alias_of, rec_info, t, ty
10151028
in
10161029
(* Beware: if we're about to add the equation on a name which is different
10171030
from the one that the caller passed in, then we need to make sure that the
@@ -1047,7 +1060,7 @@ and add_equation t name ty =
10471060
| Bottom -> Type_grammar.bottom (Type_grammar.kind ty)
10481061
| Ok ty -> ty
10491062
in
1050-
let [@inline always] name name = add_equation0 t aliases name ty in
1063+
let [@inline always] name name = add_equation0 t name ty in
10511064
Simple.pattern_match simple ~name ~const:(fun _ -> t)
10521065

10531066
and add_env_extension t (env_extension : Typing_env_extension.t) =

0 commit comments

Comments
 (0)