Add typed Kotlin mutable set construction
ober
00a2198245678a73086e18bb6c893779b6100c26
--- a/lib/jerboa/typed/checker.ss +++ b/lib/jerboa/typed/checker.ss @@ -1916,6 +1916,19 @@ (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)) + (let* ([type-arg (strip-source-annotations (car args))] + [errors (check-type type-arg type-names)]) + (values + (and (null? errors) + (make-typed-ir-call (list 'MutableSet type-arg) + (expr-source expr) 'jvm-mutable-set-empty 'mutable-set-empty + '() + (list (cons 'inner-type type-arg)))) + errors)))) + (def (infer-mutable-list-add args env type-names expr) (if (not (= (length args) 2)) (values #f (bad-constructor-arity expr 'mutable-list-add! 2 args)) @@ -2659,6 +2672,8 @@ (infer-pair args env type-names expr)] [(mutable-list-empty) (infer-mutable-list-empty args env type-names expr)] + [(mutable-set-empty) + (infer-mutable-set-empty args env type-names expr)] [(mutable-list-add!) (infer-mutable-list-add args env type-names expr)] [(mutable-set-add!) --- a/lib/jerboa/typed/core.ss +++ b/lib/jerboa/typed/core.ss @@ -137,6 +137,7 @@ jvm-list-ref jvm-map-ref-or-null jvm-set-contains + jvm-mutable-set-empty jvm-mutable-set-add jvm-mutable-list-empty jvm-mutable-list-add --- a/lib/jerboa/typed/kotlin/lower.ss +++ b/lib/jerboa/typed/kotlin/lower.ss @@ -356,6 +356,12 @@ (make-kt-index-get (car args) (cadr args))] [(jvm-set-contains) (make-kt-member-call (car args) 'contains (cdr args))] + [(jvm-mutable-set-empty) + (make-kt-new + (make-kt-type 'HashSet #f + (list (typed-type->kotlin-type + (info-ref info 'inner-type 'Any)))) + '())] [(jvm-mutable-set-add) (make-kt-block (list --- a/tests/test-typed-checker.ss +++ b/tests/test-typed-checker.ss @@ -1061,6 +1061,14 @@ (set-contains? ids id))))) '()) +(test "mutable-set empty returns a typed mutable set" + (error-kinds + '(typed-library (body mutable-set-empty-ok) + (export make-ids) + (def (make-ids) : (MutableSet String) + (mutable-set-empty String)))) + '()) + (test "mutable-set add rejects wrong element type" (error-kinds '(typed-library (body mutable-set-wrong-element) --- a/tests/test-typed-kotlin.ss +++ b/tests/test-typed-kotlin.ss @@ -664,6 +664,8 @@ (export seen? remember!) (def (seen? (ids : (Set String)) (id : String)) : Bool (set-contains? ids id)) + (def (emptyIds) : (MutableSet String) + (mutable-set-empty String)) (def (remember! (ids : (MutableSet String)) (id : String)) : Unit (mutable-set-add! ids id)))) @@ -678,6 +680,9 @@ (test-contains "MutableSet type lowers to Kotlin generics" set-kotlin "fun remember_bang(ids: MutableSet<String>, id: String)") +(test-contains "MutableSet empty lowers to typed HashSet" + set-kotlin + "return HashSet<String>()") (test-contains "MutableSet add lowers to Kotlin add" set-kotlin "ids.add(id)")