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:
14
semen.scm
14
semen.scm
@@ -1116,7 +1116,8 @@
|
|||||||
;;; A type we cannot work out is read as a value, which is what the
|
;;; 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
|
;;; 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
|
;;; 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)
|
(define (walk-member-path form env)
|
||||||
(let ((base (second form)))
|
(let ((base (second form)))
|
||||||
(let step ((expr (walk-operand base env))
|
(let step ((expr (walk-operand base env))
|
||||||
@@ -1125,7 +1126,10 @@
|
|||||||
(told? #f))
|
(told? #f))
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
(copy-form-source! form expr)
|
(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?)
|
(unless (or type told?)
|
||||||
(let ((walked (drop-right (cddr form) (length fields))))
|
(let ((walked (drop-right (cddr form) (length fields))))
|
||||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
@@ -1135,6 +1139,12 @@
|
|||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(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)
|
(define (lvalue? expr)
|
||||||
(cond ((symbol? expr) #t)
|
(cond ((symbol? expr) #t)
|
||||||
((vector? expr) #t) ; a compound literal is one
|
((vector? expr) #t) ; a compound literal is one
|
||||||
|
|||||||
@@ -742,7 +742,8 @@ whitespace aside, THEN?"
|
|||||||
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
|
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
|
||||||
"*(p).x")))
|
"*(p).x")))
|
||||||
(test-assert "member access through a cast"
|
(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"))
|
"(*(struct s*)a).f"))
|
||||||
;; ...without gaining parens where none are due
|
;; ...without gaining parens where none are due
|
||||||
(test-assert "a bare dereference is left alone"
|
(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"
|
(test-assert "which one step through it does not need"
|
||||||
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
|
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
|
||||||
"cannot type")))
|
"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 `->'
|
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
||||||
;; is an ordinary head -- a threading macro that nobody imported,
|
;; is an ordinary head -- a threading macro that nobody imported,
|
||||||
;; and a call like any other unknown
|
;; and a call like any other unknown
|
||||||
|
|||||||
Reference in New Issue
Block a user