flan/test/programs/pkg-return.flan
Joseph Ferano 90d3d6694e A package struct can be a return type
The parser decides "return type or first body form?" from the set of type
names the file declares, and an import is resolved after parsing - so a
package's structs cannot be in that set by construction. (defn mk [] rl/Vector2
...) therefore read the return type as the body and failed with "unknown name
rl/Vector2", which names the symptom and not the cause.

The signal is the alias plus the capital, and both halves are needed. An alias
is syntactically obvious and the same pre-pass collects it. A bare capitalised
symbol is never a value in this language - a struct or union constructor is
(Name {...}), a List, and an enum member is a keyword - so the hazard the
surrounding comment warns about, a body form eaten as a return type, has no
form of this shape to eat. A lowercase qualified name stays an expression,
which is what rl/get-color has to be.

Found by the raylib lane, which hit it on rl/Vector2 and reported it rather
than reaching into a file it did not own.
2026-09-12 03:59:57 +07:00

28 lines
1.1 KiB
Plaintext

;;;; A package-qualified struct in return position.
;;;;
;;;; The parser decides "is this form a return type or the first body form?"
;;;; from the set of type names the file declares — and an import is resolved
;;;; after parsing, so a package's structs cannot be in that set. The result
;;;; was that (defn mk [] rl/Vector2 ...) read the return type as the body and
;;;; failed with "unknown name rl/Vector2", which named the symptom and not the
;;;; cause. The alias plus the capital is the signal: a bare capitalised symbol
;;;; is never a value here, since a constructor is (Name {...}) and an enum
;;;; member is a keyword.
(import edn "vendor:edn")
(defstruct Local [n i32])
;;; The case that used to fail: a package struct as the declared return type.
(defn fresh [src [u8]] edn/Cursor (edn/cursor src))
;;; And the one that must keep working: a lowercase qualified name in the same
;;; position is an expression, not a type.
(defn local [] Local (Local {:n 5}))
(defn main [] i32
(let [c (fresh (bytes "[1 2]"))
t (edn/next (addr c))]
(print-i64 (i64 (.kind t))) (newline))
(print-i64 (i64 (.n (local)))) (newline)
0)