Skip to content

Commit d5aeefb

Browse files
committed
feat(pkg): simplify and/or_absorb_undefined_var
- Fixes #13669 Signed-off-by: Ali Caglayan <alizter@gmail.com>
1 parent 0063581 commit d5aeefb

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

src/dune_lang/slang.ml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,21 +334,49 @@ let rec simplify = function
334334
| Form (_, Blang (Const _)) as value -> value
335335
| value -> Form (loc, Catch_undefined_var { value; fallback = simplify fallback }))
336336
| And_absorb_undefined_var blangs ->
337-
let blangs : blang list =
337+
let blangs =
338338
List.concat_map blangs ~f:(fun blang ->
339339
match simplify_blang blang with
340340
| Expr (Form (_, And_absorb_undefined_var blangs)) -> blangs
341341
| blang -> [ blang ])
342342
in
343-
Form (loc, And_absorb_undefined_var blangs)
343+
if
344+
List.exists blangs ~f:(function
345+
| Blang.Const false -> true
346+
| _ -> false)
347+
then Form (loc, Blang (Const false))
348+
else (
349+
let blangs =
350+
List.filter blangs ~f:(function
351+
| Blang.Const true -> false
352+
| _ -> true)
353+
in
354+
match blangs with
355+
| [] -> Form (loc, Blang (Const true))
356+
| [ b ] -> Form (loc, Blang b)
357+
| _ -> Form (loc, And_absorb_undefined_var blangs))
344358
| Or_absorb_undefined_var blangs ->
345-
let blangs : blang list =
346-
List.concat_map blangs ~f:(fun (blang : blang) ->
359+
let blangs =
360+
List.concat_map blangs ~f:(fun blang ->
347361
match simplify_blang blang with
348362
| Expr (Form (_, Or_absorb_undefined_var blangs)) -> blangs
349363
| blang -> [ blang ])
350364
in
351-
Form (loc, Or_absorb_undefined_var (List.map blangs ~f:simplify_blang))
365+
if
366+
List.exists blangs ~f:(function
367+
| Blang.Const true -> true
368+
| _ -> false)
369+
then Form (loc, Blang (Const true))
370+
else (
371+
let blangs =
372+
List.filter blangs ~f:(function
373+
| Blang.Const false -> false
374+
| _ -> true)
375+
in
376+
match blangs with
377+
| [] -> Form (loc, Blang (Const false))
378+
| [ b ] -> Form (loc, Blang b)
379+
| _ -> Form (loc, Or_absorb_undefined_var blangs))
352380
| Blang b -> Form (loc, Blang (simplify_blang b)))
353381

354382
and simplify_blang = function

test/expect-tests/dune_lang/slang_tests.ml

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -360,45 +360,30 @@ let%expect_test "has_undefined_var const" =
360360

361361
(* Slang: And_absorb_undefined_var *)
362362

363-
(* CR-soon Alizter: and_absorb_undefined_var should simplify to [Blang (Const true)] *)
364363
let%expect_test "and_absorb empty" =
365364
print_slang (Slang.and_absorb_undefined_var []);
366-
[%expect {| And_absorb_undefined_var |}]
365+
[%expect {| Blang (Const true) |}]
367366
;;
368367

369-
(* CR-soon Alizter: and_absorb_undefined_var should unwrap singleton *)
370368
let%expect_test "and_absorb singleton" =
371369
print_slang (Slang.and_absorb_undefined_var [ expr (pform "x") ]);
372-
[%expect {| And_absorb_undefined_var (Expr (Literal (template "%{pkg-self:x}"))) |}]
370+
[%expect {| Blang (Expr (Literal (template "%{pkg-self:x}"))) |}]
373371
;;
374372

375-
(* CR-soon Alizter: and_absorb_undefined_var should filter true *)
376373
let%expect_test "and_absorb filters true" =
377374
print_slang (Slang.and_absorb_undefined_var [ const true; expr (pform "x") ]);
378-
[%expect
379-
{|
380-
And_absorb_undefined_var
381-
(Const true, Expr (Literal (template "%{pkg-self:x}")))
382-
|}]
375+
[%expect {| Blang (Expr (Literal (template "%{pkg-self:x}"))) |}]
383376
;;
384377

