Skip to content

Commit

Permalink
Nullness bugfix :: could not assign null to member val property (#17845)
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Gro authored Oct 15, 2024
1 parent 349a95d commit 3493c76
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fix extension methods support for non-reference system assemblies ([PR #17799](https://github.com/dotnet/fsharp/pull/17799))
* Ensure `frameworkTcImportsCache` mutations are thread-safe. ([PR #17795](https://github.com/dotnet/fsharp/pull/17795))
* Fix concurrency issue in `ILPreTypeDefImpl` ([PR #17812](https://github.com/dotnet/fsharp/pull/17812))
* Fix nullness inference for member val and other OO scenarios ([PR #17845](https://github.com/dotnet/fsharp/pull/17845))

### Added

Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/TypedTree/TypedTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4358,6 +4358,12 @@ type NullnessVar() =

member nv.IsSolved = solution.IsSome

member nv.IsFullySolved =
match solution with
| None -> false
| Some (Nullness.Known _) -> true
| Some (Nullness.Variable v) -> v.IsFullySolved

member nv.Set(nullness) =
assert (not nv.IsSolved)
solution <- Some nullness
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/TypedTree/TypedTree.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,7 @@ type NullnessVar =
member Evaluate: unit -> NullnessInfo
member TryEvaluate: unit -> NullnessInfo voption
member IsSolved: bool
member IsFullySolved: bool
member Set: Nullness -> unit
member Unset: unit -> unit
member Solution: Nullness
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/TypedTree/TypedTreeBasics.fs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ let tryAddNullnessToTy nullnessNew (ty:TType) =
let addNullnessToTy (nullness: Nullness) (ty:TType) =
match nullness with
| Nullness.Known NullnessInfo.WithoutNull -> ty
| Nullness.Variable nv when nv.IsSolved && nv.Evaluate() = NullnessInfo.WithoutNull -> ty
| Nullness.Variable nv when nv.IsFullySolved && nv.TryEvaluate() = ValueSome NullnessInfo.WithoutNull -> ty
| _ ->
match ty with
| TType_var (tp, nullnessOrig) -> TType_var (tp, combineNullness nullnessOrig nullness)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ let nonStrictFunc(x:string | null) = strictFunc(x)
|> shouldFail
|> withDiagnostics [
Error 3261, Line 4, Col 49, Line 4, Col 50, "Nullness warning: The types 'string' and 'string | null' do not have equivalent nullability."]

[<Fact>]
let ``Can have nullable prop of same type T within a custom type T``() =
FSharp """
module MyLib
type T () =
let mutable v : T | null = null
member val P : T | null = null with get, set
member this.M() =
v <- null
this.P <- null
"""
|> asLibrary
|> typeCheckWithStrictNullness
|> shouldSucceed

[<Theory>]
[<InlineData("fileExists(path)")>]
Expand Down

0 comments on commit 3493c76

Please sign in to comment.