Browse Source

[nullsafety] check left & right expressions of assignment before the assignment itself

Alexander Kuzmenko 6 years ago
parent
commit
4cbc7acaad
2 changed files with 14 additions and 10 deletions
  1. 5 10
      src/typing/nullSafety.ml
  2. 9 0
      tests/nullsafety/src/cases/TestStrict.hx

+ 5 - 10
src/typing/nullSafety.ml

@@ -1026,7 +1026,7 @@ class expr_checker mode immediate_execution report =
 		*)
 		method private check_while e =
 			match e.eexpr with
-				| TWhile (_, _, while_flag) ->
+				| TWhile _ ->
 					let check_condition condition =
 						if self#is_nullable_expr condition then
 							self#error "Cannot use nullable value as a condition in \"while\"." condition.epos;
@@ -1175,16 +1175,11 @@ class expr_checker mode immediate_execution report =
 				| OpBoolOr ->
 					local_safety#process_or left_expr right_expr self#is_nullable_expr self#check_expr
 				| OpAssign ->
+					check_both();
 					if not (self#can_pass_expr right_expr left_expr.etype p) then
-						begin
-							self#error "Cannot assign nullable value here." p;
-							check_both()
-						end
+						self#error "Cannot assign nullable value here." p
 					else
-						begin
-							check_both();
-							local_safety#handle_assignment self#is_nullable_expr left_expr right_expr;
-						end
+						local_safety#handle_assignment self#is_nullable_expr left_expr right_expr;
 				| _->
 					if self#is_nullable_expr left_expr || self#is_nullable_expr right_expr then
 						self#error "Cannot perform binary operation on nullable value." p;
@@ -1288,7 +1283,7 @@ class class_checker cls immediate_execution report  =
 			if is_safe_class && (not cls.cl_extern) && (not cls.cl_interface) then
 				self#check_var_fields;
 			let check_field is_static f =
-				(* if f.cf_name = "return_assignNonNullable_shouldPass" then
+				(* if f.cf_name = "ternary_returnedFromInlinedFunction_shouldPass" then
 					Option.may (fun e -> print_endline (s_expr str_type e)) f.cf_expr; *)
 				match (safety_mode (cls.cl_meta @ f.cf_meta)) with
 					| SMOff -> ()

+ 9 - 0
tests/nullsafety/src/cases/TestStrict.hx

@@ -267,6 +267,15 @@ class TestStrict {
 		shouldFail((true ? 'hello' : v).length);
 	}
 
+	static function ternary_returnedFromInlinedFunction_shouldPass() {
+		var str:String = inlinedNullableSafeString([null]);
+	}
+
+	static inline function inlinedNullableSafeString(nullables:Array<Null<String>>):String {
+		var s = nullables[0];
+		return (s != null ? s : 'hello' );
+	}
+
 	static function arrayAccess_nullableArray_shouldFail() {
 		var a:Null<Array<Int>> = null;
 		shouldFail(a[0]);