2
0
Эх сурвалжийг харах

use local name instead of temp name for ??

closes #11464
Simon Krajewski 10 сар өмнө
parent
commit
109bdfccc9

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

@@ -35,21 +35,12 @@ let preprocess_expr com e = match com.display.dms_kind with
 	| DMSignature -> ExprPreprocessing.find_display_call e
 	| _ -> e
 
-let get_expected_name with_type = match with_type with
-	| WithType.Value (Some src) | WithType.WithType(_,Some src) ->
-		(match src with
-		| WithType.FunctionArgument si -> Some si.si_name
-		| WithType.StructureField si -> Some si .si_name
-		| WithType.ImplicitReturn -> None
-		)
-	| _ -> None
-
 let sort_fields l with_type tk =
 	let p = match tk with
 		| TKExpr p | TKField p -> Some p
 		| _ -> None
 	in
-	let expected_name = get_expected_name with_type in
+	let expected_name = WithType.get_expected_name with_type in
 	let l = List.map (fun ci ->
 		let i = get_sort_index tk ci (Option.default Globals.null_pos p) expected_name in
 		ci,i

+ 15 - 0
src/core/withType.ml

@@ -9,6 +9,7 @@ type with_type_source =
 	| FunctionArgument of with_type_source_information
 	| StructureField of with_type_source_information
 	| ImplicitReturn
+	| LocalVariable of string
 
 type t =
 	| NoValue
@@ -25,6 +26,7 @@ let of_implicit_return t = WithType(t,Some ImplicitReturn)
 let with_argument t name = WithType(t,Some(FunctionArgument (make_with_type_source_information name None)))
 let with_argument_and_doc t name doc = WithType(t,Some(FunctionArgument (make_with_type_source_information name (Some doc))))
 let with_structure_field t name = WithType(t,Some(StructureField (make_with_type_source_information name None)))
+let with_local_variable t name = WithType(t,Some(LocalVariable name))
 let value = Value None
 let named_argument name = Value (Some(FunctionArgument (make_with_type_source_information name None)))
 let named_structure_field name = Value (Some(StructureField (make_with_type_source_information name None)))
@@ -34,9 +36,22 @@ let to_string = function
 	| NoValue -> "NoValue"
 	| Value (None | Some ImplicitReturn) -> "Value"
 	| Value (Some(FunctionArgument si | StructureField si)) -> "Value " ^ si.si_name
+	| Value (Some(LocalVariable name)) -> "Value " ^ name
 	| WithType(t,s) ->
 		let name = match s with
 			| Some(FunctionArgument si | StructureField si) -> si.si_name
+			| Some(LocalVariable name) -> name
 			| _ -> "None"
 		in
 		Printf.sprintf "WithType(%s, %s)" (s_type (print_context()) t) name
+
+let get_expected_name with_type = match with_type with
+	| Value (Some src) | WithType(_,Some src) ->
+		(match src with
+		| FunctionArgument si -> Some si.si_name
+		| StructureField si -> Some si.si_name
+		| LocalVariable name -> Some name
+		| ImplicitReturn -> None
+		)
+	| _ ->
+		None

+ 2 - 2
src/typing/typer.ml

@@ -703,7 +703,7 @@ and type_vars ctx vl p =
 				| Some e ->
 					let old_in_loop = ctx.e.in_loop in
 					if ev.ev_static then ctx.e.in_loop <- false;
-					let e = Std.finally (fun () -> ctx.e.in_loop <- old_in_loop) (type_expr ctx e) (WithType.with_type t) in
+					let e = Std.finally (fun () -> ctx.e.in_loop <- old_in_loop) (type_expr ctx e) (WithType.with_local_variable t n) in
 					let e = AbstractCast.cast_or_unify ctx t e p in
 					Some e
 			) in
@@ -1866,7 +1866,7 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
 			| _ -> follow_null tmin
 		in
 		let e1_null_t = if is_nullable e1.etype then e1.etype else ctx.t.tnull e1.etype in
-		let e1 = vr#as_var "tmp" {e1 with etype = e1_null_t} in
+		let e1 = vr#as_var (Option.default "tmp" (WithType.get_expected_name with_type)) {e1 with etype = e1_null_t} in
 		let e_null = Builder.make_null e1_null_t e1.epos in
 		let e_cond = mk (TBinop(OpNotEq,e1,e_null)) ctx.t.tbool e1.epos in
 		let e_if = mk (TIf(e_cond,cast e1,Some e2)) iftype p in

+ 19 - 0
tests/optimization/src/issues/Issue11464.hx

@@ -0,0 +1,19 @@
+package issues;
+
+class Issue11464 {
+	@:js('
+		var name = issues_Issue11464.call();
+		issues_Issue11464.use(name != null ? name : "default");
+	')
+	static function test() {
+		final name = call() ?? "default";
+		use(name);
+	}
+
+	static function call() {
+		return "";
+	}
+
+	@:pure(false)
+	static function use(v:String) {}
+}