Repository navigation
Expand file tree
/
Copy pathcompilation.clj
More file actions
453 lines (386 loc) · 18.6 KB
/
Copy pathcompilation.clj
File metadata and controls
453 lines (386 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
; Author: Frantisek Sodomka
(ns clojure.test-clojure.compilation
(:import (clojure.lang Compiler Compiler$CompilerException))
(:require [clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen]
[clojure.test-clojure.compilation.line-number-examples :as line])
(:use clojure.test
[clojure.test-helper :only (should-not-reflect should-print-err-message)]))
; http://clojure.org/compilation
; compile
; gen-class, gen-interface
(deftest test-compiler-metadata
(let [m (meta #'when)]
(are [x y] (= x y)
(list? (:arglists m)) true
(> (count (:arglists m)) 0) true
(string? (:doc m)) true
(> (.length (:doc m)) 0) true
(string? (:file m)) true
(> (.length (:file m)) 0) true
(integer? (:line m)) true
(> (:line m) 0) true
(integer? (:column m)) true
(> (:column m) 0) true
(:macro m) true
(:name m) 'when )))
(deftest test-embedded-constants
(testing "Embedded constants"
(is (eval `(= Boolean/TYPE ~Boolean/TYPE)))
(is (eval `(= Byte/TYPE ~Byte/TYPE)))
(is (eval `(= Character/TYPE ~Character/TYPE)))
(is (eval `(= Double/TYPE ~Double/TYPE)))
(is (eval `(= Float/TYPE ~Float/TYPE)))
(is (eval `(= Integer/TYPE ~Integer/TYPE)))
(is (eval `(= Long/TYPE ~Long/TYPE)))
(is (eval `(= Short/TYPE ~Short/TYPE)))))
(deftest test-compiler-resolution
(testing "resolve nonexistent class create should return nil (assembla #262)"
(is (nil? (resolve 'NonExistentClass.))))
(testing "resolve nonexistent class should return nil"
(is (nil? (resolve 'NonExistentClass.Name)))))
(deftest test-no-recur-across-try
(testing "don't recur to function from inside try"
(is (thrown? Compiler$CompilerException
(eval '(fn [x] (try (recur 1)))))))
(testing "don't recur to loop from inside try"
(is (thrown? Compiler$CompilerException
(eval '(loop [x 5]
(try (recur 1)))))))
(testing "don't recur to loop from inside of catch inside of try"
(is (thrown? Compiler$CompilerException
(eval '(loop [x 5]
(try
(catch Exception e
(recur 1))))))))
(testing "don't recur to loop from inside of finally inside of try"
(is (thrown? Compiler$CompilerException
(eval '(loop [x 5]
(try
(finally
(recur 1))))))))
(testing "don't get confused about what the recur is targeting"
(is (thrown? Compiler$CompilerException
(eval '(loop [x 5]
(try (fn [x]) (recur 1)))))))
(testing "don't allow recur across binding"
(is (thrown? Compiler$CompilerException
(eval '(fn [x] (binding [+ *] (recur 1)))))))
(testing "allow loop/recur inside try"
(is (= 0 (eval '(try (loop [x 3]
(if (zero? x) x (recur (dec x)))))))))
(testing "allow loop/recur fully inside catch"
(is (= 3 (eval '(try
(throw (Exception.))
(catch Exception e
(loop [x 0]
(if (< x 3) (recur (inc x)) x))))))))
(testing "allow loop/recur fully inside finally"
(is (= "012" (eval '(with-out-str
(try
:return-val-discarded-because-of-with-out-str
(finally (loop [x 0]
(when (< x 3)
(print x)
(recur (inc x)))))))))))
(testing "allow fn/recur inside try"
(is (= 0 (eval '(try
((fn [x]
(if (zero? x)
x
(recur (dec x))))
3)))))))
;; disabled until build box can call java from mvn
#_(deftest test-numeric-dispatch
(is (= "(int, int)" (TestDispatch/someMethod (int 1) (int 1))))
(is (= "(int, long)" (TestDispatch/someMethod (int 1) (long 1))))
(is (= "(long, long)" (TestDispatch/someMethod (long 1) (long 1)))))
(deftest test-CLJ-671-regression
(testing "that the presence of hints does not cause the compiler to infinitely loop"
(letfn [(gcd [x y]
(loop [x (long x) y (long y)]
(if (== y 0)
x
(recur y ^Long(rem x y)))))]
(is (= 4 (gcd 8 100))))))
;; ensure proper use of hints / type decls
(defn hinted
(^String [])
(^Integer [a])
(^java.util.List [a & args]))
;; fn names need to be fully-qualified because should-not-reflect evals its arg in a throwaway namespace
(deftest recognize-hinted-arg-vector
(should-not-reflect #(.substring (clojure.test-clojure.compilation/hinted) 0))
(should-not-reflect #(.floatValue (clojure.test-clojure.compilation/hinted "arg")))
(should-not-reflect #(.size (clojure.test-clojure.compilation/hinted :many :rest :args :here))))
(deftest CLJ-1232-qualify-hints
(let [arglists (-> #'clojure.test-clojure.compilation/hinted meta :arglists)]
(is (= 'java.lang.String (-> arglists first meta :tag)))
(is (= 'java.lang.Integer (-> arglists second meta :tag)))))
(deftest CLJ-1232-return-type-not-imported
(is (thrown-with-cause-msg? Compiler$CompilerException #"Unable to resolve classname: Closeable"
(eval '(defn a ^Closeable []))))
(is (thrown-with-cause-msg? Compiler$CompilerException #"Unable to resolve classname: Closeable"
(eval '(defn a (^Closeable []))))))
(defn ^String hinting-conflict ^Integer [])
(deftest calls-use-arg-vector-hint
(should-not-reflect #(.floatValue (clojure.test-clojure.compilation/hinting-conflict)))
(should-print-err-message #"(?s)Reflection warning.*"
#(.substring (clojure.test-clojure.compilation/hinting-conflict) 0)))
(deftest deref-uses-var-tag
(should-not-reflect #(.substring clojure.test-clojure.compilation/hinting-conflict 0))
(should-print-err-message #"(?s)Reflection warning.*"
#(.floatValue clojure.test-clojure.compilation/hinting-conflict)))
(defn ^String legacy-hinting [])
(deftest legacy-call-hint
(should-not-reflect #(.substring (clojure.test-clojure.compilation/legacy-hinting) 0)))
(defprotocol HintedProtocol
(hintedp ^String [a]
^Integer [a b]))
(deftest hinted-protocol-arg-vector
(should-not-reflect #(.substring (clojure.test-clojure.compilation/hintedp "") 0))
(should-not-reflect #(.floatValue (clojure.test-clojure.compilation/hintedp :a :b))))
(defn primfn
(^long [])
(^double [a]))
(deftest primitive-return-decl
(should-not-reflect #(loop [k 5] (recur (clojure.test-clojure.compilation/primfn))))
(should-not-reflect #(loop [k 5.0] (recur (clojure.test-clojure.compilation/primfn 0))))
(should-print-err-message #"(?s).*k is not matching primitive.*"
#(loop [k (clojure.test-clojure.compilation/primfn)] (recur :foo))))
#_(deftest CLJ-1154-use-out-after-compile
;; This test creates a dummy file to compile, sets up a dummy
;; compiled output directory, and a dummy output stream, and
;; verifies the stream is still usable after compiling.
(spit "test/dummy.clj" "(ns dummy)")
(try
(let [compile-path (System/getProperty "clojure.compile.path")
tmp (java.io.File. "tmp")
new-out (java.io.OutputStreamWriter. (java.io.ByteArrayOutputStream.))]
(binding [clojure.core/*out* new-out]
(try
(.mkdir tmp)
(System/setProperty "clojure.compile.path" "tmp")
(clojure.lang.Compile/main (into-array ["dummy"]))
(println "this should still work without throwing an exception" )
(finally
(if compile-path
(System/setProperty "clojure.compile.path" compile-path)
(System/clearProperty "clojure.compile.path"))
(doseq [f (.listFiles tmp)]
(.delete f))
(.delete tmp)))))
(finally
(doseq [f (.listFiles (java.io.File. "test"))
:when (re-find #"dummy.clj" (str f))]
(.delete f)))))
(deftest CLJ-1184-do-in-non-list-test
(testing "do in a vector throws an exception"
(is (thrown? Compiler$CompilerException
(eval '[do 1 2 3]))))
(testing "do in a set throws an exception"
(is (thrown? Compiler$CompilerException
(eval '#{do}))))
;; compile uses a separate code path so we have to call it directly
;; to test it
(letfn [(compile [s]
(spit "test/clojure/bad_def_test.clj" (str "(ns clojure.bad-def-test)\n" s))
(try
(binding [*compile-path* "test"]
(clojure.core/compile 'clojure.bad-def-test))
(finally
(doseq [f (.listFiles (java.io.File. "test/clojure"))
:when (re-find #"bad_def_test" (str f))]
(.delete f)))))]
(testing "do in a vector throws an exception in compilation"
(is (thrown? Compiler$CompilerException (compile "[do 1 2 3]"))))
(testing "do in a set throws an exception in compilation"
(is (thrown? Compiler$CompilerException (compile "#{do}"))))))
(defn gen-name []
;; Not all names can be correctly demunged. Skip names that contain
;; a munge word as they will not properly demunge.
(let [munge-words (remove clojure.string/blank?
(conj (map #(clojure.string/replace % "_" "")
(vals Compiler/CHAR_MAP)) "_"))]
(first (filter (fn [n] (not-any? #(>= (.indexOf n %) 0) munge-words))
(repeatedly #(name (gen/symbol (constantly 10))))))))
(defn munge-roundtrip [n]
(Compiler/demunge (Compiler/munge n)))
(defspec test-munge-roundtrip
munge-roundtrip
[^{:tag clojure.test-clojure.compilation/gen-name} n]
(assert (= n %)))
(deftest test-fnexpr-type-hint
(testing "CLJ-1378: FnExpr should be allowed to override its reported class with a type hint."
(is (thrown? Compiler$CompilerException
(load-string "(.submit (java.util.concurrent.Executors/newCachedThreadPool) #())")))
(is (try (load-string "(.submit (java.util.concurrent.Executors/newCachedThreadPool) ^Runnable #())")
(catch Compiler$CompilerException e nil)))))
(defn ^{:tag 'long} hinted-primfn [^long x] x)
(defn unhinted-primfn [^long x] x)
(deftest CLJ-1533-primitive-functions-lose-tag
(should-not-reflect #(Math/abs (clojure.test-clojure.compilation/hinted-primfn 1)))
(should-not-reflect #(Math/abs ^long (clojure.test-clojure.compilation/unhinted-primfn 1))))
(defrecord Y [a])
#clojure.test_clojure.compilation.Y[1]
(defrecord Y [b])
(binding [*compile-path* "target/test-classes"]
(compile 'clojure.test-clojure.compilation.examples))
(deftest test-compiler-line-numbers
(let [fails-on-line-number? (fn [expected function]
(try
(function)
nil
(catch Throwable t
(let [frames (filter #(= "line_number_examples.clj" (.getFileName %))
(.getStackTrace t))
_ (if (zero? (count frames))
(.printStackTrace t)
)
actual (.getLineNumber ^StackTraceElement (first frames))]
(= expected actual)))))]
(is (fails-on-line-number? 13 line/instance-field))
(is (fails-on-line-number? 19 line/instance-field-reflected))
(is (fails-on-line-number? 25 line/instance-field-unboxed))
(is (fails-on-line-number? 32 line/instance-field-assign))
(is (fails-on-line-number? 40 line/instance-field-assign-reflected))
(is (fails-on-line-number? 47 line/static-field-assign))
(is (fails-on-line-number? 54 line/instance-method))
(is (fails-on-line-number? 61 line/instance-method-reflected))
(is (fails-on-line-number? 68 line/instance-method-unboxed))
(is (fails-on-line-number? 74 line/static-method))
(is (fails-on-line-number? 80 line/static-method-reflected))
(is (fails-on-line-number? 86 line/static-method-unboxed))
(is (fails-on-line-number? 92 line/invoke))
(is (fails-on-line-number? 101 line/threading))
(is (fails-on-line-number? 112 line/keyword-invoke))
(is (fails-on-line-number? 119 line/invoke-cast))))
(deftest CLJ-979
(is (= clojure.test_clojure.compilation.examples.X
(class (clojure.test-clojure.compilation.examples/->X))))
(is (.b (clojure.test_clojure.compilation.Y. 1)))
(is (= clojure.test_clojure.compilation.examples.T
(class (clojure.test_clojure.compilation.examples.T.))
(class (clojure.test-clojure.compilation.examples/->T)))))
(deftest clj-1208
;; clojure.test-clojure.compilation.load-ns has not been loaded
;; so this would fail if the deftype didn't load it in its static
;; initializer as the implementation of f requires a var from
;; that namespace
(is (= 1 (.f (clojure.test_clojure.compilation.load_ns.x.)))))
(deftest clj-1568
(let [compiler-fails-at?
(fn [row col source]
(let [path (name (gensym "clj-1568.example-"))]
(try
(Compiler/load (java.io.StringReader. source) path "clj-1568.example")
nil
(catch Compiler$CompilerException e
(let [data (ex-data e)]
(= [path row col]
[(:clojure.error/source data) (:clojure.error/line data) (:clojure.error/column data)]))))))]
(testing "with error in the initial form"
(are [row col source] (compiler-fails-at? row col source)
;; note that the spacing of the following string is important
1 4 " (.foo nil)"
2 18 "
(/ 1 0)"))
(testing "with error in an non-initial form"
(are [row col source] (compiler-fails-at? row col source)
;; note that the spacing of the following string is important
3 18 "(:foo {})
(.foo nil)"
4 20 "(ns clj-1568.example)
(/ 1 0)"))))
(deftype CLJ1399 [munged-field-name])
(deftest clj-1399
;; throws an exception on failure
(is (eval `(fn [] ~(CLJ1399. 1)))))
(deftest CLJ-1250-this-clearing
(testing "clearing during try/catch/finally"
(let [closed-over-in-catch (let [x :foo]
(fn []
(try
(throw (Exception. "boom"))
(catch Exception e
x)))) ;; x should remain accessible to the fn
a (atom nil)
closed-over-in-finally (fn []
(try
:ret
(finally
(reset! a :run))))]
(is (= :foo (closed-over-in-catch)))
(is (= :ret (closed-over-in-finally)))
(is (= :run @a))))
(testing "no clearing when loop not in return context"
(let [x (atom 5)
bad (fn []
(loop [] (System/getProperties))
(swap! x dec)
(when (pos? @x)
(recur)))]
(is (nil? (bad))))))
(deftest CLJ-1586-lazyseq-literals-preserve-metadata
(should-not-reflect (eval (list '.substring (with-meta (concat '(identity) '("foo")) {:tag 'String}) 0))))
(deftest CLJ-1456-compiler-error-on-incorrect-number-of-parameters-to-throw
(is (thrown? RuntimeException (eval '(defn foo [] (throw)))))
(is (thrown? RuntimeException (eval '(defn foo [] (throw RuntimeException any-symbol)))))
(is (thrown? RuntimeException (eval '(defn foo [] (throw (RuntimeException.) any-symbol)))))
(is (var? (eval '(defn foo [] (throw (IllegalArgumentException.)))))))
(deftest clj-1809
(is (eval `(fn [y#]
(try
(finally
(let [z# y#])))))))
;; See CLJ-1846
(deftest incorrect-primitive-type-hint-throws
;; invalid primitive type hint
(is (thrown-with-cause-msg? Compiler$CompilerException #"Cannot coerce long to int"
(load-string "(defn returns-long ^long [] 1) (Integer/bitCount ^int (returns-long))")))
;; correct casting instead
(is (= 1 (load-string "(defn returns-long ^long [] 1) (Integer/bitCount (int (returns-long)))"))))
;; See CLJ-1825
(def zf (fn rf [x] (lazy-seq (cons x (rf x)))))
(deftest test-anon-recursive-fn
(is (= [0 0] (take 2 ((fn rf [x] (lazy-seq (cons x (rf x)))) 0))))
(is (= [0 0] (take 2 (zf 0)))))
;; See CLJ-1845
(deftest direct-linking-for-load
(let [called? (atom nil)
logger (fn [& args]
(reset! called? true)
nil)]
(with-redefs [load logger]
;; doesn't actually load clojure.repl, but should
;; eventually call `load` and reset called?.
(require 'clojure.repl :reload))
(is @called?)))
(deftest clj-1714
(testing "CLJ-1714 Classes shouldn't have their static initialisers called simply by type hinting or importing"
;; ClassWithFailingStaticInitialiser will throw if its static initialiser is called
(is (eval '(fn [^compilation.ClassWithFailingStaticInitialiser c])))
(is (eval '(import (compilation ClassWithFailingStaticInitialiser))))))
(deftest CLJ-2284
(testing "CLJ-2284 Can call static methods on interfaces"
(is (= 42 (compilation.JDK8InterfaceMethods/staticMethod0 42)))
(is (= "test" (compilation.JDK8InterfaceMethods/staticMethod1 "test")))
(is (= 1 (if (compilation.JDK8InterfaceMethods/staticMethod2 true) 1 2)))))
(deftest CLJ-2580
(testing "CLJ-2580 Correctly calculate exit branches of case"
(is (zero? (let [d (case nil :x nil 0)] d)))
(is (nil? (let [d (case nil :x 0 nil)] d)))))
(deftest CLJ-2945
(testing "CLJ-2945 Reify form and obj meta"
(are [m x] (= (set (keys m)) x)
(meta (reify clojure.lang.ILookup)) #{}
(meta ^:foo (reify clojure.lang.ILookup)) #{:foo}
(meta (macroexpand-1 '(reify clojure.lang.ILookup))) #{:line :column}
(meta (macroexpand-1 '^:foo (reify clojure.lang.ILookup))) #{:line :column :foo})))