go: types.sx — composite-literal element checking; Phase 3 bar crossed + 10 tests [nothing]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Phase 3 cont. Adds composite-literal type-checking via go-synth-composite:
[]T{...} — go-check-composite-elems with VAL-TY=T, KEY-TY=nil.
Each plain elem assignable to T; :kv element accepted
(Go's index-keyed shorthand: `[]int{0: 5, 1: 10}`)
with only the value checked.
[N]T{...} — same as slice; result :ty-array N T.
map[K]V{...} — KEY-TY=K, VAL-TY=V. Each :kv pair: key assignable
to K, value to V. Non-:kv elements in maps are
(:type-error :map-elem-missing-key).
The literal's *synthesised* type is the type expression itself, so
nested composites fall out by recursion:
[][]int{[]int{1,2}, []int{3,4}}
→ outer: go-check-composite-elems with VAL-TY=[]int
→ each inner []int{1,2} goes through go-synth-composite recursively,
yielding :ty-slice :ty-name "int" — assignable-equal to VAL-TY.
Coverage: positive cases (homogeneous slices/arrays/maps, empty
slice, nested), and three negative cases (slice element mismatch,
map key mismatch, map value mismatch). Also a decl test:
var x = []int{1, 2, 3} → binds x to :ty-slice :ty-name "int"
Named-type literals (`Point{1,2}`, `pkg.T{...}`) need type-decl-driven
field resolution; deferred. Interface satisfaction and AST-path error
context also remain — neither gates Phase 4.
**Phase 3 acceptance bar (60+) crossed: types 65/65, total 370/370.**
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -262,6 +262,9 @@
|
||||
(go-is-binop-call? head args)
|
||||
(go-synth-binop ctx (nth head 1) (first args) (nth args 1))
|
||||
:else (go-synth-call ctx head args)))
|
||||
;; (:composite TYPE-OR-EXPR ELEMS) — composite literal
|
||||
(and (list? expr) (= (first expr) :composite))
|
||||
(go-synth-composite ctx (nth expr 1) (nth expr 2))
|
||||
:else (list :type-error :unsupported-synth expr))))
|
||||
|
||||
(define
|
||||
@@ -288,6 +291,65 @@
|
||||
(go-type-error? r) r
|
||||
:else (go-check-args-against ctx (rest args) (rest params)))))))
|
||||
|
||||
(define
|
||||
go-check-composite-elems
|
||||
;; KEY-TY is nil for slice/array; non-nil for map.
|
||||
;; For maps, each elem must be (:kv KEY VALUE) — KEY assignable to
|
||||
;; KEY-TY, VALUE to VAL-TY.
|
||||
;; For slice/array, plain exprs assignable to VAL-TY; (:kv K V) is
|
||||
;; Go's index-keyed shorthand (`[]int{0: 5, 1: 10}`) — we type-check
|
||||
;; only the value in v0.
|
||||
(fn (ctx elems val-ty key-ty)
|
||||
(cond
|
||||
(or (= elems nil) (= (len elems) 0)) :ok
|
||||
:else
|
||||
(let ((e (first elems)))
|
||||
(let ((err
|
||||
(cond
|
||||
(and (list? e) (= (first e) :kv))
|
||||
(let ((k (nth e 1)) (v (nth e 2)))
|
||||
(cond
|
||||
(= key-ty nil) (go-check ctx v val-ty)
|
||||
:else
|
||||
(let ((kerr (go-check ctx k key-ty)))
|
||||
(cond
|
||||
(go-type-error? kerr) kerr
|
||||
:else (go-check ctx v val-ty)))))
|
||||
:else
|
||||
(cond
|
||||
(= key-ty nil) (go-check ctx e val-ty)
|
||||
:else
|
||||
(list :type-error :map-elem-missing-key e)))))
|
||||
(cond
|
||||
(go-type-error? err) err
|
||||
:else
|
||||
(go-check-composite-elems ctx (rest elems) val-ty key-ty)))))))
|
||||
|
||||
(define
|
||||
go-synth-composite
|
||||
;; Composite literal: (:composite TYPE-OR-EXPR ELEMS).
|
||||
;; []T{...} — each elem assignable to T; result :ty-slice T
|
||||
;; [N]T{...} — same; result :ty-array N T
|
||||
;; map[K]V{...} — each :kv key:K, value:V; result :ty-map K V
|
||||
;; Named-type literals (Point{...}, pkg.T{...}) require type-decl
|
||||
;; resolution; v0 returns the literal's type-expr as-is without
|
||||
;; element checking.
|
||||
(fn (ctx ty elems)
|
||||
(cond
|
||||
(and (list? ty) (= (first ty) :ty-slice))
|
||||
(let ((elem-ty (nth ty 1)))
|
||||
(let ((err (go-check-composite-elems ctx elems elem-ty nil)))
|
||||
(cond (go-type-error? err) err :else ty)))
|
||||
(and (list? ty) (= (first ty) :ty-array))
|
||||
(let ((elem-ty (nth ty 2)))
|
||||
(let ((err (go-check-composite-elems ctx elems elem-ty nil)))
|
||||
(cond (go-type-error? err) err :else ty)))
|
||||
(and (list? ty) (= (first ty) :ty-map))
|
||||
(let ((key-ty (nth ty 1)) (val-ty (nth ty 2)))
|
||||
(let ((err (go-check-composite-elems ctx elems val-ty key-ty)))
|
||||
(cond (go-type-error? err) err :else ty)))
|
||||
:else ty)))
|
||||
|
||||
(define
|
||||
go-synth-call
|
||||
;; Synth a function call. Returns the result type, or :type-error.
|
||||
|
||||
Reference in New Issue
Block a user