385-
(* CR-soon Alizter: and_absorb_undefined_var should short-circuit on false *)
386378
let%expect_test "and_absorb false short-circuits" =
387379
print_slang
388380
(Slang.and_absorb_undefined_var [ expr (pform "x"); const false; expr (pform "y") ]);
389-
[%expect
390-
{|
391-
And_absorb_undefined_var
392-
(Expr (Literal (template "%{pkg-self:x}")),
393-
Const false,
394-
Expr (Literal (template "%{pkg-self:y}")))
395-
|}]
381+
[%expect {| Blang (Const false) |}]
396382
;;
397383

398-
(* CR-soon Alizter: and_absorb_undefined_var should simplify to [Blang (Const true)] *)
399384
let%expect_test "and_absorb all true" =
400385
print_slang (Slang.and_absorb_undefined_var [ const true; const true ]);
401-
[%expect {| And_absorb_undefined_var (Const true, Const true) |}]
386+
[%expect {| Blang (Const true) |}]
402387
;;
403388

404389
let%expect_test "and_absorb flattens" =
@@ -418,45 +403,30 @@ let%expect_test "and_absorb flattens" =
418403

419404
(* Slang: Or_absorb_undefined_var *)
420405

421-
(* CR-soon Alizter: or_absorb_undefined_var should simplify to [Blang (Const false)] *)
422406
let%expect_test "or_absorb empty" =
423407
print_slang (Slang.or_absorb_undefined_var []);
424-
[%expect {| Or_absorb_undefined_var |}]
408+
[%expect {| Blang (Const false) |}]
425409
;;
426410

427-
(* CR-soon Alizter: or_absorb_undefined_var should unwrap singleton *)
428411
let%expect_test "or_absorb singleton" =
429412
print_slang (Slang.or_absorb_undefined_var [ expr (pform "x") ]);
430-
[%expect {| Or_absorb_undefined_var (Expr (Literal (template "%{pkg-self:x}"))) |}]
413+
[%expect {| Blang (Expr (Literal (template "%{pkg-self:x}"))) |}]
431414
;;
432415

433-
(* CR-soon Alizter: or_absorb_undefined_var should filter false *)
434416
let%expect_test "or_absorb filters false" =
435417
print_slang (Slang.or_absorb_undefined_var [ const false; expr (pform "x") ]);
436-
[%expect
437-
{|
438-
Or_absorb_undefined_var
439-
(Const false, Expr (Literal (template "%{pkg-self:x}")))
440-
|}]
418+
[%expect {| Blang (Expr (Literal (template "%{pkg-self:x}"))) |}]
441419
;;
442420

443-
(* CR-soon Alizter: or_absorb_undefined_var should short-circuit on true *)
444421
let%expect_test "or_absorb true short-circuits" =
445422
print_slang
446423
(Slang.or_absorb_undefined_var [ expr (pform "x"); const true; expr (pform "y") ]);
447-
[%expect
448-
{|
449-
Or_absorb_undefined_var
450-
(Expr (Literal (template "%{pkg-self:x}")),
451-
Const true,
452-
Expr (Literal (template "%{pkg-self:y}")))
453-
|}]
424+
[%expect {| Blang (Const true) |}]
454425
;;
455426

456-
(* CR-soon Alizter: or_absorb_undefined_var should simplify to [Blang (Const false)] *)
457427
let%expect_test "or_absorb all false" =
458428
print_slang (Slang.or_absorb_undefined_var [ const false; const false ]);
459-
[%expect {| Or_absorb_undefined_var (Const false, Const false) |}]
429+
[%expect {| Blang (Const false) |}]
460430
;;
461431

462432
let%expect_test "or_absorb flattens" =

0 commit comments

Comments
 (0)