浏览代码

make sure member variables do not access the vthis variable (closes #3783)

Simon Krajewski 10 年之前
父节点
当前提交
52d1ec7c1e

+ 11 - 0
tests/misc/projects/Issue3783/Main.hx

@@ -0,0 +1,11 @@
+class Main {
+	var field = function () {
+		method();
+	}
+
+	function method () {
+	}
+
+	private static function main () {
+	}
+}

+ 2 - 0
tests/misc/projects/Issue3783/compile-fail.hxml

@@ -0,0 +1,2 @@
+-main Main
+--interp

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

@@ -0,0 +1 @@
+Main.hx:3: characters 2-8 : Cannot access this or other member field in variable initialization

+ 3 - 0
type.ml

@@ -2056,3 +2056,6 @@ let map_expr_type f ft fv e =
 		{ e with eexpr = TCast (f e1,t); etype = ft e.etype }
 		{ e with eexpr = TCast (f e1,t); etype = ft e.etype }
 	| TMeta (m,e1) ->
 	| TMeta (m,e1) ->
 		{e with eexpr = TMeta(m, f e1); etype = ft e.etype }
 		{e with eexpr = TMeta(m, f e1); etype = ft e.etype }
+
+let print_if b e =
+	if b then print_endline (s_expr_pretty "" (s_type (print_context())) e)

+ 2 - 0
typeload.ml

@@ -1828,6 +1828,8 @@ let init_class ctx c p context_init herits fields =
 								let rec has_this e = match e.eexpr with
 								let rec has_this e = match e.eexpr with
 									| TConst TThis ->
 									| TConst TThis ->
 										display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
 										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
 									Type.iter has_this e
 								in
 								in