[nullsafe][refactor] Make DereferenceViolation depend on InferredNullability instead of Nullability

Summary: This diff is on par with this change, with the same motivation

Reviewed By: artempyanykh

Differential Revision: D22924891

fbshipit-source-id: 578ca5869
master
Mitya Lyubarskiy 4 years ago committed by Facebook GitHub Bot
parent 81951edf55
commit 3573e0d202

@ -6,7 +6,7 @@
*)
open! IStd
type violation = {nullability: Nullability.t} [@@deriving compare]
type violation = {nullability: InferredNullability.t} [@@deriving compare]
module ReportableViolation = struct
type t = {nullsafe_mode: NullsafeMode.t; violation: violation}
@ -19,7 +19,10 @@ module ReportableViolation = struct
[@@deriving compare]
let from nullsafe_mode ({nullability} as violation) =
if Nullability.is_considered_nonnull ~nullsafe_mode nullability then None
if
Nullability.is_considered_nonnull ~nullsafe_mode
(InferredNullability.get_nullability nullability)
then None
else Some {nullsafe_mode; violation}
@ -96,14 +99,16 @@ module ReportableViolation = struct
let get_description {nullsafe_mode; violation= {nullability}} ~dereference_location
dereference_type ~nullable_object_descr ~nullable_object_origin =
dereference_type ~nullable_object_descr =
let user_friendly_nullable =
ErrorRenderingUtils.UserFriendlyNullable.from_nullability nullability
ErrorRenderingUtils.UserFriendlyNullable.from_nullability
(InferredNullability.get_nullability nullability)
|> IOption.if_none_eval ~f:(fun () ->
Logging.die InternalError
"get_description:: Dereference violation should not be possible for non-nullable \
values" )
in
let nullable_object_origin = InferredNullability.get_origin nullability in
match user_friendly_nullable with
| ErrorRenderingUtils.UserFriendlyNullable.UntrustedNonnull untrusted_kind ->
(* Attempt to dereference a value which is not explictly declared as nullable,
@ -121,7 +126,7 @@ module ReportableViolation = struct
end
let check nullability =
match nullability with
match InferredNullability.get_nullability nullability with
(* StrictNonnull is the only "real" value that is not null according to type system rules.
Other values can not be fully trusted.
*)

@ -11,7 +11,7 @@ open! IStd
type violation [@@deriving compare]
val check : Nullability.t -> (unit, violation) result
val check : InferredNullability.t -> (unit, violation) result
(** violation of Dereference rule reflects possibility of dereferencing of `null`. Note that this
might or might not be severe enough to be reported to the user, depending on the mode
agreements. *)
@ -39,7 +39,6 @@ module ReportableViolation : sig
-> dereference_location:Location.t
-> dereference_type
-> nullable_object_descr:string option
-> nullable_object_origin:TypeOrigin.t
-> string * IssueType.t * Location.t
(** Given context around violation, return error message together with the info where to put this
message *)

@ -28,18 +28,11 @@ let is_virtual = function
let check_object_dereference ({IntraproceduralAnalysis.tenv; _} as analysis_data) ~nullsafe_mode
find_canonical_duplicate node instr_ref object_exp dereference_type inferred_nullability loc =
Result.iter_error
(DereferenceRule.check (InferredNullability.get_nullability inferred_nullability))
~f:(fun dereference_violation ->
let nullable_object_origin = InferredNullability.get_origin inferred_nullability in
Result.iter_error (DereferenceRule.check inferred_nullability) ~f:(fun dereference_violation ->
let nullable_object_descr = explain_expr tenv node object_exp in
let type_error =
TypeErr.Nullable_dereference
{ dereference_violation
; dereference_location= loc
; nullable_object_descr
; dereference_type
; nullable_object_origin }
{dereference_violation; dereference_location= loc; nullable_object_descr; dereference_type}
in
TypeErr.register_error analysis_data find_canonical_duplicate type_error (Some instr_ref)
~nullsafe_mode loc )

@ -75,8 +75,7 @@ type err_instance =
{ dereference_violation: DereferenceRule.violation
; dereference_location: Location.t
; dereference_type: DereferenceRule.ReportableViolation.dereference_type
; nullable_object_descr: string option
; nullable_object_origin: TypeOrigin.t }
; nullable_object_descr: string option }
| Bad_assignment of
{ assignment_violation: AssignmentRule.violation
; assignment_location: Location.t
@ -269,11 +268,7 @@ let get_error_info_if_reportable_lazy ~nullsafe_mode err_instance =
let severity = AssignmentRule.ReportableViolation.get_severity reportable_violation in
(description, issue_type, Some error_location, severity) )
| Nullable_dereference
{ dereference_violation
; dereference_location
; nullable_object_descr
; dereference_type
; nullable_object_origin } ->
{dereference_violation; dereference_location; nullable_object_descr; dereference_type} ->
(* If violation is reportable, create tuple, otherwise None *)
let+ reportable_violation =
DereferenceRule.ReportableViolation.from nullsafe_mode dereference_violation
@ -281,7 +276,7 @@ let get_error_info_if_reportable_lazy ~nullsafe_mode err_instance =
lazy
(let description, issue_type, error_location =
DereferenceRule.ReportableViolation.get_description reportable_violation
~dereference_location dereference_type ~nullable_object_descr ~nullable_object_origin
~dereference_location dereference_type ~nullable_object_descr
in
let severity = DereferenceRule.ReportableViolation.get_severity reportable_violation in
(description, issue_type, Some error_location, severity) )

@ -49,8 +49,7 @@ type err_instance =
{ dereference_violation: DereferenceRule.violation
; dereference_location: Location.t
; dereference_type: DereferenceRule.ReportableViolation.dereference_type
; nullable_object_descr: string option
; nullable_object_origin: TypeOrigin.t }
; nullable_object_descr: string option }
| Bad_assignment of
{ assignment_violation: AssignmentRule.violation
; assignment_location: Location.t

Loading…
Cancel
Save