Skip to content

Nullness issue - No warning when nullable generic return type used with anonymous record #17951

@kerams

Description

@kerams

Issue description

type R = { x: int }
type RA = {| x: int |}

[<EntryPoint>]
let main _args =
    let a = System.Text.Json.JsonSerializer.Deserialize<{| x: int |}> "null"
    let _a = a.x

    let b = System.Text.Json.JsonSerializer.Deserialize<RA> "null"
    let _b = b.x

    let c = System.Text.Json.JsonSerializer.Deserialize<R> "null"
    let _c = c.x
    0

The return type of Deserialize is 'T | null, but a warning is only raised for the nominal record on c.x, yet a.x and b.x will throw too.

Additionally, there's a discrepancy in tooltips - a is val a: {| a: int |}, but on b, where the type is aliased, it is val b: RA | null.

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

Windows (Default)

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

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

.NET Runtime/SDK version

9.0.100-rc.2.24474.11

Reproducible code snippet and actual behavior

No response

Possible workarounds

No response

Metadata

Metadata

Assignees

Labels

Area-NullnessIssues related to handling of Nullable Reference TypesBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

Type

No fields configured for Bug.

Projects

Status

New

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions