ソースを参照

[filters] turn "on static platforms, null can't be used as basic type" error into a warning

Rudy Ges 1 週間 前
コミット
7ba02290e8

+ 5 - 0
src-json/warning.json

@@ -89,6 +89,11 @@
 		"doc": "The compiler could not determine a type order due to mutually dependent static initializations",
 		"parent": "WTyper"
 	},
+	{
+		"name": "WStaticPlatformBasicTypeNull",
+		"doc": "Warns when the compiler replaces `null` with the default value for a basic type on static platforms.",
+		"parent": "WTyper"
+	},
 	{
 		"name": "WClosureCompare",
 		"doc": "Closures are being compared, which might be undefined",

+ 1 - 1
src/filters/filters.ml

@@ -439,7 +439,7 @@ let run_safe_filters ectx com (scom : SafeCom.t) all_types_array new_types_array
 	] in
 
 	let filters_after_analyzer = [
-		"sanitize",(fun scom e -> Sanitize.sanitize scom.SafeCom.platform_config e);
+		"sanitize",(fun scom e -> Sanitize.sanitize scom e);
 		"add_final_return",(fun _ -> if scom.platform_config.pf_add_final_return then AddFinalReturn.add_final_return else (fun e -> e));
 		"RenameVars",(match scom.platform with
 			| Eval -> (fun _ e -> e)

+ 1 - 1
src/filters/safe/addFieldInits.ml

@@ -54,7 +54,7 @@ let add_field_inits cl_path locals scom t =
 			| Some e ->
 				(* This seems a bit expensive, but hopefully constructor expressions aren't that massive. *)
 				let e = RenameVars.run cl_path locals e in
-				let e = Sanitize.sanitize scom.platform_config e in
+				let e = Sanitize.sanitize scom e in
 				let e = if scom.platform_config.pf_add_final_return then AddFinalReturn.add_final_return e else e in
 				cf.cf_expr <- Some e
 			| _ ->

+ 6 - 6
src/filters/safe/sanitize.ml

@@ -42,7 +42,7 @@ let rec need_parent e =
 	| TCast _ | TThrow _ | TReturn _ | TTry _ | TSwitch _ | TIf _ | TWhile _ | TBinop _ | TContinue | TBreak
 	| TBlock _ | TVar _ | TFunction _ | TUnop _ -> true
 
-let sanitize_expr config e =
+let sanitize_expr scom e =
 	let parent e =
 		match e.eexpr with
 		| TParenthesis _ -> e
@@ -70,15 +70,15 @@ let sanitize_expr config e =
 	in
 	match e.eexpr with
 	| TConst TNull ->
-		if config.PlatformConfig.pf_static && not (is_nullable e.etype) then begin
+		if scom.SafeCom.platform_config.pf_static && not (is_nullable e.etype) then begin
 			let rec loop t = match follow t with
 				| TMono _ -> () (* in these cases the null will cast to default value *)
 				| TFun _ -> () (* this is a bit a particular case, maybe flash-specific actually *)
 				(* TODO: this should use get_underlying_type, but we do not have access to Codegen here.  *)
 				| TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) -> loop (apply_params a.a_params tl a.a_this)
+				| _ when scom.platform == Cross -> ()
 				| _ ->
-					if config != Common.default_config then (* This is atrocious; see #12105 *)
-						Error.raise_typing_error ("On static platforms, null can't be used as basic type " ^ s_type (print_context()) e.etype) e.epos
+					SafeCom.add_warning scom WStaticPlatformBasicTypeNull (Printf.sprintf "On static platforms, `null` can't be used as basic type `%s`; using platform default value instead" (s_type (print_context()) e.etype)) e.epos
 			in
 			loop e.etype
 		end;
@@ -194,5 +194,5 @@ let reduce_expr com e =
 	| _ ->
 		e
 
-let rec sanitize config e =
-	sanitize_expr config (reduce_expr config (Type.map_expr (sanitize config) e))
+let rec sanitize scom e =
+	sanitize_expr scom (reduce_expr scom.SafeCom.platform_config (Type.map_expr (sanitize scom) e))

+ 1 - 1
src/optimization/optimizer.ml

@@ -132,7 +132,7 @@ let reduce_control_flow scom e = match e.eexpr with
 let rec reduce_loop (scom : SafeCom.t) stack e =
 	let e = Type.map_expr (reduce_loop scom stack) e in
 	let reduce_expr = Sanitize.reduce_expr in
-	Sanitize.sanitize_expr scom.platform_config (match e.eexpr with
+	Sanitize.sanitize_expr scom (match e.eexpr with
 	| TCall(e1,el) ->
 		begin match Texpr.skip e1 with
 			| { eexpr = TFunction func } as ef ->