define an empty lambda as an empty function

A lifted function with no body was a declaration of one, and nothing
defined it, so the program did not link.
This commit is contained in:
2026-10-18 22:02:55 +03:00
parent b3d7decc6b
commit 94f41b5fad
5 changed files with 45 additions and 10 deletions

View File

@@ -511,7 +511,16 @@ forms, and what remains."
,(atom-to-fmt-c name) ,(atom-to-fmt-c name)
,(walk-arglist args) ,(walk-arglist args)
. .
,(walk-body maybe-body))))) ,(let ((body (walk-body maybe-body)))
;; one that is empty blocks, and where they were, is nothing:
;; what a lifted empty lambda is given to be a definition
(if (every (lambda (s) (equal? s '(%block-begin)))
(remove line-directive? body))
(list)
body))))))
(define (line-directive? form)
(and (pair? form) (eq? (car form) '%line)))
;;; The type of one parameter ;;; The type of one parameter
(define (arg-type arg) (define (arg-type arg)

View File

@@ -1505,10 +1505,17 @@
(string->symbol (string->symbol
(fmt #f "λ" counter "_" enclosing-fn-name))) (fmt #f "λ" counter "_" enclosing-fn-name)))
;;; A function with no body is a declaration, and nothing would define
;;; one lifted from an empty lambda; an empty block keeps it a definition,
;;; and the writer leaves the block out
(define (defining body)
(if (every comment-form? body) (append body (list '(do))) body))
(define (lift-lambda name form) (define (lift-lambda name form)
(match form (match form
(('lambda arglist ret-type . body) (('lambda arglist ret-type . body)
(process-fn (copy-form-source! form `(fn ,name ,arglist ,ret-type ,@body)) (process-fn (copy-form-source! form `(fn ,name ,arglist ,ret-type
,@(defining body)))
(list))) (list)))
(else (sex-error form "malformed lambda" form)))) (else (sex-error form "malformed lambda" form))))
@@ -1844,11 +1851,13 @@
(list (list
`(fn ,code ((ƛe (* void)) ,@(named-parameters arglist)) ,ret-type `(fn ,code ((ƛe (* void)) ,@(named-parameters arglist)) ,ret-type
,@(if (null? caps) ,@(defining
(list) (append
`((var ƛcaptures (* (struct ,record)) ƛe) (if (null? caps)
,@(append-map (cut capture-local <> form) caps))) (list)
,@body) `((var ƛcaptures (* (struct ,record)) ƛe)
,@(append-map (cut capture-local <> form) caps)))
body)))
`(fn ,construct ,caps (struct ,closure) `(fn ,construct ,caps (struct ,closure)
(var ƛc (struct ,closure)) (var ƛc (struct ,closure))

View File

@@ -784,9 +784,12 @@
(c-type 'void) (c-type 'void)
(c-in-expr (fmt-join/dot c-param (lambda (dot) (dsp "...")) ls ", ")))) (c-in-expr (fmt-join/dot c-param (lambda (dot) (dsp "...")) ls ", "))))
;; MODIFIED FROM UPSTREAM fmt-c: a function of no statements is `{}'
(define (c-fun type name params . body) (define (c-fun type name params . body)
(cat (c-block (c-in-expr (c-prototype type name params)) (cat (if (null? body)
(c-in-stmt (apply c-begin body))) (c-braced-block (c-in-expr (c-prototype type name params)))
(c-block (c-in-expr (c-prototype type name params))
(c-in-stmt (apply c-begin body))))
fl)) fl))
;; MODIFIED FROM UPSTREAM fmt-c: the name and parameters are the ;; MODIFIED FROM UPSTREAM fmt-c: the name and parameters are the

View File

@@ -441,6 +441,13 @@
(test-assert "and its code names it, a definition needing one before C23" (test-assert "and its code names it, a definition needing one before C23"
(emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))") (emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))")
"int ƛu0, int b)")) "int ƛu0, int b)"))
;; a lifted function with no body was a declaration of one, and
;; nothing defined it
(test-assert "an empty lambda is an empty function"
(emits? "(fn f () void (var g (fn () void) (lambda () void)))"
"static void λ0_f (void) {}"))
(test-assert "so is an empty closure's code"
(emits? (in-fn "(var c (closure () void) (closure () void ()))") "(void * ƛe) {}"))
;; the call helper would have to forward the rest, which C cannot ;; the call helper would have to forward the rest, which C cannot
(test-assert "a closure cannot be variadic" (test-assert "a closure cannot be variadic"
(reports? (in-fn "(var c _ (closure ((n int) ...) int () (return n)))") (reports? (in-fn "(var c _ (closure ((n int) ...) int () (return n)))")

View File

@@ -13,7 +13,8 @@
"From a bare fn: 20 42 7" "From a bare fn: 20 42 7"
"Captured array: 6 3, still 6" "Captured array: 6 3, still 6"
"Captured const: 6" "Captured const: 6"
"Unnamed parameter: 2") "Unnamed parameter: 2"
"Empty bodies: called")
(return 0) (return 0)
;;; A closure is a code pointer beside its captures, so what this ;;; A closure is a code pointer beside its captures, so what this
@@ -139,4 +140,10 @@
(var second (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b))) (var second (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))
(printf "Unnamed parameter: %d\n" (second 1 2)) (printf "Unnamed parameter: %d\n" (second 1 2))
(var nothing (closure () void) (closure () void ()))
(var nothing-either (fn () void) (lambda () void))
(nothing)
(nothing-either)
(printf "Empty bodies: called\n")
(return 0)) (return 0))