Skip to content

Commit

Permalink
handle nullability check before true magicasting
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrand committed Dec 6, 2023
1 parent 1e081ac commit d4f2491
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion clj/src/cljd/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@
(cond
(= 'dc.dynamic (:canon-qname expected-type)) dart-expr ; TODO: should be covered by is-assignable?
(is-assignable? expected-type actual-type) dart-expr ; <1>
(and (nullable-type? expected-type) (nullable-type? actual-type))
(and (nullable-type? expected-type) (nullable-type? actual-type)) ; <2>
(if-some [expected-type+ (positive-type expected-type)]
(with-once [dart-expr dart-expr] env
`(dart/if (dart/. nil "!=" ~dart-expr)
Expand All @@ -1543,6 +1543,13 @@
~(magicast dart-expr expected-type+ actual-type env)
nil))
(list 'dart/let [[nil dart-expr]] nil))
(and
(not= 'dc.dynamic (:canon-qname actual-type))
(nullable-type? actual-type))
; expected-type is not nullable because of the previous check <2>
(if-some [actual-type+ (positive-type actual-type)]
(recur (list 'dart/as dart-expr actual-type+) expected-type actual-type+ env)
(throw (Exception. "Can't cast a null expression to something non-nullable.")))
;; When inlined #dart[], we keep it inlines
;; TODO: don't like the (vector? dart-expr) check, it smells bad
(and (= 'dc.List (:canon-qname expected-type) (:canon-qname actual-type))
Expand Down
7 changes: 4 additions & 3 deletions clj/test/cljd/test_clojure/core_test_cljd.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,8 @@
(deftest nil-as-test-expression-in-when-some
(is (nil? (when-some [x nil] 42))))

(deftest nametobedefined
(let [f (fn [^Function? f ^Iterable l]
(deftest throw-when-passing-a-nullable-fn-instead-of-a-non-nullable-fn
(let [f (fn [^^{:fn-type {:fixed-params-types [dynamic]
:ret-type bool}} Function? f ^Iterable l]
(.any l f))]
(is (thrown? Exception (f nil [1])))))
(is (thrown? Error (f nil [1])))))

0 comments on commit d4f2491

Please sign in to comment.