Permalink
Browse files

Handle an expected type on def forms

  • Loading branch information...
1 parent e24b461 commit efb90909b74aca1aaec53d48da49b3f89ebe4fe7 @frenchy64 frenchy64 committed Sep 4, 2013
Showing with 18 additions and 5 deletions.
  1. +10 −5 src/main/clojure/clojure/core/typed/check.clj
  2. +8 −0 src/test/clojure/clojure/core/typed/test/core.clj
@@ -4389,14 +4389,16 @@
;[Expr (Option TCResult) -> Expr]
(defn check-normal-def [{:keys [var init init-provided env] :as expr} & [expected]]
- (assert (not expected))
(assert init-provided)
(let [vsym (u/var->symbol var)
warn-if-unannotated? (ns-opts/warn-on-unannotated-vars? (expr-ns expr))
t (var-env/lookup-Var-nofail vsym)
;def returns a Var
+ actual-t (c/RClass-of Var [(or t r/-any)])
+ _ (when (and expected (not (sub/subtype? actual-t (ret-t expected))))
+ (expected-error actual-t (ret-t expected)))
res-expr (assoc expr
- expr-type (ret (c/RClass-of Var [(or t r/-any)])))
+ expr-type (ret actual-t))
check? (var-env/check-var? vsym)]
(cond
(and check? t)
@@ -4425,7 +4427,6 @@
;TODO print a hint that `ann` forms must be wrapping in `cf` at the REPL
(add-check-method :def
[{:keys [var init init-provided env] :as expr} & [expected]]
- (assert (not expected) expected)
(assert (:line env))
;(prn "Checking def" var)
(binding [vs/*current-env* env
@@ -4434,8 +4435,12 @@
;ignore macro definitions and declare
(or (.isMacro ^Var var)
(not init-provided))
- (assoc expr
- expr-type (ret (c/RClass-of Var [r/-any])))
+ (let [actual-t (c/RClass-of Var [r/-any])
+ _ (when (and expected
+ (not (sub/subtype? actual-t (ret-t expected))))
+ (expected-error actual-t (ret-t expected)))]
+ (assoc expr
+ expr-type (ret actual-t)))
:else (check-normal-def expr expected))))
@@ -1872,6 +1872,14 @@
(deftest typed-deps-fail-gracefully-test
(is (cf (clojure.core.typed/typed-deps clojure.core.async))))
+(deftest def-expected-test
+ (is (cf (do
+ (clojure.core.typed/ann foo Any)
+ (clojure.core.typed/ann-form (def foo 1) Any))))
+ (is (cf (do
+ (clojure.core.typed/ann foo Any)
+ (clojure.core.typed/ann-form (def foo) Any)))))
+
;(reset-caches)
;(chk/abstract-result

0 comments on commit efb9090

Please sign in to comment.