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 cb37821e3e
commit 3436c4ff3e
2 changed files with 21 additions and 11 deletions

View File

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

View File

@@ -678,6 +678,15 @@
(emits? "(typedef count int) (fn g () count (return 0)) (emits? "(typedef count int) (fn g () count (return 0))
(fn f () void (var x _ (g)))" (fn f () void (var x _ (g)))"
"count 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 ;; a variable had nowhere to keep a qualifier written on it, and
;; what it was solved to came out without ;; what it was solved to came out without
(test-assert "a qualified wildcard keeps its qualifier" (test-assert "a qualified wildcard keeps its qualifier"