From e243abea644016102395304703af84451742eae2 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 13:54:17 +0300 Subject: [PATCH] lower match to an if chain, so break in an arm leaves the loop The switch it lowered to took a break in an arm for its own, and the loop around the match went on. With no else the last arm is the chain's else, which keeps C from warning that a function may return nothing. --- semen.scm | 48 +++++++++++++++++++++++++------------ tests/encapsulation.scm | 30 +++++++++++++---------- tests/sex-programs/sums.sex | 12 ++++++++++ 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/semen.scm b/semen.scm index d53cf6c..d137d60 100644 --- a/semen.scm +++ b/semen.scm @@ -942,7 +942,7 @@ ((for) (< i 3)) (else (= i 0)))) -;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the +;;; (match s ((circle (r)) ...) ((empty ()) ...)) is an if over the ;;; tag, each arm a block binding that variant's fields as locals. `_' ;;; drops one. An arm the sum does not have, a wrong number of ;;; binders, or a variant left out with no `else' are all errors here: @@ -965,11 +965,11 @@ ;; in scope from its own initializer on (if (and (simple-path? scrutinee) (not (any (cut mentions? scrutinee <>) (match-binders arms)))) - (walk-statement (match-switch name scrutinee variants arms) env) + (walk-statement (match-branches name scrutinee variants arms) env) (let ((held (aux-name! env make-match-name))) (walk-statement `(do (var ,held ,type ,scrutinee) - ,(match-switch name held variants arms)) + ,(match-branches name held variants arms)) env))))) (define (match-binders arms) @@ -1027,18 +1027,36 @@ (when (and (pair? missing) (not (any else? arms))) (apply sex-error form "match leaves a variant out:" missing))))) -(define (match-switch name scrutinee variants arms) - `(switch (dot-access ,scrutinee tag) - ,@(map (lambda (arm) - (if (eq? (car arm) 'else) - `(default (do ,@(cdr arm))) - (let* ((variant (match-arm-variant arm)) - (binders (second (car arm))) - (fields (variant-fields variants variant))) - `(case ,(sum-tag-constant name variant) - (do ,@(match-bindings scrutinee variant binders fields) - ,@(cdr arm)))))) - arms))) +;;; Not a switch, which would take a `break' in an arm for its own: +;;; there is none in the source, so the break is the loop's. With no +;;; else the arms cover every variant, so the last needs no test -- and +;;; C, seeing every path end, does not warn of one returning nothing +;;; +;;; (if (== s.tag shapeǂcircle) (do (var r float s.u.circle.r) ...) +;;; (== s.tag shapeǂrect) (do ...) +;;; (do ...)) ; the else, or else empty's arm +(define (match-branches name scrutinee variants arms) + (let-values (((otherwise named) (partition (lambda (arm) (eq? (car arm) 'else)) + arms))) + (let* ((body (lambda (arm) + (if (eq? (car arm) 'else) + `(do ,@(cdr arm)) + (let ((variant (match-arm-variant arm))) + `(do ,@(match-bindings scrutinee variant (second (car arm)) + (variant-fields variants variant)) + ,@(cdr arm)))))) + (ordered (append named otherwise)) + (tested (drop-right ordered 1)) + (final (body (last ordered)))) + (if (null? tested) + final + `(if ,@(append-map + (lambda (arm) + (list `(== (dot-access ,scrutinee tag) + ,(sum-tag-constant name (match-arm-variant arm))) + (body arm))) + tested) + ,final))))) ;;; A binder is a copy of its field, an array one included: ;;; diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 8d8ea58..018c5c5 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -396,13 +396,17 @@ ahead of its pub forms." "(realize (p v) (fn s ((a (* const v))) void (return)))") "method is already defined on"))) - ;; match is a switch over the tag whose arms bind the payload. What + ;; match is an if over the tag whose arms bind the payload. What ;; it refuses is why it exists. (test-group "match" - (test-assert "it switches on the tag" + (test-assert "it tests the tag" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") - "switch (s.tag)")) + "if (s.tag == shapeǂcircle)")) + (test-assert "but not for the last variant, which is all that is left" + (not (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" + " ((empty ()) (= out 0.0)))") + "s.tag == shapeǂempty"))) (test-assert "an arm binds its payload" (emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))" " ((empty ()) (= out 0.0)))") @@ -432,13 +436,15 @@ ahead of its pub forms." "(typedef alias shape)" "(fn f () int (var s alias (shape/circle 1))" " (match s ((circle (r)) (return r)) ((empty ()) (return 0))))") - "case shapeǂcircle")) - (test-assert "else becomes default" - (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") - "default:")) + "s.tag == shapeǂcircle")) + (test-assert "else is the last branch, wherever it is written" + (let ((c (silently + (lambda () + (sex->c (sum-src "(match s (else (= out 2.0)) ((circle (r)) (= out r)))")))))) + (< (string-contains c "out = r") (string-contains c "out = 2.0")))) (test-assert "a comment between arms is dropped" - (emits? (sum-src "(match s ((circle (r)) (= out r)) ;; gone\n (else (= out 0.0)))") - "default:")) + (emits? (sum-src "(match s ((circle (r)) (= out r)) ;; gone\n (else (= out 2.0)))") + "out = 2.0")) (test-assert "a variant left out is an error" (reports? (sum-src "(match s ((circle (r)) (= out r)))") @@ -447,7 +453,7 @@ ahead of its pub forms." (reports? (sum-src "(match s ((circle (r)) (= out r)))") "rect")) (test-assert "else excuses the rest" (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))") - "switch (s.tag)")) + "s.tag == shapeǂcircle")) (test-assert "a variant the sum does not have is an error" (reports? (sum-src "(match s ((circle (r)) (= out r)) ((square (w)) (= out w))" " ((rect (w h)) (= out w)) ((empty ()) (= out 0.0)))") @@ -495,11 +501,11 @@ ahead of its pub forms." (test-assert "but a case body is a statement" (emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))" " (else (= out 0.0)))))") - "switch (s.tag)")) + "s.tag == shapeǂcircle")) (test-assert "and so is a closure's, wherever the closure stands" (emits? (sum-src "(var c (closure () int) (closure () int ((t s))" " (match t ((circle (r)) (return 1)) (else (return 0)))))") - "switch (t.tag)")) + "t.tag == shapeǂcircle")) (test-assert "and a scrutinee that is not a sum" (reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))" "match wants a sum"))) diff --git a/tests/sex-programs/sums.sex b/tests/sex-programs/sums.sex index 3ad2b1c..ca1dbc7 100644 --- a/tests/sex-programs/sums.sex +++ b/tests/sex-programs/sums.sex @@ -4,6 +4,7 @@ "nested: -(2 + 3) = -5" "size: 4" "tagged only: 1" + "broke out after 2" "rows: 18 5") (return 0) @@ -93,6 +94,17 @@ ((red ()) (printf "tagged only: 0\n")) ((green ()) (printf "tagged only: 1\n"))) + ;; there is no switch in the source, so `break' in an arm leaves the + ;; loop around the match; one in a loop of the arm's own leaves that + (var lights [light 3] #((light/green) (light/red) (light/green))) + (var seen int 0) + (for (var i int 0) (< i 3) (++ i) + (++ seen) + (match [lights i] + ((red ()) (while true (break)) (break)) + ((green ()) (continue)))) + (printf "broke out after %d\n" seen) + (var a [int N] #(5 6 7 8)) (var rows [row 2] #((row/cells a) (row/cells #(1 2 3 4)))) (= [a 0] 50) ; the row holds a copy