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

Better diagnostics for mixing different nullness pattern matching ways #17432

Open
psfinaki opened this issue Jul 23, 2024 · 6 comments
Open
Labels
Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen Area-Diagnostics mistakes and possible improvements to diagnostics Area-Nullness Issues related to handling of Nullable Reference Types
Milestone

Comments

@psfinaki
Copy link
Member

psfinaki commented Jul 23, 2024

There are 2 ways to do null-related pattern matching now:

let len1 (str: string | null) =
    match str with
    | Null -> -1
    | NonNull s -> s.Length

let len2 (str: string | null) =
    match str with
    | null -> -1
    | s -> s.Length

Both produce no warnings, all good. What I don't like is the behavior when we start mixing those two approaches:

let len3 (str: string | null) =
    match str with
    | null -> -1
    | NonNull s -> s.Length 

warning FS3262: Value known to be without null passed to a function meant for nullables: You can remove this |Null|NonNull| pattern usage.

let len4 (str: string | null) =
    match str with
    | Null -> -1
    | s -> s.Length 

warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability.


As a user I just want consistent diags here in the spirit of "hey, pick one way or another, don't mix things up" - ideally with exact pointers what to change but that's an extra.

I think such errors can happen really often because of fatfingering or copypasting - and casing differences are not even very noticeable, so there is accessibility in the game as well.

Originally posted by @psfinaki in #15181 (comment)

@github-actions github-actions bot added this to the Backlog milestone Jul 23, 2024
@psfinaki psfinaki added Area-Nullness Issues related to handling of Nullable Reference Types Area-Diagnostics mistakes and possible improvements to diagnostics labels Jul 23, 2024
@T-Gro
Copy link
Member

T-Gro commented Jul 31, 2024

Related to #17409

@T-Gro T-Gro added Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen and removed Needs-Triage labels Jul 31, 2024
@T-Gro
Copy link
Member

T-Gro commented Jul 31, 2024

Might be implicitely solved by contextual information to nullness diag (... "possible derefence"..)

@T-Gro
Copy link
Member

T-Gro commented Jul 31, 2024

Idea: Make the active pattern |Null|NonNull| to an module which needs explicit opening or explicit prefixing.

@T-Gro
Copy link
Member

T-Gro commented Jul 31, 2024

Idea: RQA for the active pattern:

[<RequireQualifiedAccess>]
module Nullable = 

    let inline (|Null|NonNull|) (x: 'T) : Choice<unit,'T> = 
        match x with null -> Null | v -> NonNull v

@smoothdeveloper
Copy link
Contributor

What is the main reason for those patterns to exist?

I find matching on literal null is most natural thing to do.

I'd favour explicit opening but no RQA attribute as it makes it really inconvenient to use for those that prefer it.

@T-Gro
Copy link
Member

T-Gro commented Aug 1, 2024

The active pattern can eliminated nullness from a type (i.e. what you match as values will be known to be non-nullable) even in deeply nested scenarios. Imagine a string | null field within record, being wrapped in voption, within another record.

The the classical null literal this only eliminated nullness on first level values, but not from nested members of types.

From it's signature, NonNull will give you a value known to be not null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen Area-Diagnostics mistakes and possible improvements to diagnostics Area-Nullness Issues related to handling of Nullable Reference Types
Projects
Status: New
Development

No branches or pull requests

3 participants