Skip to content

Commit

Permalink
Fix issue 10505 --- string function returns null on null paramete…
Browse files Browse the repository at this point in the history
…r of type `string`.
  • Loading branch information
KevinRansom authored and laenas committed Dec 1, 2020
1 parent f514951 commit 30777b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/fsharp/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4509,7 +4509,10 @@ namespace Microsoft.FSharp.Core
[<CompiledName("ToString")>]
let inline string (value: 'T) =
anyToString "" value
when 'T : string = (# "" value : string #) // force no-op

when 'T : string =
if value = unsafeDefault<'T> then ""
else (# "" value : string #) // force no-op

// Using 'let x = (# ... #) in x.ToString()' leads to better IL, without it, an extra stloc and ldloca.s (get address-of)
// gets emitted, which are unnecessary. With it, the extra address-of-variable is not created
Expand Down
14 changes: 14 additions & 0 deletions tests/FSharp.Core.UnitTests/FSharp.Core/OperatorsModule2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ type OperatorsModule2() =

[<Fact>]
member _.string() =

let result = Operators.string null
Assert.AreEqual("", result)

let nullStr:string = null
let result = Operators.string nullStr
Assert.AreEqual("", result)

let result = Operators.string null
Assert.AreEqual("", result)

let result = Operators.string (null:string)
Assert.AreEqual("", result)

// value type
let result = Operators.string 100
Assert.AreEqual("100", result)
Expand Down

0 comments on commit 30777b1

Please sign in to comment.