Fork me on GitHub
#cljs-dev
<
2019-07-10
>
thheller10:07:08

@anmonteiro sounds like a good idea to me. I reported this a few months ago and yours seems like a good solution. https://clojure.atlassian.net/browse/CLJS-3055

aisamu14:07:18

Hi! Is this an instance of garbage-in/garbage-out or is it a small regression?

(ns cljsbug.wat.fig)

(def a (rand-nth [nil]))
(when a (* a 2))
// Compiled by ClojureScript 1.10.339 {}
goog.provide('cljsbug.wat.fig');
goog.require('cljs.core');
cljsbug.wat.fig.a = cljs.core.rand_nth.call(null,new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [null], null));
if(cljs.core.truth_(cljsbug.wat.fig.a)){
(cljsbug.wat.fig.a * (2));
} else {
}
// Compiled by ClojureScript 1.10.520 {}
goog.provide('cljsbug.wat.fig');
goog.require('cljs.core');
cljsbug.wat.fig.a = cljs.core.rand_nth.call(null,new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [null], null));
if(cljs.core.truth_(cljsbug.wat.fig.a)){
(
The file is truncated, and there's the following NPE on the terminal:
java.lang.NullPointerException
        clojure.core$namespace/invokeStatic at core.clj(1595)
              clojure.core$namespace/invoke at core.clj(1589)
        cljs.compiler$emit_var/invokeStatic at compiler.cljc(448)
              cljs.compiler$emit_var/invoke at compiler.cljc(442)
        cljs.compiler$fn__3341/invokeStatic at compiler.cljc(489)
              cljs.compiler$fn__3341/invoke at compiler.cljc(489)
                clojure.lang.MultiFn/invoke at MultiFn.java(229)
            cljs.compiler$emit/invokeStatic at compiler.cljc(202)
                  cljs.compiler$emit/invoke at compiler.cljc(182)
           cljs.compiler$emits/invokeStatic at compiler.cljc(209)
                 cljs.compiler$emits/invoke at compiler.cljc(204)
           cljs.compiler$emits/invokeStatic at compiler.cljc(229)
               cljs.compiler$emits/doInvoke at compiler.cljc(204)
                clojure.lang.RestFn/applyTo at RestFn.java(157)
            clojure.core$apply/invokeStatic at core.clj(657)
                  clojure.core$apply/invoke at core.clj(652)
           cljs.compiler$emits/invokeStatic at compiler.cljc(210)
                 cljs.compiler$emits/invoke at compiler.cljc(204)
        cljs.compiler$fn__3828/invokeStatic at compiler.cljc(1382)
              cljs.compiler$fn__3828/invoke at compiler.cljc(1374)
                clojure.lang.MultiFn/invoke at MultiFn.java(229)
            cljs.compiler$emit/invokeStatic at compiler.cljc(202)
                  cljs.compiler$emit/invoke at compiler.cljc(182)
        cljs.compiler$fn__3621/invokeStatic at compiler.cljc(1045)
              cljs.compiler$fn__3621/invoke at compiler.cljc(1040)
                clojure.lang.MultiFn/invoke at MultiFn.java(229)
            cljs.compiler$emit/invokeStatic at compiler.cljc(202)
                  cljs.compiler$emit/invoke at compiler.cljc(182)
           cljs.compiler$emits/invokeStatic at compiler.cljc(209)
                 cljs.compiler$emits/invoke at compiler.cljc(204)
Wrapping rand-nth with a call to Math.round (as in (js/Math.round (rand-nth [nil])) seems to "fix" it:
// Compiled by ClojureScript 1.10.520 {}
goog.provide('cljsbug.wat.fig');
goog.require('cljs.core');
cljsbug.wat.fig.a = Math.round(cljs.core.rand_nth.call(null,new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [null], null)));
if(cljs.core.truth_(cljsbug.wat.fig.a)){
(cljsbug.wat.fig.a * (2));
} else {
}

aisamu01:07:33

Confirmed that it works, thanks!