Macros don't expand in record or struct field types #77

Open
opened 2026-11-04 14:20:22 +01:00 by alex-eg · 0 comments
Owner

Macros are not expanded when they appear in a record's field type. The call is carried through as a type, and sexc emits C that cannot compile.

(pub sum page
  ((main ())
   (settings ())))

(pub defmacro (sum-type sum type)
  (cat sum (cat 'ǂ type)))

(pub record app-state
  ((page page)
   (target-page (sum-type page main))))

sexc -m leaves the call in the type position:

(pub struct app-state
  ((page page)
   (target-page (sum-type page main))))

sexc -C emits:

struct app_state {
    page page;
    sum_type page main target_page;
};

The simplest case shows it is not about the macro's result being a complex type:

(defmacro (ty) 'int)
(record r ((x (ty))))

sexc -C emits ty x; and exits 0.

struct fields behave the same:

(defmacro (ty) 'int)
(struct r ((x (ty))))
;; -> ty x;

var and typedef type positions too:

(var x (ty) 0)      ; -> static ty x = 0;
(typedef foo (ty))  ; -> typedef ty foo;

Function headers do expand:

(defmacro (ty) 'int)
(fn f ((p (ty))) (ty) (return 0))
;; sexc -m: (fn f ((p int)) int (return 0))

so the behaviour is inconsistent: function signature types are expanded, but record/struct fields, var types and typedef targets are not.

Expected:

  • expand macros in type position before parsing/registering the type, so the type database sees the expanded type; or
  • reject a macro call where a type is expected with a clear sex-error, rather than emitting invalid C and exiting 0.

For record/struct fields the expansion must happen before register-aggregate!, so field lookup and later type checking see the expanded type.

Macros are not expanded when they appear in a record's field type. The call is carried through as a type, and `sexc` emits C that cannot compile. ```scheme (pub sum page ((main ()) (settings ()))) (pub defmacro (sum-type sum type) (cat sum (cat 'ǂ type))) (pub record app-state ((page page) (target-page (sum-type page main)))) ``` `sexc -m` leaves the call in the type position: ```scheme (pub struct app-state ((page page) (target-page (sum-type page main)))) ``` `sexc -C` emits: ```c struct app_state { page page; sum_type page main target_page; }; ``` The simplest case shows it is not about the macro's result being a complex type: ```scheme (defmacro (ty) 'int) (record r ((x (ty)))) ``` `sexc -C` emits `ty x;` and exits 0. `struct` fields behave the same: ```scheme (defmacro (ty) 'int) (struct r ((x (ty)))) ;; -> ty x; ``` `var` and `typedef` type positions too: ```scheme (var x (ty) 0) ; -> static ty x = 0; (typedef foo (ty)) ; -> typedef ty foo; ``` Function headers *do* expand: ```scheme (defmacro (ty) 'int) (fn f ((p (ty))) (ty) (return 0)) ;; sexc -m: (fn f ((p int)) int (return 0)) ``` so the behaviour is inconsistent: function signature types are expanded, but record/struct fields, `var` types and `typedef` targets are not. Expected: - expand macros in type position before parsing/registering the type, so the type database sees the expanded type; or - reject a macro call where a type is expected with a clear `sex-error`, rather than emitting invalid C and exiting 0. For record/struct fields the expansion must happen before `register-aggregate!`, so field lookup and later type checking see the expanded type.
alex-eg added the bug label 2026-11-04 14:20:22 +01:00
Sign in to join this conversation.