Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[CTYP-33] Support Vars as functions.

  • Loading branch information...
commit 2dd93b6946dbeb3b6741c44f0374cd580919b3ef 1 parent f9007ea
@frenchy64 frenchy64 authored
View
17 src/main/clojure/clojure/core/typed/base_env.clj
@@ -427,10 +427,11 @@ Delay [[[r :variance :covariant]]
:replace
{IDeref (IDeref r)}]
-Var [[]
+Var [[[v :variance :invariant]]
:replace
{AReference (AReference Any Any)
IReference (IReference Any Any)
+ IFn v
IRef (IRef Any Any)
ARef (ARef Any Any)
IDeref (IDeref Any)
@@ -673,8 +674,8 @@ clojure.core/identity (All [x] [x -> x
:object {:id 0}])
clojure.core/gensym (Fn [-> Symbol]
[String -> Symbol])
-clojure.core/intern (Fn [(U Symbol Namespace) Symbol -> Var]
- [(U Symbol Namespace) Symbol Any -> Var])
+clojure.core/intern (Fn [(U Symbol Namespace) Symbol -> (Var Any)]
+ [(U Symbol Namespace) Symbol Any -> (Var Any)])
clojure.core/doall (All [[c :< (U nil (Seqable Any))]]
@@ -1193,16 +1194,16 @@ clojure.core/= [Any Any * -> (U true false)]
clojure.core/integer? (predicate AnyInteger)
clojure.core/number? (predicate Number)
-clojure.core/var? (predicate clojure.lang.Var)
+clojure.core/var? (predicate (clojure.lang.Var Any))
clojure.core/class? (predicate Class)
-clojure.core/resolve (Fn [Symbol -> (U Var Class nil)]
+clojure.core/resolve (Fn [Symbol -> (U (Var Any) Class nil)]
; should &env arg be more accurate?
- [Any Symbol -> (U Var Class nil)])
+ [Any Symbol -> (U (Var Any) Class nil)])
-clojure.core/ns-resolve (Fn [Namespace Symbol -> (U Var Class nil)]
+clojure.core/ns-resolve (Fn [Namespace Symbol -> (U (Var Any) Class nil)]
; should &env arg be more accurate?
- [Namespace Any Symbol -> (U Var Class nil)])
+ [Namespace Any Symbol -> (U (Var Any) Class nil)])
clojure.core/extenders [Any -> (U nil (Seqable (U Class nil)))]
View
21 src/main/clojure/clojure/core/typed/check.clj
@@ -1003,6 +1003,14 @@
('#{clojure.lang.Symbol} (.the-class ^RClass fexpr-type)))
(let [symfn (prs/parse-type '(All [x] [(U (clojure.lang.IPersistentMap Any x) Any) -> (U x nil)]))]
(check-funapp fexpr args (ret symfn) arg-ret-types expected))
+
+ ;Var function
+ (and (r/RClass? fexpr-type)
+ ('#{clojure.lang.Var} (.the-class ^RClass fexpr-type)))
+ (let [{[ftype :as poly?] :poly?} fexpr-type
+ _ (assert (= 1 (count poly?))
+ "Assuming clojure.lang.Var only takes 1 argument")]
+ (check-funapp fexpr args (ret ftype) arg-ret-types expected))
;Error is perfectly good fn type
(r/TCError? fexpr-type)
@@ -1213,10 +1221,11 @@
(add-check-method :the-var
[{:keys [var] :as expr} & [expected]]
- (assoc expr
- expr-type (ret (c/RClass-of Var)
- (fo/-FS fl/-top fl/-bot)
- obj/-empty)))
+ (let [t (var-env/lookup-Var (u/var->symbol var))]
+ (assoc expr
+ expr-type (ret (c/RClass-of Var [t])
+ (fo/-FS fl/-top fl/-bot)
+ obj/-empty))))
;[Any TCResult * -> TCResult]
(defn tc-equiv [comparator & vs]
@@ -4361,7 +4370,7 @@
t (var-env/lookup-Var-nofail vsym)
;def returns a Var
res-expr (assoc expr
- expr-type (ret (c/RClass-of Var)))
+ expr-type (ret (c/RClass-of Var [(or t r/-any)])))
check? (var-env/check-var? vsym)]
(cond
(and check? t)
@@ -4398,7 +4407,7 @@
(or (.isMacro ^Var var)
(not init-provided))
(assoc expr
- expr-type (ret (c/RClass-of Var)))
+ expr-type (ret (c/RClass-of Var [r/-any])))
:else (check-normal-def expr expected))))
View
10 src/main/clojure/clojure/core/typed/subtype.clj
@@ -153,6 +153,7 @@
(if (or (u/p :subtype/query-current-seen
(contains? A [s t]))
(= s t)
+ ; FIXME TypeFn's probably are not between Top/Bottom
(r/Top? t)
(r/Bottom? s)
;TCError is top and bottom
@@ -529,6 +530,15 @@
(r/RClass? t))
(u/p :subtype/RClass (subtype-RClass s t))
+ ; handles Var-as-function
+ (and (r/RClass? s)
+ (r/FnIntersection? t))
+ (if (some #(when (r/FnIntersection? %)
+ (subtype? % t))
+ (map c/fully-resolve-type (c/RClass-supers* s)))
+ *sub-current-seen*
+ (fail! s t))
+
(and (r/CountRange? s)
(r/CountRange? t))
(subtype-CountRange s t)
View
5 src/test/clojure/clojure/core/typed/test/core.clj
@@ -1852,6 +1852,11 @@
(deftest subtype-explosion-test
(is (sub? nil clojure.core.typed.type-rep/TCType)))
+(deftest var-as-function-test
+ (is (cf #'+ [Number * -> Number]))
+ (is (cf (#'+ 1 2)))
+ (is (sub? (Var [-> nil]) [-> nil])))
+
;(reset-caches)
;(chk/abstract-result
Please sign in to comment.
Something went wrong with that request. Please try again.