Skip to content

Commit 5fd201d

Browse files
lthlschambart
authored andcommitted
Fix potential bug with caused by delaying the addition of aliases
1 parent 7437e0b commit 5fd201d

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ module Cached : sig
4343
-> Type_grammar.t
4444
-> Binding_time.t
4545
-> Name_mode.t
46-
-> new_aliases:Aliases.t
4746
-> t
4847

4948
val replace_variable_binding
5049
: t
5150
-> Variable.t
5251
-> Type_grammar.t
53-
-> new_aliases:Aliases.t
5452
-> t
5553

54+
val with_aliases : t -> aliases:Aliases.t -> t
55+
5656
val add_symbol_projection : t -> Variable.t -> Symbol_projection.t -> t
5757

5858
val find_symbol_projection : t -> Variable.t -> Symbol_projection.t option
@@ -131,27 +131,30 @@ end = struct
131131
(used to be add-or-replace), the [names_to_types] map addition was a
132132
major source of allocation. *)
133133

134-
let add_or_replace_binding t (name : Name.t) ty binding_time name_mode ~new_aliases =
134+
let add_or_replace_binding t (name : Name.t) ty binding_time name_mode =
135135
let names_to_types =
136136
Name.Map.add name (ty, binding_time, name_mode) t.names_to_types
137137
in
138138
{ names_to_types;
139-
aliases = new_aliases;
139+
aliases = t.aliases;
140140
symbol_projections = t.symbol_projections;
141141
}
142142

143-
let replace_variable_binding t var ty ~new_aliases =
143+
let replace_variable_binding t var ty =
144144
let names_to_types =
145145
Name.Map.replace (Name.var var)
146146
(function (_old_ty, binding_time, name_mode) ->
147147
ty, binding_time, name_mode)
148148
t.names_to_types
149149
in
150150
{ names_to_types;
151-
aliases = new_aliases;
151+
aliases = t.aliases;
152152
symbol_projections = t.symbol_projections;
153153
}
154154

155+
let with_aliases t ~aliases =
156+
{ t with aliases; }
157+
155158
let add_symbol_projection t var proj =
156159
let symbol_projections = Variable.Map.add var proj t.symbol_projections in
157160
{ t with symbol_projections; }
@@ -260,6 +263,12 @@ module One_level = struct
260263
let level t = t.level
261264
let just_after_level t = t.just_after_level
262265

266+
let with_aliases t ~aliases =
267+
let just_after_level =
268+
Cached.with_aliases t.just_after_level ~aliases
269+
in
270+
{ t with just_after_level; }
271+
263272
let is_empty t = Typing_env_level.is_empty t.level
264273

265274
(*
@@ -781,6 +790,12 @@ let with_current_level_and_next_binding_time t ~current_level
781790
invariant t;
782791
t
783792

793+
let with_aliases t ~aliases =
794+
let current_level =
795+
One_level.with_aliases t.current_level ~aliases
796+
in
797+
with_current_level t ~current_level
798+
784799
let cached t = One_level.just_after_level t.current_level
785800

786801
let add_variable_definition t var kind name_mode =
@@ -810,7 +825,6 @@ let add_variable_definition t var kind name_mode =
810825
Cached.add_or_replace_binding (cached t)
811826
name (Type_grammar.unknown kind)
812827
t.next_binding_time name_mode
813-
~new_aliases:(aliases t)
814828
in
815829
let current_level =
816830
One_level.create (current_scope t) level ~just_after_level
@@ -911,7 +925,7 @@ let invariant_for_new_equation t aliases name ty =
911925
end
912926
end
913927

914-
let rec add_equation0 t aliases name ty =
928+
let rec add_equation0 t name ty =
915929
if !Clflags.Flambda.Debug.concrete_types_only_on_canonicals then begin
916930
let is_concrete =
917931
match Type_grammar.get_alias_exn ty with
@@ -943,20 +957,18 @@ let rec add_equation0 t aliases name ty =
943957
then
944958
Cached.replace_variable_binding
945959
(One_level.just_after_level t.current_level)
946-
var ty ~new_aliases:aliases
960+
var ty
947961
else
948962
Cached.add_or_replace_binding
949963
(One_level.just_after_level t.current_level)
950964
name ty Binding_time.imported_variables Name_mode.in_types
951-
~new_aliases:aliases
952965
in
953966
just_after_level)
954967
~symbol:(fun _ ->
955968
let just_after_level =
956969
Cached.add_or_replace_binding
957970
(One_level.just_after_level t.current_level)
958971
name ty Binding_time.symbols Name_mode.normal
959-
~new_aliases:aliases
960972
in
961973
just_after_level)
962974
in
@@ -1011,15 +1023,15 @@ and add_equation t name ty =
10111023
end)
10121024
~const:(fun _ -> ())
10131025
end;
1014-
let aliases, simple, t, ty =
1026+
let simple, t, ty =
10151027
let aliases = aliases t in
10161028
match Type_grammar.get_alias_exn ty with
10171029
| exception Not_found ->
10181030
(* Equations giving concrete types may only be added to the canonical
10191031
element as known by the alias tracker (the actual canonical, ignoring
10201032
any name modes). *)
10211033
let canonical = Aliases.get_canonical_ignoring_name_mode aliases name in
1022-
aliases, canonical, t, ty
1034+
canonical, t, ty
10231035
| alias_of ->
10241036
let alias_of = Simple.without_coercion alias_of in
10251037
let alias = Simple.name name in
@@ -1033,10 +1045,11 @@ and add_equation t name ty =
10331045
Aliases.add aliases alias binding_time_and_mode_alias
10341046
alias_of binding_time_and_mode_alias_of
10351047
in
1048+
let t = with_aliases t ~aliases in
10361049
let ty =
10371050
Type_grammar.alias_type_of kind canonical_element
10381051
in
1039-
aliases, alias_of_demoted_element, t, ty
1052+
alias_of, t, ty
10401053
in
10411054
(* Beware: if we're about to add the equation on a name which is different
10421055
from the one that the caller passed in, then we need to make sure that the
@@ -1064,7 +1077,15 @@ and add_equation t name ty =
10641077
in
10651078
Simple.pattern_match simple ~name ~const:(fun _ -> ty, t)
10661079
in
1067-
let [@inline always] name name = add_equation0 t aliases name ty in
1080+
let ty =
1081+
match rec_info with
1082+
| None -> ty
1083+
| Some rec_info ->
1084+
match Type_grammar.apply_rec_info ty rec_info with
1085+
| Bottom -> Type_grammar.bottom (Type_grammar.kind ty)
1086+
| Ok ty -> ty
1087+
in
1088+
let [@inline always] name name = add_equation0 t name ty in
10681089
Simple.pattern_match simple ~name ~const:(fun _ -> t)
10691090

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

0 commit comments

Comments
 (0)