Skip to content

Commit f4c96ff

Browse files
authored
flambda-backend: Fix programmatically enabling and disabling the same layouts extension (#1446)
* Add test case for enabling + disabling the same layouts extension * Fix behavior of enabling and disabling the same layouts extension
1 parent cc58003 commit f4c96ff

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

testsuite/tests/language-extensions/language_extensions.ml

+31-16
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,37 @@ let try_disallowing_extensions name =
6060

6161
type goal = Fail | Succeed
6262

63+
let with_goal goal ~name ~what test = match goal with
64+
| Fail -> should_fail name test
65+
| Succeed -> should_succeed name what test
66+
67+
let with_two_layouts goal first second ~enabled =
68+
let verb ending =
69+
"enabl" ^ ending ^ (if enabled then "" else " and disabl" ^ ending)
70+
in
71+
let comparison, plural =
72+
if first = second then "the same", "" else "two different", "s"
73+
in
74+
let description ending =
75+
verb ending ^ " " ^ comparison ^ " layouts extension" ^ plural
76+
in
77+
Language_extension.with_enabled (Layouts first) (fun () ->
78+
with_goal goal
79+
~name:(description "e")
80+
~what:(description "ing")
81+
(fun () ->
82+
Language_extension.with_set (Layouts second) ~enabled (fun () -> ())))
83+
6384
let when_disallowed goal f_str f =
6485
let can_or_can't = match goal with
6586
| Fail -> "can't"
6687
| Succeed -> "can"
6788
in
6889
let f_code = "[" ^ f_str ^ "]" in
69-
let name =
70-
can_or_can't ^ " call " ^ f_code ^ " when extensions are disallowed"
71-
in
72-
let action () = f extension in
73-
match goal with
74-
| Fail -> should_fail name action
75-
| Succeed -> should_succeed name ("redundantly calling " ^ f_code) action
90+
with_goal goal
91+
~name:(can_or_can't ^ " call " ^ f_code ^ " when extensions are disallowed")
92+
~what:("redundantly calling " ^ f_code)
93+
(fun () -> f extension)
7694
;;
7795

7896
let lift_with with_fn extension = with_fn extension Fun.id;;
@@ -147,16 +165,13 @@ Language_extension.with_set extension ~enabled:false (fun () ->
147165
Language_extension.with_set extension ~enabled:true (fun () ->
148166
typecheck_with_extension "disabled locally via [with_set] and also globally");
149167

150-
(* Test that we only allow you to pass one layouts extension flag *)
151-
should_fail "Enable two layouts"
152-
(fun () ->
153-
Language_extension.(enable (Layouts Alpha));
154-
Language_extension.(enable (Layouts Beta)));
168+
(* Test that we only allow you to pass one distinct layouts extension flag*)
169+
170+
with_two_layouts Succeed Alpha Alpha ~enabled:false;
171+
172+
with_two_layouts Fail Alpha Beta ~enabled:false;
155173

156-
should_fail "Enable and disable layouts"
157-
(fun () ->
158-
Language_extension.(enable (Layouts Alpha));
159-
Language_extension.(disable (Layouts Beta)));
174+
with_two_layouts Fail Alpha Beta ~enabled:true;
160175

161176
(* Test disallowing extensions *)
162177

testsuite/tests/language-extensions/reference.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ Successfully typechecked "[x for x = 1 to 10]"
5252
# "comprehensions" extension disabled locally via [with_set] and also globally [comprehensions enabled]:
5353
Successfully typechecked "[x for x = 1 to 10]"
5454

55-
# Enable two layouts [comprehensions enabled]:
56-
Failed as expected: Invalid extensions: Please enable at most one of 'layouts', 'layouts_beta', and 'layouts_alpha'.
55+
# enable and disable the same layouts extension [comprehensions enabled]:
56+
Succeeded at enabling and disabling the same layouts extension
5757

58-
# Enable and disable layouts [comprehensions enabled]:
58+
# enable and disable two different layouts extensions [comprehensions enabled]:
5959
Failed as expected: Cannot disable extension layouts_beta because extension layouts_alpha is enabled. Please enable or disable at most one of the layouts extensions.
6060

61+
# enable two different layouts extensions [comprehensions enabled]:
62+
Failed as expected: Invalid extensions: Please enable at most one of 'layouts', 'layouts_beta', and 'layouts_alpha'.
63+
6164
# can disallow extensions while extensions are enabled [comprehensions disabled]:
6265
Succeeded at disallowing all extensions
6366

utils/language_extension.ml

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
type maturity = Stable | Beta | Alpha
22

3+
let equal_maturity (a : maturity) (b : maturity) = (a = b)
4+
35
type t =
46
| Comprehensions
57
| Local
@@ -125,7 +127,7 @@ let check_conflicts t1 =
125127
'layouts_beta', and 'layouts_alpha'."
126128
in
127129
let c = List.find_map (fun t2 ->
128-
if t1 = t2 then Some Duplicate else
130+
if equal t1 t2 then Some Duplicate else
129131
match t1, t2 with
130132
| Layouts _, Layouts _ -> Some (Incompatible layouts_err)
131133
| _, _ -> None)
@@ -149,12 +151,13 @@ let set extn ~enabled =
149151
extensions :=
150152
List.filter (fun extn' ->
151153
match extn, extn' with
152-
| Layouts _, Layouts _ ->
153-
raise (Arg.Bad(Printf.sprintf
154-
"Cannot disable extension %s because extension %s is enabled. \
155-
Please enable or disable at most one of the layouts extensions."
156-
(to_string extn) (to_string extn')))
157-
| _, _ -> not (equal extn extn'))
154+
| Layouts m1, Layouts m2 when not (equal_maturity m1 m2) ->
155+
raise (Arg.Bad(Printf.sprintf
156+
"Cannot disable extension %s because extension %s is enabled. \
157+
Please enable or disable at most one of the layouts extensions."
158+
(to_string extn) (to_string extn')))
159+
| _, _ ->
160+
not (equal extn extn'))
158161
!extensions
159162

160163
let enable = set ~enabled:true

0 commit comments

Comments
 (0)