Skip to content

Commit 3493c76

Browse files
authored
Nullness bugfix :: could not assign null to member val property (#17845)
1 parent 349a95d commit 3493c76

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.200.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix extension methods support for non-reference system assemblies ([PR #17799](https://github.com/dotnet/fsharp/pull/17799))
66
* Ensure `frameworkTcImportsCache` mutations are thread-safe. ([PR #17795](https://github.com/dotnet/fsharp/pull/17795))
77
* Fix concurrency issue in `ILPreTypeDefImpl` ([PR #17812](https://github.com/dotnet/fsharp/pull/17812))
8+
* Fix nullness inference for member val and other OO scenarios ([PR #17845](https://github.com/dotnet/fsharp/pull/17845))
89

910
### Added
1011

src/Compiler/TypedTree/TypedTree.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,6 +4358,12 @@ type NullnessVar() =
43584358

43594359
member nv.IsSolved = solution.IsSome
43604360

4361+
member nv.IsFullySolved =
4362+
match solution with
4363+
| None -> false
4364+
| Some (Nullness.Known _) -> true
4365+
| Some (Nullness.Variable v) -> v.IsFullySolved
4366+
43614367
member nv.Set(nullness) =
43624368
assert (not nv.IsSolved)
43634369
solution <- Some nullness

src/Compiler/TypedTree/TypedTree.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,7 @@ type NullnessVar =
31003100
member Evaluate: unit -> NullnessInfo
31013101
member TryEvaluate: unit -> NullnessInfo voption
31023102
member IsSolved: bool
3103+
member IsFullySolved: bool
31033104
member Set: Nullness -> unit
31043105
member Unset: unit -> unit
31053106
member Solution: Nullness

src/Compiler/TypedTree/TypedTreeBasics.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ let tryAddNullnessToTy nullnessNew (ty:TType) =
284284
let addNullnessToTy (nullness: Nullness) (ty:TType) =
285285
match nullness with
286286
| Nullness.Known NullnessInfo.WithoutNull -> ty
287-
| Nullness.Variable nv when nv.IsSolved && nv.Evaluate() = NullnessInfo.WithoutNull -> ty
287+
| Nullness.Variable nv when nv.IsFullySolved && nv.TryEvaluate() = ValueSome NullnessInfo.WithoutNull -> ty
288288
| _ ->
289289
match ty with
290290
| TType_var (tp, nullnessOrig) -> TType_var (tp, combineNullness nullnessOrig nullness)

tests/FSharp.Compiler.ComponentTests/Language/Nullness/NullableReferenceTypesTests.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ let nonStrictFunc(x:string | null) = strictFunc(x)
144144
|> shouldFail
145145
|> withDiagnostics [
146146
Error 3261, Line 4, Col 49, Line 4, Col 50, "Nullness warning: The types 'string' and 'string | null' do not have equivalent nullability."]
147+
148+
[<Fact>]
149+
let ``Can have nullable prop of same type T within a custom type T``() =
150+
FSharp """
151+
module MyLib
152+
type T () =
153+
let mutable v : T | null = null
154+
member val P : T | null = null with get, set
155+
member this.M() =
156+
v <- null
157+
this.P <- null
158+
"""
159+
|> asLibrary
160+
|> typeCheckWithStrictNullness
161+
|> shouldSucceed
147162

148163
[<Theory>]
149164
[<InlineData("fileExists(path)")>]

0 commit comments

Comments
 (0)