Browse Source

[display] deal with positions in `import as`

closes #7020
Simon Krajewski 7 years ago
parent
commit
d1c58f4046

+ 1 - 1
src/core/ast.ml

@@ -292,7 +292,7 @@ type ('a,'b) definition = {
 
 type import_mode =
 	| INormal
-	| IAsName of string
+	| IAsName of placed_name
 	| IAll
 
 type import = placed_name list * import_mode

+ 2 - 2
src/macro/macroApi.ml

@@ -358,7 +358,7 @@ let encode_unop op =
 let encode_import (path,mode) =
 	let tag,pl = match mode with
 		| INormal -> 0, []
-		| IAsName s -> 1, [encode_string s]
+		| IAsName(s,_) -> 1, [encode_string s]
 		| IAll -> 2,[]
 	in
 	let mode = encode_enum IImportMode tag pl in
@@ -644,7 +644,7 @@ let decode_unop op =
 let decode_import_mode t =
 	match decode_enum t with
 	| 0, [] -> INormal
-	| 1, [alias] -> IAsName (decode_string alias)
+	| 1, [alias] -> IAsName (decode_string alias,Globals.null_pos) (* TODO: is it okay to lose the pos here? *)
 	| 2, [] -> IAll
 	| _ -> raise Invalid_expr
 

+ 4 - 4
src/syntax/grammar.mly

@@ -205,10 +205,10 @@ and parse_import s p1 =
 				serror());
 		| [< '(Semicolon,p2) >] ->
 			p2, List.rev acc, INormal
-		| [< '(Kwd In,_); '(Const (Ident name),_); '(Semicolon,p2) >] ->
-			p2, List.rev acc, IAsName name
-		| [< '(Const (Ident "as"),_); '(Const (Ident name),_); '(Semicolon,p2) >] ->
-			p2, List.rev acc, IAsName name
+		| [< '(Kwd In,_); '(Const (Ident name),pname); '(Semicolon,p2) >] ->
+			p2, List.rev acc, IAsName(name,pname)
+		| [< '(Const (Ident "as"),_); '(Const (Ident name),pname); '(Semicolon,p2) >] ->
+			p2, List.rev acc, IAsName(name,pname)
 		| [< >] ->
 			serror()
 	in

+ 11 - 8
src/typing/typeloadModule.ml

@@ -375,25 +375,28 @@ let init_module_type ctx context_init do_init (decl,p) =
 				chk_private t p_type;
 				t
 			in
-			let rebind t name =
+			let rebind t name p =
 				if not (name.[0] >= 'A' && name.[0] <= 'Z') then
 					error "Type aliases must start with an uppercase letter" p;
 				let _, _, f = ctx.g.do_build_instance ctx t p_type in
 				(* create a temp private typedef, does not register it in module *)
-				TTypeDecl {
+				let mt = TTypeDecl {
 					t_path = (fst md.m_path @ ["_" ^ snd md.m_path],name);
 					t_module = md;
 					t_pos = p;
-					t_name_pos = null_pos;
+					t_name_pos = p;
 					t_private = true;
 					t_doc = None;
 					t_meta = [];
 					t_params = (t_infos t).mt_params;
 					t_type = f (List.map snd (t_infos t).mt_params);
-				}
+				} in
+				if ctx.is_display_file && Display.is_display_position p then
+					Display.DisplayEmitter.display_module_type ctx.com.display mt p;
+				mt
 			in
 			let add_static_init t name s =
-				let name = (match name with None -> s | Some n -> n) in
+				let name = (match name with None -> s | Some (n,_) -> n) in
 				match resolve_typedef t with
 				| TClassDecl c ->
 					ignore(c.cl_build());
@@ -413,14 +416,14 @@ let init_module_type ctx context_init do_init (decl,p) =
 					(match name with
 					| None ->
 						ctx.m.module_types <- List.filter no_private (List.map (fun t -> t,p) types) @ ctx.m.module_types
-					| Some newname ->
-						ctx.m.module_types <- (rebind (get_type tname) newname,p) :: ctx.m.module_types);
+					| Some(newname,pname) ->
+						ctx.m.module_types <- (rebind (get_type tname) newname pname,p) :: ctx.m.module_types);
 				| [tsub,p2] ->
 					let pu = punion p1 p2 in
 					(try
 						let tsub = List.find (has_name tsub) types in
 						chk_private tsub pu;
-						ctx.m.module_types <- ((match name with None -> tsub | Some n -> rebind tsub n),p) :: ctx.m.module_types
+						ctx.m.module_types <- ((match name with None -> tsub | Some(n,pname) -> rebind tsub n pname),p) :: ctx.m.module_types
 					with Not_found ->
 						(* this might be a static property, wait later to check *)
 						let tmain = get_type tname in

+ 18 - 0
tests/display/src/cases/Issue7020.hx

@@ -0,0 +1,18 @@
+package cases;
+
+class Issue7020 extends DisplayTestCase {
+	/**
+	import String as {-2-}ExprAc{-4-}cess{-3-};
+
+	class Main {
+		public static function main() {
+			var access:ExprA{-1-}ccess;
+		}
+	}
+	**/
+	function test() {
+		eq(range(2, 3), position(pos(1)));
+		eq(range(2, 3), position(pos(4)));
+		eq("_String.ExprAccess", type(pos(4)));
+	}
+}