Bladeren bron

[syntax] keep position of EFunction name

closes #7148
Simon Krajewski 7 jaren geleden
bovenliggende
commit
e127b0a24f

+ 1 - 1
src/context/display/documentSymbols.ml

@@ -22,7 +22,7 @@ let collect_module_symbols with_locals (pack,decls) =
 				add s Variable p;
 				expr parent e
 			) catches;
-		| EFunction(Some s,f) ->
+		| EFunction(Some (s,_),f) ->
 			add s Function p;
 			func parent f
 		| EBinop(OpIn,(EConst(Ident s),p),e2) ->

+ 2 - 2
src/core/ast.ml

@@ -194,7 +194,7 @@ and expr_def =
 	| ENew of placed_type_path * expr list
 	| EUnop of unop * unop_flag * expr
 	| EVars of (placed_name * type_hint option * expr option) list
-	| EFunction of string option * func
+	| EFunction of placed_name option * func
 	| EBlock of expr list
 	| EFor of expr * expr
 	| EIf of expr * expr * expr option
@@ -734,7 +734,7 @@ let s_expr e =
 		| ENew (t,el) -> "new " ^ s_complex_type_path tabs t ^ "(" ^ s_expr_list tabs el ", " ^ ")"
 		| EUnop (op,Postfix,e) -> s_expr_inner tabs e ^ s_unop op
 		| EUnop (op,Prefix,e) -> s_unop op ^ s_expr_inner tabs e
-		| EFunction (Some n,f) -> "function " ^ n ^ s_func tabs f
+		| EFunction (Some (n,_),f) -> "function " ^ n ^ s_func tabs f
 		| EFunction (None,f) -> "function" ^ s_func tabs f
 		| EVars vl -> "var " ^ String.concat ", " (List.map (s_var tabs) vl)
 		| EBlock [] -> "{ }"

+ 2 - 2
src/macro/macroApi.ml

@@ -521,7 +521,7 @@ and encode_expr e =
 					]
 				) vl)]
 			| EFunction (name,f) ->
-				11, [null encode_string name; encode_fun f]
+				11, [null encode_placed_name name; encode_fun f]
 			| EBlock el ->
 				12, [encode_array (List.map loop el)]
 			| EFor (e,eloop) ->
@@ -818,7 +818,7 @@ and decode_expr v =
 				((decode_placed_name (field v "name_pos") (field v "name")),opt decode_ctype (field v "type"),opt loop (field v "expr"))
 			) (decode_array vl))
 		| 11, [fname;f] ->
-			EFunction (opt decode_string fname,decode_fun f)
+			EFunction (opt (fun v -> decode_string v,Globals.null_pos) fname,decode_fun f)
 		| 12, [el] ->
 			EBlock (List.map loop (decode_array el))
 		| 13, [e1;e2] ->

+ 1 - 1
src/optimization/optimizer.ml

@@ -1304,7 +1304,7 @@ let optimize_completion_expr e args =
 		| EFunction (v,f) ->
 			(match v with
 			| None -> ()
-			| Some name ->
+			| Some (name,_) ->
 				decl name None (Some e));
 			let old = save() in
 			List.iter (fun ((n,_),_,_,t,e) -> decl n (Option.map fst t) e) f.f_args;

+ 1 - 1
src/syntax/grammar.mly

@@ -1022,7 +1022,7 @@ and parse_function p1 inl = parser
 				f_args = al;
 				f_expr = Some e;
 			} in
-			EFunction ((match name with None -> None | Some (name,_) -> Some (if inl then "inline_" ^ name else name)),f), punion p1 (pos e)
+			EFunction ((match name with None -> None | Some (name,pn) -> Some ((if inl then "inline_" ^ name else name),pn)),f), punion p1 (pos e)
 		in
 		(try
 			expr_next (make (secure_expr s)) s

+ 3 - 3
src/syntax/reification.ml

@@ -284,15 +284,15 @@ let reify in_macro =
 			let name = match name with
 				| None ->
 					to_null null_pos
-				| Some name ->
+				| Some (name,pn) ->
 					if ExtString.String.starts_with name "inline_$" then begin
 						let real_name = (String.sub name 7 (String.length name - 7)) in
-						let e_name = to_string real_name p in
+						let e_name = to_string real_name pn in
 						let e_inline = to_string "inline_" p in
 						let e_add = (EBinop(OpAdd,e_inline,e_name),p) in
 						e_add
 					end else
-						to_string name p
+						to_string name pn
 			in
 			expr "EFunction" [name; to_fun f p]
 		| EBlock el ->

+ 6 - 6
src/typing/typer.ml

@@ -1976,16 +1976,16 @@ and type_map_declaration ctx e1 el with_type p =
 	mk (TBlock el) tmap p
 
 and type_local_function ctx name f with_type p =
-	let params = TypeloadFunction.type_function_params ctx f (match name with None -> "localfun" | Some n -> n) p in
+	let params = TypeloadFunction.type_function_params ctx f (match name with None -> "localfun" | Some (n,_) -> n) p in
 	if params <> [] then begin
 		if name = None then display_error ctx "Type parameters not supported in unnamed local functions" p;
 		if with_type <> NoValue then error "Type parameters are not supported for rvalue functions" p
 	end;
 	List.iter (fun tp -> if tp.tp_constraints <> None then display_error ctx "Type parameter constraints are not supported for local functions" p) f.f_params;
-	let inline, v = (match name with
-		| None -> false, None
-		| Some v when ExtString.String.starts_with v "inline_" -> true, Some (String.sub v 7 (String.length v - 7))
-		| Some v -> false, Some v
+	let inline,v,pname = (match name with
+		| None -> false,None,p
+		| Some (v,pn) when ExtString.String.starts_with v "inline_" -> true,Some (String.sub v 7 (String.length v - 7)),pn
+		| Some (v,pn) -> false,Some v,pn
 	) in
 	let old_tp,old_in_loop = ctx.type_params,ctx.in_loop in
 	ctx.type_params <- params @ ctx.type_params;
@@ -2026,7 +2026,7 @@ and type_local_function ctx name f with_type p =
 		| None -> None
 		| Some v ->
 			if starts_with v '$' then display_error ctx "Variable names starting with a dollar are not allowed" p;
-			let v = (add_local_with_origin ctx v ft p (TVarOrigin.TVOLocalFunction)) (* TODO: var pos *) in
+			let v = (add_local_with_origin ctx v ft pname (TVarOrigin.TVOLocalFunction)) in
 			if params <> [] then v.v_extra <- Some (params,None);
 			Some v
 	) in

+ 19 - 0
tests/display/src/cases/Issue7148.hx

@@ -0,0 +1,19 @@
+package cases;
+
+class Issue7148 extends DisplayTestCase {
+	/**
+	class Main {
+		static function main() {
+			function {-1-}local{-2-}() {}
+			lo{-3-}cal;
+
+			inline function {-4-}local{-5-}() {}
+			lo{-6-}cal;
+		}
+	}
+	**/
+	function test() {
+		eq(range(1, 2), position(pos(3)));
+		eq(range(4, 5), position(pos(6)));
+	}
+}