From f84010eda87b228af83f54a2a317cf8169752c05 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Thu, 15 Oct 2026 14:47:12 +0300 Subject: [PATCH] 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. --- semen.scm | 14 ++++++++++++-- tests/codegen.scm | 11 ++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/semen.scm b/semen.scm index 8dac182e..7d3cfc85 100644 --- a/semen.scm +++ b/semen.scm @@ -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 diff --git a/tests/codegen.scm b/tests/codegen.scm index 552c552f..48cefd47 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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