keep the qualifiers written on a wildcard

(const _) parsed to a type variable, which had nowhere to keep the
const, so the type it was solved to came out without one -- for
(const _) and for the target of (* const _) alike.
This commit is contained in:
2026-10-16 17:04:20 +03:00
parent 39a5f4de6b
commit b68b91b119
2 changed files with 27 additions and 14 deletions

View File

@@ -89,14 +89,16 @@
;;; (`numeric', and one day `ord'); see "constraints" below. `rigid?'
;;; marks a variable that must not unify with anything but itself --
;;; unused until a `fn' grows type parameters, and five lines now
;;; against an IR change later.
;;; against an IR change later. `quals' are the ones written on it --
;;; `(const _)' -- and go onto whatever it is bound to.
(define-record-type <tvar>
(%make-tvar id ref classes rigid?)
(%make-tvar id ref classes rigid? quals)
tvar?
(id tvar-id)
(ref tvar-ref tvar-ref-set!)
(classes tvar-classes tvar-classes-set!)
(rigid? tvar-rigid?))
(rigid? tvar-rigid?)
(quals tvar-quals tvar-quals-set!))
;;; A primitive or otherwise nominal type. `name' is the list of words
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
@@ -167,11 +169,11 @@
(define (fresh-tvar . classes)
(set! tvar-counter (+ tvar-counter 1))
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #f))
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #f (list)))
(define (fresh-rigid-tvar . classes)
(set! tvar-counter (+ tvar-counter 1))
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #t))
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #t (list)))
;;; 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
@@ -265,7 +267,7 @@
;; The type-level wildcard. It is a fresh variable wherever it
;; appears, which is what makes partial types -- `(* _)', `(¤ _ 4)'
;; -- 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)))
=> (lambda (target)
(make-alias (car words) (parse-type target) quals)))
@@ -330,20 +332,22 @@
(if (list? s) s (list 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 (plus written) (append quals (remove (lambda (q) (memq q quals)) written)))
(if (null? quals)
type
(cond ((prim-type? type) (make-prim (prim-name type)
(append quals (prim-quals type))))
((ptr-type? type) (make-ptr (ptr-target type)
(append quals (ptr-quals type))))
(cond ((prim-type? type) (make-prim (prim-name type) (plus (prim-quals type))))
((ptr-type? type) (make-ptr (ptr-target type) (plus (ptr-quals type))))
((agg-type? type) (make-agg (agg-kind type) (agg-name type)
(agg-spelling type)
(append quals (agg-quals type))))
(plus (agg-quals type))))
((alias-type? type) (make-alias (alias-name 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))))
;;; ---------------------------------------------------------------
@@ -620,7 +624,7 @@
(else
(when (tvar? t)
(tvar-classes-set! t (lset-union eq? (tvar-classes t) (tvar-classes v))))
(tvar-ref-set! v t)
(tvar-ref-set! v (with-quals t (tvar-quals v)))
(unless (tvar? t)
(check-classes (tvar-classes v) t form))
#t)))

View File

@@ -663,6 +663,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]"))
;; 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
;; initializer has none and takes one from its context
(test-assert "a compound literal answers a bare wildcard"