1
0
forked from alex-eg/sex

read a spread self as the pointer it is

(self * const v) typed the method's self as `self', so a call passed a
value where the method took a pointer, and a spec written one way
refused a realization written the other. A const receiver handed to a
method taking (* v) is now refused rather than left to cc's warning.
This commit is contained in:
2026-10-14 16:24:00 +03:00
parent f5c53cdd41
commit 5ba55f05a5
2 changed files with 53 additions and 21 deletions

View File

@@ -443,17 +443,11 @@
(define (make-fn-env fn-form)
(let ((env (make-hash-table))
(parameters (make-hash-table)))
;; `(p (* struct point))' writes the type in one piece and
;; `(p * struct point)' spreads it over the rest, so the type is
;; the whole tail whenever there is more of it than one element
(for-each (lambda (param)
(when (and (pair? param) (pair? (cdr param))
(named-arg? param))
(when (named-arg? param)
(hash-table-set! parameters
(first param)
(if (null? (cddr param))
(second param)
(cdr param)))))
(named-arg-type param))))
(sex-fn-arglist fn-form))
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
(set! (hash-table-ref env :lambda-counter) 0)
@@ -462,13 +456,22 @@
(set! (hash-table-ref env :scopes) (list parameters))
env))
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int).
;;; Anything that is not a plain (name type) -- a variadic tail -- goes
;;; through untouched.
;;; `(p (* struct point))' writes the type in one piece and
;;; `(p * struct point)' spreads it over the rest; both are
;;; `(* struct point)'
(define (named-arg-type param)
(if (null? (cddr param))
(second param)
(cdr param)))
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
;;; Anything that names nothing -- a variadic tail -- goes through
;;; untouched.
(define (arglist-types arglist)
(map (lambda (param)
(if (and (pair? param) (= 2 (length param)) (named-arg? param))
(list (second param))
(if (named-arg? param)
(list (named-arg-type param))
param))
arglist))
@@ -876,17 +879,25 @@
(copy-form-source!
form
`(,method
,(method-receiver method (and target #t)
,(method-receiver method (or target written) (and target #t)
(walk-statement receiver env) form)
,@(map (lambda (argument) (walk-statement argument env))
(cddr form))))))))
;;; A method taking (* Self) wants an address and one taking Self wants
;;; a value; the receiver is one or the other, so one of the two gets
;;; `&' or `*' put in front of it
(define (method-receiver method pointer? receiver form)
(let ((wants-pointer? (let ((self (method-self-type method)))
(and (pair? self) (eq? '* (car self))))))
;;; `&' or `*' put in front of it.
;;;
;;; OBJECT is what the receiver holds or points at: a `(const v)' handed
;;; to a method taking (* v) would be quietly stripped of its const.
(define (method-receiver method object pointer? receiver form)
(let* ((self (method-self-type method))
(wants-pointer? (and (pair? self) (eq? '* (car self)))))
(when (and wants-pointer?
(const-type? object)
(not (const-type? (pointer-target self))))
(sex-error form "method may change its receiver, and this one is const"
method))
(cond ((eq? wants-pointer? pointer?) receiver)
((not wants-pointer?) `(* ,receiver))
;; `&(f())' is not C, and the method cannot be given a
@@ -937,8 +948,10 @@
(eq? 'fn (car signature))
(pair? (cdr signature))
(pair? (second signature))
(let ((self (car (second signature))))
(if (pair? self) (car self) self)))))
(unwrap-type (car (second signature))))))
(define (const-type? type)
(and (pair? type) (eq? 'const (car type))))
(define (as-closure-type type)