dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.94k stars 789 forks source link

Better diagnostics for nullness inference related errors #17433

Open psfinaki opened 4 months ago

psfinaki commented 4 months ago

I think it would be good to be more explicit about nullness inference happening behind the scenes.

For the code

let xs = [| ""; ""; null |]

we'll get

Nullness warning: The type 'string' does not support 'null'

It would be helpful to have something like

"xs was inferred to have the type string[] but got null among its values. Either explicitly specify null in the return type ((string | null)[]), or remove null from the collection - or suppress the warning but no please don't."

_Originally posted by @psfinaki in https://github.com/dotnet/fsharp/pull/15181#discussion_r1679527189_

vzarytovskii commented 4 months ago

We don't differentiate between collections and other types. string and string array are no different for us. This would probably be better solved in analyzers, rather than typechecker.

T-Gro commented 4 months ago

Related to: https://github.com/dotnet/fsharp/issues/17409

T-Gro commented 4 months ago

Option: Specialize type unification for list/array/sequence builders.

T-Gro commented 4 months ago

Diagnostics can offer: Explicit annotation or "be first" rule.