Skip to content

Commit

Permalink
don't run the analyzer if we have this in a non-static var init
Browse files Browse the repository at this point in the history
closes #6030
closes #6295
closes #6314
  • Loading branch information
Simn committed Jun 2, 2017
1 parent 79c2b8f commit 36edba4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/typing/typeload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/misc/projects/Issue6030/Main1.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions tests/misc/projects/Issue6030/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-main Main1
--no-output
-js js.js
1 change: 1 addition & 0 deletions tests/misc/projects/Issue6030/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main1.hx:4: characters 96-100 : Cannot access this or other member field in variable initialization

0 comments on commit 36edba4

Please sign in to comment.