Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullness issue - Spurious nullness warning with flexible type #18056

Open
1 of 7 tasks
glchapman opened this issue Nov 24, 2024 · 3 comments
Open
1 of 7 tasks

Nullness issue - Spurious nullness warning with flexible type #18056

glchapman opened this issue Nov 24, 2024 · 3 comments
Labels
Area-Nullness Issues related to handling of Nullable Reference Types Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Milestone

Comments

@glchapman
Copy link

Issue description

I get a nullness warning with the following:

let dispose (x: IDisposable | null) : unit =
    match x with
    | null -> ()
    | d -> d.Dispose()

let useThing (thing: #IDisposable) =
    try
        printfn "%O" thing
    finally
        dispose thing // warning generated

Since the dispose function allows null, I do not think a warning should be generated. No warning is generated for a non-flexible type, e.g.:

let useThing (thing: IDisposable) =
    try
        printfn "%O" thing
    finally
        dispose thing // no warning generated 

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

net9.0

Reproducible code snippet and actual behavior

No response

Possible workarounds

No response

@glchapman glchapman added Area-Nullness Issues related to handling of Nullable Reference Types Bug Needs-Triage labels Nov 24, 2024
@github-actions github-actions bot added this to the Backlog milestone Nov 24, 2024
@glchapman
Copy link
Author

I just reread the section "Inference in generic code" from Nullable Reference Types in F# 9. I thought perhaps it meant that the following should work without a warning, but a warning is still generated

let useThing<'d when 'd: not struct and 'd :> IDisposable> (thing: 'd) =
    try
        printfn "%O" thing
    finally
        dispose thing // warning generated

@T-Gro
Copy link
Member

T-Gro commented Nov 25, 2024

Could you please also include which warning this produces incl. the range (= coordinates in code) for it?

@T-Gro T-Gro removed their assignment Nov 25, 2024
@glchapman
Copy link
Author

The warning is:

Program.fs(12,17): warning FS3261: Nullness warning: The types 'IDisposable' and 'IDisposable | null' do not have equivalent nullability.

In VSCode, thing in the last line (dispose thing) is highlighted.

@abonie abonie modified the milestones: Backlog, November-2024 Nov 25, 2024
@abonie abonie added Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. and removed Needs-Triage labels Nov 25, 2024
@T-Gro T-Gro modified the milestones: November-2024, December-2024 Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Nullness Issues related to handling of Nullable Reference Types Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Projects
Status: New
Development

No branches or pull requests

3 participants