read a match scrutinee before a binder can shadow it

C has a declaration in scope from its own initializer, so in
(match (* e) ((neg (e)) ...)) the binding read the new, uninitialized e.
A binder named like a part of the scrutinee now has it held in a local.
This commit is contained in:
2026-10-16 13:53:49 +03:00
parent 1c6d0aae62
commit bf9e5c4a53
3 changed files with 24 additions and 3 deletions

View File

@@ -955,16 +955,24 @@
(sex-error form "match wants a sum, and this is not one"
(or type 'unknown)))
(check-match form name variants arms)
(if (simple-path? scrutinee)
;; the scrutinee is read once per binding, so anything that could
;; read differently the second time is read into a local first --
;; a binder named like a part of it included, since C has a binder
;; 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)
;; the scrutinee is read once per binding, so anything that
;; could be read twice is read into a local first
(let ((held (aux-name! env make-match-name)))
(walk-statement
`(do (var ,held ,type ,scrutinee)
,(match-switch name held variants arms))
env)))))
(define (match-binders arms)
(append-map (lambda (arm)
(if (eq? (car arm) 'else) '() (delete '_ (second (car arm)))))
arms))
(define (make-match-name fn-name counter)
(string->symbol (string-append "ƛm" (number->string counter))))