defmacro parameters named after imported bindings expand to procedures #79

Open
opened 2026-11-08 09:04:37 +01:00 by alex-eg · 0 comments
Owner

Summary

A defmacro parameter whose name is also exported by one of the modules register-macro imports (scheme, (scheme base), srfi-1, sex-macros, or types) resolves to the imported binding instead of the call argument. Common names such as min, max, car, list, or + therefore become procedure objects in the expansion.

Reproduction

repro.sex:

(pub defmacro (clamp val min max)
  `(do (if (< ,val ,min)
           (= ,val ,min))
       (if (> ,val ,max)
           (= ,val ,max))))

(pub fn f ((x float)) void
  (var alpha float 0.5f)
  (clamp alpha alpha 1.0f))

sexc -m repro.sex expands min and max to the CHICKEN procedures rather than to alpha and 1.0f:

(do (if (< alpha #<procedure (scheme#min x1 . xs)>)
        (= alpha #<procedure (scheme#min x1 . xs)>))
    (if (> alpha #<procedure (scheme#max x1 . xs)>)
        (= alpha #<procedure (scheme#max x1 . xs)>)))

sexc -C then fails in the C writer with a misleading bad argument type error while trying to emit the procedure object.

Cause

sex-macros.scm:33 registers each macro as:

`(lambda ,arglist
   (import scheme
           (scheme base)
           ...)
   ,@body)

The (import ...) is inside the lambda, and in CHICKEN an import shadows same-named lambda parameters. The macro's min and max parameters are therefore shadowed before the quasiquote body is evaluated.

Possible fix

Move the (import ...) out of the macro lambda:

`(let ()
   (import scheme
           (scheme base)
           ...)
   (lambda ,arglist ,@body))

check-macro-arity currently reads the argument list from (cadr (get name 'sex-macro)), so that lookup would need adjusting for the new shape.

Environment

sexc main at 7315694 (2026-10-29), CHICKEN 6.0.0, macOS arm64.

## Summary A `defmacro` parameter whose name is also exported by one of the modules `register-macro` imports (`scheme`, `(scheme base)`, `srfi-1`, `sex-macros`, or `types`) resolves to the imported binding instead of the call argument. Common names such as `min`, `max`, `car`, `list`, or `+` therefore become procedure objects in the expansion. ## Reproduction `repro.sex`: ```scheme (pub defmacro (clamp val min max) `(do (if (< ,val ,min) (= ,val ,min)) (if (> ,val ,max) (= ,val ,max)))) (pub fn f ((x float)) void (var alpha float 0.5f) (clamp alpha alpha 1.0f)) ``` `sexc -m repro.sex` expands `min` and `max` to the CHICKEN procedures rather than to `alpha` and `1.0f`: ```scheme (do (if (< alpha #<procedure (scheme#min x1 . xs)>) (= alpha #<procedure (scheme#min x1 . xs)>)) (if (> alpha #<procedure (scheme#max x1 . xs)>) (= alpha #<procedure (scheme#max x1 . xs)>))) ``` `sexc -C` then fails in the C writer with a misleading `bad argument type` error while trying to emit the procedure object. ## Cause `sex-macros.scm:33` registers each macro as: ```scheme `(lambda ,arglist (import scheme (scheme base) ...) ,@body) ``` The `(import ...)` is inside the lambda, and in CHICKEN an import shadows same-named lambda parameters. The macro's `min` and `max` parameters are therefore shadowed before the quasiquote body is evaluated. ## Possible fix Move the `(import ...)` out of the macro lambda: ```scheme `(let () (import scheme (scheme base) ...) (lambda ,arglist ,@body)) ``` `check-macro-arity` currently reads the argument list from `(cadr (get name 'sex-macro))`, so that lookup would need adjusting for the new shape. ## Environment `sexc` main at `7315694` (2026-10-29), CHICKEN 6.0.0, macOS arm64.
alex-eg added the bug label 2026-11-08 09:04:37 +01:00
Sign in to join this conversation.