Browse Source

[typer] don't try counting in gen_local (#10648)

Also use store_typed_expr for array comprehension
Simon Krajewski 3 years ago
parent
commit
5e726809c7
3 changed files with 21 additions and 24 deletions
  1. 15 9
      src/context/typecore.ml
  2. 1 12
      src/typing/macroContext.ml
  3. 5 3
      src/typing/typer.ml

+ 15 - 9
src/context/typecore.ml

@@ -360,15 +360,7 @@ let add_local_with_origin ctx origin n t p =
 let gen_local_prefix = "`"
 let gen_local_prefix = "`"
 
 
 let gen_local ctx t p =
 let gen_local ctx t p =
-	(* ensure that our generated local does not mask an existing one *)
-	let rec loop n =
-		let nv = (if n = 0 then gen_local_prefix else gen_local_prefix ^ string_of_int n) in
-		if PMap.mem nv ctx.locals then
-			loop (n+1)
-		else
-			nv
-	in
-	add_local ctx VGenerated (loop 0) t p
+	add_local ctx VGenerated "`" t p
 
 
 let is_gen_local v =
 let is_gen_local v =
 	String.unsafe_get v.v_name 0 = String.unsafe_get gen_local_prefix 0
 	String.unsafe_get v.v_name 0 = String.unsafe_get gen_local_prefix 0
@@ -680,6 +672,20 @@ let is_empty_or_pos_infos args =
 	| [] -> true
 	| [] -> true
 	| _ -> false
 	| _ -> false
 
 
+let get_next_stored_typed_expr_id =
+	let uid = ref 0 in
+	(fun() -> incr uid; !uid)
+
+let get_stored_typed_expr com id =
+	let e = PMap.find id com.stored_typed_exprs in
+	Texpr.duplicate_tvars e
+
+let store_typed_expr com te p =
+	let id = get_next_stored_typed_expr_id() in
+	com.stored_typed_exprs <- PMap.add id te com.stored_typed_exprs;
+	let eid = (EConst (Int (string_of_int id, None))), p in
+	(EMeta ((Meta.StoredTypedExpr,[],p), eid)), p
+
 (* -------------- debug functions to activate when debugging typer passes ------------------------------- *)
 (* -------------- debug functions to activate when debugging typer passes ------------------------------- *)
 (*/*
 (*/*
 
 

+ 1 - 12
src/typing/macroContext.ml

@@ -55,14 +55,6 @@ let safe_decode ctx v expected t p f =
 		close_out ch;
 		close_out ch;
 		typing_error (Printf.sprintf "Expected %s but got %s (see %s.txt for details)" expected (Interp.value_string v) (String.concat "/" path)) p
 		typing_error (Printf.sprintf "Expected %s but got %s (see %s.txt for details)" expected (Interp.value_string v) (String.concat "/" path)) p
 
 
-let get_next_stored_typed_expr_id =
-	let uid = ref 0 in
-	(fun() -> incr uid; !uid)
-
-let get_stored_typed_expr com id =
-	let e = PMap.find id com.stored_typed_exprs in
-	Texpr.duplicate_tvars e
-
 let get_type_patch ctx t sub =
 let get_type_patch ctx t sub =
 	let new_patch() =
 	let new_patch() =
 		{ tp_type = None; tp_remove = false; tp_meta = [] }
 		{ tp_type = None; tp_remove = false; tp_meta = [] }
@@ -213,10 +205,7 @@ let make_macro_api ctx p =
 		);
 		);
 		MacroApi.store_typed_expr = (fun te ->
 		MacroApi.store_typed_expr = (fun te ->
 			let p = te.epos in
 			let p = te.epos in
-			let id = get_next_stored_typed_expr_id() in
-			ctx.com.stored_typed_exprs <- PMap.add id te ctx.com.stored_typed_exprs;
-			let eid = (EConst (Int (string_of_int id, None))), p in
-			(EMeta ((Meta.StoredTypedExpr,[],p), eid)), p
+			Typecore.store_typed_expr ctx.com te p
 		);
 		);
 		MacroApi.allow_package = (fun v -> Common.allow_package ctx.com v);
 		MacroApi.allow_package = (fun v -> Common.allow_package ctx.com v);
 		MacroApi.type_patch = (fun t f s v ->
 		MacroApi.type_patch = (fun t f s v ->

+ 5 - 3
src/typing/typer.ml

@@ -1392,6 +1392,8 @@ and type_array_decl ctx el with_type p =
 
 
 and type_array_comprehension ctx e with_type p =
 and type_array_comprehension ctx e with_type p =
 	let v = gen_local ctx (spawn_monomorph ctx p) p in
 	let v = gen_local ctx (spawn_monomorph ctx p) p in
+	let ev = mk (TLocal v) v.v_type p in
+	let e_ref = store_typed_expr ctx.com ev p in
 	let et = ref (EConst(Ident "null"),p) in
 	let et = ref (EConst(Ident "null"),p) in
 	let comprehension_pos = p in
 	let comprehension_pos = p in
 	let rec map_compr (e,p) =
 	let rec map_compr (e,p) =
@@ -1409,10 +1411,10 @@ and type_array_comprehension ctx e with_type p =
 		| EParenthesis e2 -> (EParenthesis (map_compr e2),p)
 		| EParenthesis e2 -> (EParenthesis (map_compr e2),p)
 		| EBinop(OpArrow,a,b) ->
 		| EBinop(OpArrow,a,b) ->
 			et := (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),comprehension_pos);
 			et := (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),comprehension_pos);
-			(ECall ((efield ((EConst (Ident v.v_name),p),"set"),p),[a;b]),p)
+			(ECall ((efield (e_ref,"set"),p),[a;b]),p)
 		| _ ->
 		| _ ->
 			et := (EArrayDecl [],comprehension_pos);
 			et := (EArrayDecl [],comprehension_pos);
-			(ECall ((efield ((EConst (Ident v.v_name),p),"push"),p),[(e,p)]),p)
+			(ECall ((efield (e_ref,"push"),p),[(e,p)]),p)
 	in
 	in
 	let e = map_compr e in
 	let e = map_compr e in
 	let ea = type_expr ctx !et with_type in
 	let ea = type_expr ctx !et with_type in
@@ -1421,7 +1423,7 @@ and type_array_comprehension ctx e with_type p =
 	mk (TBlock [
 	mk (TBlock [
 		mk (TVar (v,Some ea)) ctx.t.tvoid p;
 		mk (TVar (v,Some ea)) ctx.t.tvoid p;
 		efor;
 		efor;
-		mk (TLocal v) v.v_type p;
+		ev;
 	]) v.v_type p
 	]) v.v_type p
 
 
 and type_return ?(implicit=false) ctx e with_type p =
 and type_return ?(implicit=false) ctx e with_type p =