read an array's length as types does

Inference took only an integer for a length, and read a name there as
part of the element type: [_ N] did not solve, and an array of [int N]
decayed to int N *.
This commit is contained in:
2026-10-18 22:02:55 +03:00
parent 230f65e588
commit 60680bbb0a
2 changed files with 21 additions and 11 deletions

View File

@@ -79,8 +79,9 @@
(target ptr-target)
(quals ptr-quals))
;;; `size' is an integer, or #f for `(¤ int)' -- an array of unwritten
;;; length.
;;; `size' is the length as written -- 4, N, (* 2 N) -- or #f for
;;; `(¤ int)', an array of unwritten length. Two are one length when
;;; they are written alike.
(define-record-type <array>
(make-array-type elt size)
array-type?
@@ -223,15 +224,15 @@
(name (and (pair? (cdr words)) (symbol? (cadr words)) (cadr words))))
(make-agg kind name words quals)))
;;; (¤ elt ... size) -- the size is the last element when it is an
;;; integer, and absent otherwise. The element words are unwrapped the
;;; way the C writer unwraps them, so `[int 16]' and `[(int) 16]' are
;;; one type.
;;; (¤ elt ... size) -- the size is the bound `types' finds there: an
;;; integer, a name, an expression, or absent. The element words are
;;; unwrapped the way the C writer unwraps them, so `[int 16]' and
;;; `[(int) 16]' are one type.
(define (parse-array surface)
(let* ((rest (cdr (nest-array-type surface)))
(sized? (and (pair? rest) (integer? (last rest))))
(size (and sized? (last rest)))
(words (if sized? (drop-right rest 1) rest)))
(let* ((nested (nest-array-type surface))
(sized? (array-bound? nested))
(size (and sized? (array-bound nested)))
(words (if sized? (drop-right (cdr nested) 1) (cdr nested))))
(when (null? words)
(sex-error surface "array type without an element type" surface))
(make-array-type (parse-type (maybe-unwrap words)) size)))
@@ -384,7 +385,7 @@
;; One of them may be `(¤ int)': an unwritten length constrains
;; nothing, the way it does not in C either.
(when (and (array-size a) (array-size b)
(not (= (array-size a) (array-size b))))
(not (equal? (array-size a) (array-size b))))
(type-mismatch a b form))
(unify (array-elt a) (array-elt b) form))
((and (fn-type? a) (fn-type? b))

View File

@@ -678,6 +678,15 @@
(emits? "(typedef count int) (fn g () count (return 0))
(fn f () void (var x _ (g)))"
"count x = g()"))
;; only an integer was taken for a length; a name was read as part
;; of the element's
(test-assert "of a named length"
(emits? "(define N 3) (fn f () void (var t [_ N] #(1 2 3)))" "int t[N]"))
(test-assert "which an array of decays from as any other"
(emits? "(define N 3) (fn f () void (var a [int N]) (var p _ (+ a 1)))"
"int * p = a + 1"))
(test-assert "while a length left to work out is not one"
(reports? (in-fn "(var t [_ _] #(1 2))") "cannot infer"))
;; a variable had nowhere to keep a qualifier written on it, and
;; what it was solved to came out without
(test-assert "a qualified wildcard keeps its qualifier"