From e646ddd0ed2a031bf546006f908d03879d084a4b Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 17:04:20 +0300 Subject: [PATCH] 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. --- infer.scm | 30 +++++++++++++++++------------- tests/codegen.scm | 9 +++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/infer.scm b/infer.scm index 78f5c1a..c661f61 100644 --- a/infer.scm +++ b/infer.scm @@ -53,11 +53,13 @@ ;;; 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 -;;; 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 - (%make-tvar ref) + (%make-tvar ref quals) 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 ;;; making it up, so `int', `(unsigned int)' and `(long long)' are all @@ -114,7 +116,7 @@ (quals alias-quals)) (define (fresh-tvar) - (%make-tvar #f)) + (%make-tvar #f (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 @@ -198,7 +200,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))) @@ -263,20 +265,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)))) ;;; --------------------------------------------------------------- @@ -407,7 +411,7 @@ ;; Without recursive types this cannot trigger. It is four lines, ;; and the alternative to having it is a hang. ((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) (let ((t (resolve type))) diff --git a/tests/codegen.scm b/tests/codegen.scm index 9beb4eb..588c2f4 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -605,6 +605,15 @@ (emits? "(typedef count int) (fn g () count (return 0)) (fn f () void (var 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 ;; initializer has none and takes one from its context (test-assert "a compound literal answers a bare wildcard"