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 - nullable types are not automatically inferred #17738

Closed
1 of 7 tasks
Lanayx opened this issue Sep 15, 2024 · 2 comments
Closed
1 of 7 tasks

Nullness issue - nullable types are not automatically inferred #17738

Lanayx opened this issue Sep 15, 2024 · 2 comments
Labels
Area-Nullness Issues related to handling of Nullable Reference Types Bug
Milestone

Comments

@Lanayx
Copy link
Contributor

Lanayx commented Sep 15, 2024

Issue description

I expect NRT to be inferred just like other types, but nullness information is lost when automatic infer happens

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

Reproducible code snippet and actual behavior

let f1 (x: string | null) = ""
let f2 x = f1 x // x has just type string

Possible workarounds

Specify explicit type signatures everywhere

@Lanayx Lanayx added Area-Nullness Issues related to handling of Nullable Reference Types Bug Needs-Triage labels Sep 15, 2024
@github-actions github-actions bot added this to the Backlog milestone Sep 15, 2024
@T-Gro
Copy link
Member

T-Gro commented Sep 15, 2024

This by design and an intentional feature implementation.
Why? Because in the opposite scenario, imagine the following code:

let f1 (x: string | null) = ""
let f2 x = 
     let y = f1 x // x was infered to be nullable string
     x.Length // suddenly, a nullness warning is returned on this line

Which is something I received many times when implementing nullness on this codebase itself for the fist time, before inference rules were changed.
There is a clear tradeoff between the two, and the decision is to make inference favour code that avoids nulls. And annotations have to be put in explicitly by users who want to allow nulls in, not vice versa.

==> This is by design.

@T-Gro T-Gro closed this as completed Sep 15, 2024
@T-Gro T-Gro removed their assignment Sep 15, 2024
@T-Gro
Copy link
Member

T-Gro commented Sep 15, 2024

This plays with the ideology at eliminating nullness as soon is it flows (typically via interop) into F#, not having it spread throughout F# codebase quickly.

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
Projects
Archived in project
Development

No branches or pull requests

2 participants