Lower typed list literals to Kotlin
ober
549bed276332a66bed4f912bcce12be451a2e38b
--- a/lib/jerboa/typed/checker.ss +++ b/lib/jerboa/typed/checker.ss @@ -2594,6 +2594,44 @@ (list (cons 'inner-type type-arg)))) errors)))) + (def (infer-list-of args env type-names expr) + (if (null? args) + (values #f (bad-constructor-arity expr 'list-of 1 args)) + (let* ([type-arg (strip-source-annotations (car args))] + [items (cdr args)] + [type-errors (check-type type-arg type-names)]) + (let-values ([(item-irs item-errors) + (infer-args items env type-names)]) + (let ([arg-type-errors + (let loop ([irs item-irs] [forms items] [out '()]) + (cond + [(null? irs) (reverse out)] + [else + (let ([item-type (ir-type (car irs))]) + (loop + (cdr irs) + (cdr forms) + (if (and item-type + (type-assignable? item-type type-arg)) + out + (cons + (make-check-error + 'argument-type-mismatch + "list-of item must match the declared element type" + (list 'list-of type-arg item-type) + (and (pair? forms) + (expr-source (car forms)))) + out))))]))]) + (let ([errors (append type-errors item-errors arg-type-errors)]) + (values + (and (null? errors) + (all-irs-valid? item-irs) + (make-typed-ir-call (list 'List type-arg) + (expr-source expr) 'jvm-list-of 'list-of + item-irs + (list (cons 'inner-type type-arg)))) + errors))))))) + (def (infer-mutable-set-empty args env type-names expr) (if (not (= (length args) 1)) (values #f (bad-constructor-arity expr 'mutable-set-empty 1 args)) @@ -3724,6 +3762,8 @@ [(pair-second) (infer-pair-access args env type-names expr 'pair-second 'jvm-pair-second cdr)] + [(list-of) + (infer-list-of args env type-names expr)] [(mutable-list-empty) (infer-mutable-list-empty args env type-names expr)] [(mutable-set-empty) --- a/lib/jerboa/typed/kotlin/lower.ss +++ b/lib/jerboa/typed/kotlin/lower.ss @@ -393,6 +393,8 @@ (make-kt-member-get (car args) 'size)] [(jvm-list-ref) (make-kt-index-get (car args) (cadr args))] + [(jvm-list-of) + (make-kt-call (kt-name1 'listOf) args)] [(jvm-map-ref-or-null) (make-kt-index-get (car args) (cadr args))] [(jvm-set-contains) --- a/tests/test-typed-kotlin.ss +++ b/tests/test-typed-kotlin.ss @@ -971,6 +971,7 @@ (define list-form '(typed-library (sample typed lists) (export make-Cell Cell? Cell-x listCount firstX secondName + firingArcs emptyCells appendCell removeCell replaceCell replaceNames) (type Int32) (type Float32) @@ -983,6 +984,8 @@ (Cell-x (list-ref cells (int32 0)))) (def (secondName (cells : (List Cell))) : String (Cell-name (list-ref cells (int32 1)))) + (def (firingArcs) : (List String) + (list-of String "FA" "FH" "RX")) (def (emptyCells) : (MutableList Cell) (mutable-list-empty Cell)) (def (appendCell (cells : (MutableList Cell)) (cell : Cell)) : Unit @@ -1011,6 +1014,9 @@ (test-contains "List ref preserves element type" list-kotlin "return cells[1].name") +(test-contains "List literal lowers to Kotlin listOf" + list-kotlin + "return listOf(\"FA\", \"FH\", \"RX\")") (test-contains "MutableList empty lowers to typed ArrayList" list-kotlin "return ArrayList<Cell>()")