From 3436c4ff3ea02c82fd8820b199df65c4e66ed8da Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sun, 18 Oct 2026 22:02:55 +0300 Subject: [PATCH] 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 *. --- infer.scm | 23 ++++++++++++----------- tests/codegen.scm | 9 +++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/infer.scm b/infer.scm index c5f7f12..32300aa 100644 --- a/infer.scm +++ b/infer.scm @@ -78,8 +78,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 (make-array-type elt size) array-type? @@ -222,15 +223,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))) @@ -383,7 +384,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)) diff --git a/tests/codegen.scm b/tests/codegen.scm index 0b5cfa9..d8853c4 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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"