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.
This commit is contained in:
2026-10-16 13:54:17 +03:00
parent bf9e5c4a53
commit 6661dd82b4
3 changed files with 63 additions and 27 deletions

View File

@@ -938,7 +938,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:
@@ -961,11 +961,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)
@@ -1023,18 +1023,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:
;;;