From 5ba55f05a5efd46366e789a373788cdf3d9e29e1 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 14 Oct 2026 16:24:00 +0300 Subject: [PATCH] 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. --- semen.scm | 53 +++++++++++++++++++++++++---------------- tests/encapsulation.scm | 21 +++++++++++++++- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/semen.scm b/semen.scm index c4954dc..55e5b6c 100644 --- a/semen.scm +++ b/semen.scm @@ -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) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 22f4ced..ce8d772 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -13,7 +13,8 @@ "(record vec2 ((x float) (y float)))" "(ops vec2" " (pub fn length ((self (* const vec2))) float (return 1.0))" - " (fn sum-of ((self vec2)) float (return (. self x))))" + " (fn sum-of ((self vec2)) float (return (. self x)))" + " (fn scale ((self (* vec2))) void (= (. self x) 2.0)))" "(fn mk () vec2 (return #(vec2 : 1.0 2.0)))" "(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) " (apply string-append body) ")")) @@ -111,6 +112,19 @@ (reports? (ops-src "(.nope v)") "no such method on")) (test-assert "and so is a receiver with no type to dispatch on" (reports? "(fn f () void (var n int 1) (.get n))" "no such method on")) + (test-assert "a self spread over the parameter is a pointer self too" + (emits? (string-append + "(record w ((x int)))" + "(ops w (fn get ((self * const w)) int (return (. self x))))" + "(fn f () int (var a w #(w : 1)) (var p (* const w) (& a))" + " (return (+ (.get a) (.get p))))") + "wːget(&a) + wːget(p)")) + (test-assert "a const receiver is not handed to a method that may change it" + (reports? (ops-src "(var c (* const vec2) p) (.scale c)") + "this one is const")) + (test-assert "one that cannot change it takes it" + (emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))") + "vec2ːlength(c)")) (test-assert "an address cannot be taken of a call" (reports? (ops-src "(.length (mk))") "receiver is not an lvalue")) ;; the inner call has to be typed to find the outer one's receiver @@ -231,6 +245,11 @@ "(spec (p T) (fn s ((a (* const T))) void))" "(realize (p v) (fn s ((a v)) void (return)))") "arguments do not match")) + (test-assert "but not on how a pointer is spelled" + (emits? (string-append "(record v ((x int)))" + "(spec (p T) (fn s ((a * const T)) void))" + "(realize (p v) (fn s ((a (* const v))) void (return)))") + "void vːpːs (const v * a)")) (test-assert "a method name already taken on the type is refused" (reports? (string-append "(record v ((x int)))" "(ops v (fn s ((a (* const v))) void (return)))"