[nullsafe] Treat not annotated synthetic fields as StrictNonnull

Reviewed By: artempyanykh

Differential Revision: D21040264

fbshipit-source-id: 3557c5f60
master
Mitya Lyubarskiy 5 years ago committed by Facebook GitHub Bot
parent 6247437296
commit c4c8c053a1

@ -36,6 +36,8 @@ let is_enum_value tenv ~class_typ (field_info : Struct.field_info) =
false false
let is_synthetic field_name = String.contains field_name '$'
let get tenv field_name class_typ = let get tenv field_name class_typ =
let open IOption.Let_syntax in let open IOption.Let_syntax in
let lookup = Tenv.lookup tenv in let lookup = Tenv.lookup tenv in
@ -59,13 +61,19 @@ let get tenv field_name class_typ =
~is_third_party field_typ annotations ~is_third_party field_typ annotations
in in
let corrected_nullability = let corrected_nullability =
if Nullability.is_nonnullish (AnnotatedNullability.get_nullability nullability) && is_enum_value if Nullability.is_nonnullish (AnnotatedNullability.get_nullability nullability) then
then if
(* Enum values are the special case - they can not be null. So we can strengten nullability. is_enum_value
Note that if it is nullable, we do NOT change nullability: in this case this is probably (* Enum values are the special case - they can not be null. So we can strengten nullability.
not an enum value, but just a static field annotated as nullable. Note that if it is nullable, we do NOT change nullability: in this case this is probably
*) not an enum value, but just a static field annotated as nullable.
AnnotatedNullability.StrictNonnull EnumValue *)
then AnnotatedNullability.StrictNonnull EnumValue
else if is_synthetic (Fieldname.get_field_name field_name) then
(* This field is artifact of codegen and is not visible to the user.
Surfacing it as non-strict is non-actionable for the user *)
AnnotatedNullability.StrictNonnull SyntheticField
else nullability
else nullability else nullability
in in
let annotated_type = AnnotatedType.{nullability= corrected_nullability; typ= field_typ} in let annotated_type = AnnotatedType.{nullability= corrected_nullability; typ= field_typ} in

@ -39,6 +39,7 @@ and strict_nonnull_origin =
| StrictMode | StrictMode
| PrimitiveType | PrimitiveType
| EnumValue | EnumValue
| SyntheticField
[@@deriving compare] [@@deriving compare]
let get_nullability = function let get_nullability = function
@ -83,6 +84,8 @@ let pp fmt t =
"primitive" "primitive"
| EnumValue -> | EnumValue ->
"enum" "enum"
| SyntheticField ->
"synthetic_field"
in in
match t with match t with
| Nullable origin -> | Nullable origin ->

@ -61,6 +61,9 @@ and strict_nonnull_origin =
| PrimitiveType (** Primitive types are non-nullable by language design *) | PrimitiveType (** Primitive types are non-nullable by language design *)
| EnumValue | EnumValue
(** Java enum value are statically initialized with non-nulls according to language semantics *) (** Java enum value are statically initialized with non-nulls according to language semantics *)
| SyntheticField
(** Fake field that is not part of user codebase, but rather artifact of codegen/annotation
processor *)
[@@deriving compare] [@@deriving compare]
val get_nullability : t -> Nullability.t val get_nullability : t -> Nullability.t

Loading…
Cancel
Save