Browse Source

resolve unqualified identifiers to @:enum abstract fields (closes #2939)

Simon Krajewski 11 years ago
parent
commit
f9ff593b31
2 changed files with 32 additions and 6 deletions
  1. 17 0
      tests/unit/issues/Issue2939.hx
  2. 15 6
      typer.ml

+ 17 - 0
tests/unit/issues/Issue2939.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+@:enum
+private abstract A(Int) to Int {
+	var a = 1;
+	var b = 2;
+
+	static var c = 3;
+}
+
+class Issue2939 extends Test {
+	function test() {
+		eq(1, a);
+		eq(2, b);
+		t(unit.TestType.typeError(c));
+	}
+}

+ 15 - 6
typer.ml

@@ -1096,12 +1096,25 @@ let rec type_ident_raise ?(imported_enums=true) ctx i p mode =
 		field_access ctx mode f (FStatic (ctx.curclass,f)) (field_type ctx ctx.curclass [] f p) e p
 	with Not_found -> try
 		if not imported_enums then raise Not_found;
+		let wrap e = if mode = MSet then
+				AKNo i
+			else
+				AKExpr e
+		in
 		(* lookup imported enums *)
 		let rec loop l =
 			match l with
 			| [] -> raise Not_found
 			| t :: l ->
 				match t with
+ 				| TAbstractDecl ({a_impl = Some c} as a) when Meta.has Meta.Enum a.a_meta ->
+					let cf = PMap.find i c.cl_statics in
+					if not (Meta.has Meta.Enum cf.cf_meta) then
+						loop l
+					else begin
+						let et = type_module_type ctx (TClassDecl c) None p in
+						AKInline(et,cf,FStatic(c,cf),monomorphs cf.cf_params cf.cf_type)
+					end
 				| TClassDecl _ | TAbstractDecl _ ->
 					loop l
 				| TTypeDecl t ->
@@ -1114,15 +1127,11 @@ let rec type_ident_raise ?(imported_enums=true) ctx i p mode =
 						let et = type_module_type ctx t None p in
 						let monos = List.map (fun _ -> mk_mono()) e.e_types in
 						let monos2 = List.map (fun _ -> mk_mono()) ef.ef_params in
-						mk (TField (et,FEnum (e,ef))) (enum_field_type ctx e ef monos monos2 p) p
+						wrap (mk (TField (et,FEnum (e,ef))) (enum_field_type ctx e ef monos monos2 p) p)
 					with
 						Not_found -> loop l
 		in
-		let e = (try loop (List.rev ctx.m.curmod.m_types) with Not_found -> loop ctx.m.module_types) in
-		if mode = MSet then
-			AKNo i
-		else
-			AKExpr e
+		(try loop (List.rev ctx.m.curmod.m_types) with Not_found -> loop ctx.m.module_types)
 	with Not_found ->
 		(* lookup imported globals *)
 		let t, name = PMap.find i ctx.m.module_globals in