Skip to content

Nullness issue - Option<string> is marked as NotNull using NullabilityInfoContext #17517

@pchalamet

Description

@pchalamet

Issue description

Looks like Option<string> in marked as NotNull when using NullabilityInfoContext. I expect it to be reported as Nullable since Option<string> for null has a null representation (UseNullAsTrueValue).

With provided snippet, I expect StringOption to be reported as Nullable, not NotNull.

StringOrNull = <null>
StringOption = None <-- this
ObjNull = <null>
ObjOrNull = <null>
StringOrNull => Nullable / Nullable
StringOption => NotNull / NotNull <-- this
ObjNull => Nullable / Nullable
ObjOrNull => Nullable / Nullable

Choose one or more from the following categories of impact

  • Unexpected nullness warning (false positive in nullness checking, code uses --checknulls and langversion:preview).
  • Missing nullness warning in a case which can produce nulls (false negative, code uses --checknulls and langversion:preview).
  • Breaking change related to older null constructs in code not using the checknulls switch.
  • Breaking change related to generic code and explicit type constraints (null, not null).
  • Type inference issue (i.e. code worked without type annotations before, and applying the --checknulls enforces type annotations).
  • C#/F# interop issue related to nullness metadata.
  • Other (none of the categories above apply).

Operating System

macOS

What .NET runtime/SDK kind are you seeing the issue on

.NET SDK (.NET Core, .NET 5+)

.NET Runtime/SDK version

9.0.100-preview.6.24328.19

Reproducible code snippet and actual behavior

open System

open System
open System.Reflection

type MyClass(StringOrNull: string | null, StringOption: string option, ObjNull: objnull, ObjOrNull: obj | null) =
    member val StringOrNull = StringOrNull
    member val StringOption = StringOption
    member val ObjNull = ObjNull
    member val ObjOrNull = ObjOrNull

let classType = typeof<MyClass>
let ctor = classType.GetConstructors() |> Seq.exactlyOne
let paramInfos = ctor.GetParameters()

let inst = ctor.Invoke([| null; null; null; null |]) :?> MyClass
printfn "StringOrNull = %A" inst.StringOrNull
printfn "StringOption = %A" inst.StringOption
printfn "ObjNull = %A" inst.ObjNull
printfn "ObjOrNull = %A" inst.ObjOrNull

let nrtContext = NullabilityInfoContext()
for paramInfo in paramInfos do
    let nrtInfo = nrtContext.Create(paramInfo)
    let readState = nrtInfo.ReadState
    let writeState = nrtInfo.WriteState
    printfn $"{paramInfo.Name} => {readState} / {writeState}"

Possible workarounds

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions