فهرست منبع

use EObjectDecl positions for display (closes #5712)

Simon Krajewski 9 سال پیش
والد
کامیت
bbaea6fb6f
3فایلهای تغییر یافته به همراه36 افزوده شده و 7 حذف شده
  1. 5 4
      src/syntax/parser.ml
  2. 10 3
      src/typing/typer.ml
  3. 21 0
      tests/display/src/cases/Issue5712.hx

+ 5 - 4
src/syntax/parser.ml

@@ -994,7 +994,8 @@ and parse_complex_type_next (t : type_hint) = parser
 and parse_type_anonymous opt = parser
 	| [< '(Question,_) when not opt; s >] -> parse_type_anonymous true s
 	| [< name, p1 = ident; t = parse_type_hint_with_pos; s >] ->
-		let next p2 acc =
+		let p2 = pos (last_token s) in
+		let next acc =
 			{
 				cff_name = name,p1;
 				cff_meta = if opt then [Meta.Optional,[],null_pos] else [];
@@ -1005,11 +1006,11 @@ and parse_type_anonymous opt = parser
 			} :: acc
 		in
 		match s with parser
-		| [< '(BrClose,p2) >] -> next p2 [],p2
+		| [< '(BrClose,p2) >] -> next [],p2
 		| [< '(Comma,p2) >] ->
 			(match s with parser
-			| [< '(BrClose,p2) >] -> next p2 [],p2
-			| [< l,p2 = parse_type_anonymous false >] -> next p2 l,punion p1 p2
+			| [< '(BrClose,p2) >] -> next [],p2
+			| [< l,p2 = parse_type_anonymous false >] -> next l,punion p1 p2
 			| [< >] -> serror());
 		| [< >] -> serror()
 

+ 10 - 3
src/typing/typer.ml

@@ -2910,7 +2910,13 @@ and type_object_decl ctx fl with_type p =
 			let n,is_quoted,is_valid = Parser.unquote_ident n in
 			if PMap.mem n !fields then error ("Duplicate field in object declaration : " ^ n) p;
 			let e = try
-				let t = (match !dynamic_parameter with Some t -> t | None -> (PMap.find n field_map).cf_type) in
+				let t = match !dynamic_parameter with
+					| Some t -> t
+					| None ->
+						let cf = PMap.find n field_map in
+						if ctx.in_display && Display.is_display_position pn then Display.DisplayEmitter.display_field ctx.com.display cf pn;
+						cf.cf_type
+				in
 				let e = type_expr ctx e (WithType t) in
 				let e = AbstractCast.cast_or_unify ctx t e p in
 				(try type_eq EqStrict e.etype t; e with Unify_error _ -> mk (TCast (e,None)) t e.epos)
@@ -2921,7 +2927,7 @@ and type_object_decl ctx fl with_type p =
 			in
 			if is_valid then begin
 				if String.length n > 0 && n.[0] = '$' then error "Field names starting with a dollar are not allowed" p;
-				let cf = mk_field n e.etype e.epos null_pos in (* TODO: use field pos if we have it *)
+				let cf = mk_field n e.etype (punion pn e.epos) pn in
 				fields := PMap.add n cf !fields;
 			end;
 			let e = if is_quoted then wrap_quoted_meta e else e in
@@ -2946,7 +2952,8 @@ and type_object_decl ctx fl with_type p =
 			if PMap.mem f acc then error ("Duplicate field in object declaration : " ^ f) p;
 			let e = type_expr ctx e Value in
 			(match follow e.etype with TAbstract({a_path=[],"Void"},_) -> error "Fields of type Void are not allowed in structures" e.epos | _ -> ());
-			let cf = mk_field f e.etype e.epos null_pos in
+			let cf = mk_field f e.etype (punion pf e.epos) pf in
+			if ctx.in_display && Display.is_display_position pf then Display.DisplayEmitter.display_field ctx.com.display cf pf;
 			let e = if is_quoted then wrap_quoted_meta e else e in
 			((f,e) :: l, if is_valid then begin
 				if String.length f > 0 && f.[0] = '$' then error "Field names starting with a dollar are not allowed" p;

+ 21 - 0
tests/display/src/cases/Issue5712.hx

@@ -0,0 +1,21 @@
+package cases;
+
+class Issue5712 extends DisplayTestCase {
+	/**
+	typedef Struct = {
+		{-1-}field:Float{-2-}
+	}
+	class Main {
+		public static function main() {
+			var s:Struct = { fi{-3-}eld: 0 };
+			s.fi{-4-}eld;
+		}
+	}
+	**/
+	function testType1() {
+		eq(range(1, 2), position(pos(3)));
+		eq(range(1, 2), position(pos(4)));
+		eq("Float", type(pos(3))); // not sure about this one, maybe it should be Int...
+		eq("Float", type(pos(4)));
+	}
+}