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:
23
infer.scm
23
infer.scm
@@ -119,8 +119,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?
|
||||
@@ -290,15 +291,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)))
|
||||
@@ -591,7 +592,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))
|
||||
|
||||
@@ -735,6 +735,15 @@ whitespace aside, THEN?"
|
||||
"struct p* x = &s"))
|
||||
(test-assert "a wildcard inside an array"
|
||||
(emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]"))
|
||||
;; 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"
|
||||
|
||||
Reference in New Issue
Block a user