Skip to content

Nullness issue - match control flow for reference types #18488

@Smaug123

Description

@Smaug123

Issue description

let bar =
    let getEnvironmentVariable : string -> _ = failwith ""

    match "ENVVAR" |> getEnvironmentVariable with
    | null ->
        failwith ""
    | x ->
        x // inferred incorrectly as `obj | null` rather than `obj`

We've just pattern-matched, so it's locally provable that x is not null.

It's not explicitly stated in the RFC that this flow analysis is banned - the examples all use the new active pattern Null - but it's a big annoyance; I don't think it's possible for me to write polyglot F#-with-nullability and F#-before-v9 without this feature and without using unsafe casts? Is there a way to inject System.GetEnvironmentVariable as an argument to a function, while also satisfying the nullability checker, without breaking backward-compatibility and without calling unbox<string> (s : string | null) or similar?

My context is that I'm writing a source generator, so I must produce source which is compatible with any F# version (or else duplicate large amounts of code by providing two different generators). (Smaug123/WoofWare.Myriad#364)

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.203

Reproducible code snippet and actual behavior

No response

Possible workarounds

Use |> unbox<string>.

Metadata

Metadata

Assignees

Labels

Area-NullnessIssues related to handling of Nullable Reference TypesBug

Type

No type

Projects

Status

New

Relationships

None yet

Development

No branches or pull requests

Issue actions