1
0
forked from alex-eg/sex

refuse a field a struct declared here does not have

The path was walked on and handed to cc, which named the missing field
only in C; a test leaned on a struct s an earlier test had declared.
This commit is contained in:
2026-10-15 14:47:12 +03:00
parent 2279c371f2
commit f84010eda8
2 changed files with 22 additions and 3 deletions

View File

@@ -1116,7 +1116,8 @@
;;; A type we cannot work out is read as a value, which is what the
;;; path would have meant before any of this. The warning names what
;;; could not be typed: the base, or the path up to a field of a struct
;;; only C knows -- (. font handle), not font.
;;; only C knows -- (. font handle x) warns of (. font handle), not
;;; font. A field a struct declared here lacks is an error.
(define (walk-member-path form env)
(let ((base (second form)))
(let step ((expr (walk-operand base env))
@@ -1125,7 +1126,10 @@
(told? #f))
(if (null? fields)
(copy-form-source! form expr)
(let ((target (pointer-target type)))
(let* ((target (pointer-target type))
(known (type-fields (or target type))))
(when (and (pair? known) (not (assq (field-name (car fields)) known)))
(sex-error form "no field of this name in" (or target type) (car fields)))
(unless (or type told?)
(let ((walked (drop-right (cddr form) (length fields))))
(sex-warning form "cannot type this; `.' reads it as a value"
@@ -1135,6 +1139,12 @@
(cdr fields)
(or told? (not type))))))))
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
(define (field-name field)
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field)))
(second field)
field))
(define (lvalue? expr)
(cond ((symbol? expr) #t)
((vector? expr) #t) ; a compound literal is one

View File

@@ -742,7 +742,8 @@ whitespace aside, THEN?"
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
"*(p).x")))
(test-assert "member access through a cast"
(emits? (in-fn "(var n int (. (* (cast a (* (struct s)))) f))")
(emits? (string-append "(struct s ((f int)))"
(in-fn "(var n int (. (* (cast a (* (struct s)))) f))"))
"(*(struct s*)a).f"))
;; ...without gaining parens where none are due
(test-assert "a bare dereference is left alone"
@@ -884,6 +885,14 @@ whitespace aside, THEN?"
(test-assert "which one step through it does not need"
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
"cannot type")))
(test-assert "but a struct declared here has only the fields it names"
(reports? (in-line "(= (. p z) 1)") "no field of this name"))
(test-assert "the last step included"
(reports? (in-line "(= (. lp a z) 1)") "no field of this name"))
(test-assert "an indexed one by its name"
(reports? (string-append "(struct v ((xs [int 2])))"
(in-fn "(var w (struct v)) (= (. w [ys 0]) 1)"))
"no field of this name"))
;; the arrow is `%->' and nothing else writes one, so a bare `->'
;; is an ordinary head -- a threading macro that nobody imported,
;; and a call like any other unknown