magic icon indicating copy to clipboard operation
magic copied to clipboard

Restrict lifted vars/keywords to functions that actually use them

Open nasser opened this issue 5 years ago • 0 comments

Right now the following

(defn xx [a]
  (fn []
    (inc a)))

compiles into

public class <magic>foo$<fn>__17 : AFunction
{
	internal static Var clojure_core$inc;
	internal object a;

	static <magic>foo$<fn>__17()
	{
		<magic>foo$<fn>__17.clojure_core$inc = RT.var("clojure.core", "inc");
	}

	public override object invoke()
	{
		return ((IFn)<magic>foo$<fn>__17.clojure_core$inc.getRawRoot()).invoke(this.a);
	}
}
public class <magic>foo$xx__18 : AFunction
{
	internal static Var clojure_core$inc; // <== should not be here

	static <magic>foo$xx__18()
	{
		<magic>foo$xx__18.clojure_core$inc = RT.var("clojure.core", "inc");
	}

	public override IFn invokeTyped(object a)
	{
		return new <magic>foo$<fn>__17
		{
			a = a
		};
	}

	public override object invoke(object a)
	{
		return this.invokeTyped(a);
	}
}

The var inc is lifted into the function xx despite the fact that it is only used in its nested function. Lifted keywords are similarly affected. Var and keyword collection should be more like closures.

nasser avatar Jul 07 '20 21:07 nasser