more-tests-and-corner-cases #72

Merged
pkulev merged 74 commits from more-tests-and-corner-cases into main 2026-10-27 22:21:02 +01:00
2 changed files with 26 additions and 13 deletions
Showing only changes of commit 47b98e41df - Show all commits

View File

@@ -52,11 +52,13 @@
;;; A type variable is a mutable cell. `ref' is #f while unsolved and ;;; A type variable is a mutable cell. `ref' is #f while unsolved and
;;; the type it stands for once bound -- union-find, with the path ;;; the type it stands for once bound -- union-find, with the path
;;; compression done in `resolve'. ;;; compression done in `resolve'. `quals' are the ones written on it --
;;; `(const _)' -- and go onto whatever it is bound to.
(define-record-type <tvar> (define-record-type <tvar>
(%make-tvar ref) (%make-tvar ref quals)
Review

Uh... Rebase artifact?

Uh... Rebase artifact?
tvar? tvar?
(ref tvar-ref tvar-ref-set!)) (ref tvar-ref tvar-ref-set!)
(quals tvar-quals tvar-quals-set!))
;;; A primitive or otherwise nominal type. `name' is the list of words ;;; A primitive or otherwise nominal type. `name' is the list of words
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all ;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
@@ -113,7 +115,7 @@
(quals alias-quals)) (quals alias-quals))
(define (fresh-tvar) (define (fresh-tvar)
(%make-tvar #f)) (%make-tvar #f (list)))
;;; Follow a bound variable to what it stands for, compressing the path ;;; Follow a bound variable to what it stands for, compressing the path
;;; on the way out. Every procedure that looks at a type's shape starts ;;; on the way out. Every procedure that looks at a type's shape starts
@@ -197,7 +199,7 @@
;; The type-level wildcard. It is a fresh variable wherever it ;; The type-level wildcard. It is a fresh variable wherever it
;; appears, which is what makes partial types -- `(* _)', `(¤ _ 4)' ;; appears, which is what makes partial types -- `(* _)', `(¤ _ 4)'
;; -- fall out for free rather than needing their own grammar. ;; -- fall out for free rather than needing their own grammar.
((equal? words '(_)) (fresh-tvar)) ((equal? words '(_)) (with-quals (fresh-tvar) quals))
((and (null? (cdr words)) (get-underlying-type (car words))) ((and (null? (cdr words)) (get-underlying-type (car words)))
=> (lambda (target) => (lambda (target)
(make-alias (car words) (parse-type target) quals))) (make-alias (car words) (parse-type target) quals)))
@@ -262,20 +264,22 @@
(if (list? s) s (list s)))) (if (list? s) s (list s))))
;;; Re-qualify a parsed type, for the grouping case `(const (struct s))' ;;; Re-qualify a parsed type, for the grouping case `(const (struct s))'
;;; where the qualifier is read before the thing it qualifies. ;;; where the qualifier is read before the thing it qualifies, and for a
;;; variable solved to a type: `(* const _)' against `(const int)' says
;;; const once.
(define (with-quals type quals) (define (with-quals type quals)
(define (plus written) (append quals (remove (lambda (q) (memq q quals)) written)))
(if (null? quals) (if (null? quals)
type type
(cond ((prim-type? type) (make-prim (prim-name type) (cond ((prim-type? type) (make-prim (prim-name type) (plus (prim-quals type))))
(append quals (prim-quals type)))) ((ptr-type? type) (make-ptr (ptr-target type) (plus (ptr-quals type))))
((ptr-type? type) (make-ptr (ptr-target type)
(append quals (ptr-quals type))))
((agg-type? type) (make-agg (agg-kind type) (agg-name type) ((agg-type? type) (make-agg (agg-kind type) (agg-name type)
(agg-spelling type) (agg-spelling type)
(append quals (agg-quals type)))) (plus (agg-quals type))))
((alias-type? type) (make-alias (alias-name type) ((alias-type? type) (make-alias (alias-name type)
(alias-expansion type) (alias-expansion type)
(append quals (alias-quals type)))) (plus (alias-quals type))))
((tvar? type) (tvar-quals-set! type (plus (tvar-quals type))) type)
(else type)))) (else type))))
;;; --------------------------------------------------------------- ;;; ---------------------------------------------------------------
@@ -406,7 +410,7 @@
;; Without recursive types this cannot trigger. It is four lines, ;; Without recursive types this cannot trigger. It is four lines,
;; and the alternative to having it is a hang. ;; and the alternative to having it is a hang.
((occurs? v t) (sex-error form "recursive type" (unparse-type v))) ((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
(else (tvar-ref-set! v t) #t))) (else (tvar-ref-set! v (with-quals t (tvar-quals v))) #t)))
(define (occurs? v type) (define (occurs? v type)
(let ((t (resolve type))) (let ((t (resolve type)))

View File

@@ -605,6 +605,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()"))
;; 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"
(emits? (in-fn "(var k (const _) 5)") "const int k = 5"))
(test-assert "behind a pointer too"
(emits? (in-fn "(var p (* const _) (& a))") "const int * p = &a"))
(test-assert "and says it once, solved to a type already saying it"
(emits? (in-fn "(var k (const int) 1) (var p (* const _) (& k))")
"const int * p = &k"))
;; a compound literal carries its own type, where a brace ;; a compound literal carries its own type, where a brace
;; initializer has none and takes one from its context ;; initializer has none and takes one from its context
(test-assert "a compound literal answers a bare wildcard" (test-assert "a compound literal answers a bare wildcard"