Forráskód Böngészése

don't run the analyzer if we have `this` in a non-static var init

closes #6030
closes #6295
closes #6314
Simon Krajewski 8 éve
szülő
commit
36edba4da8

+ 18 - 13
src/typing/typeload.ml

@@ -2225,20 +2225,25 @@ module ClassInitializer = struct
 					| Var v when not fctx.is_static ->
 						let e = if ctx.com.display.dms_display && ctx.com.display.dms_error_policy <> EPCollect then
 							e
-						else match Optimizer.make_constant_expression ctx (maybe_run_analyzer e) with
-							| Some e -> e
-							| None ->
-								let rec has_this e = match e.eexpr with
-									| TConst TThis ->
-										display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
-									| TLocal v when (match ctx.vthis with Some v2 -> v == v2 | None -> false) ->
-										display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
-									| _ ->
-									Type.iter has_this e
-								in
-								has_this e;
+						else begin
+							let rec check_this e = match e.eexpr with
+								| TConst TThis ->
+									display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
+									raise Exit
+								| TLocal v when (match ctx.vthis with Some v2 -> v == v2 | None -> false) ->
+									display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
+									raise Exit
+								| _ ->
+								Type.iter check_this e
+							in
+							try
+								check_this e;
+								match Optimizer.make_constant_expression ctx (maybe_run_analyzer e) with
+								| Some e -> e
+								| None -> e
+							with Exit ->
 								e
-						in
+						end in
 						e
 					| Var v when v.v_read = AccInline ->
 						let e = require_constant_expression e "Inline variable initialization must be a constant value" in

+ 13 - 0
tests/misc/projects/Issue6030/Main1.hx

@@ -0,0 +1,13 @@
+class Main1 {
+    public var ok(default,null):Void->Void;
+    public var stillOk(default,never) = function(){return function(){trace("This works.");};}();
+    public var notOk(default,never) = function(){return function(){trace("This does not work. "+this);};}();
+
+    public function new(){
+        ok = function(){return function(){trace("This works. "+this);};}();
+    }
+
+    static function main() {
+        new Main1();
+    }
+}

+ 3 - 0
tests/misc/projects/Issue6030/compile-fail.hxml

@@ -0,0 +1,3 @@
+-main Main1
+--no-output
+-js js.js

+ 1 - 0
tests/misc/projects/Issue6030/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main1.hx:4: characters 96-100 : Cannot access this or other member field in variable initialization