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

String operator always returning nonNull string #17809

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/release-notes/.FSharp.Core/9.0.200.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
### Fixed

* Fix exception on Post after MailboxProcessor was disposed ([Issue #17849](https://github.com/dotnet/fsharp/issues/17849), [PR #17922](https://github.com/dotnet/fsharp/pull/17922))

### Added

### Changed

### Breaking Changes
### Fixed

* Fix exception on Post after MailboxProcessor was disposed ([Issue #17849](https://github.com/dotnet/fsharp/issues/17849), [PR #17922](https://github.com/dotnet/fsharp/pull/17922))

### Added

### Changed
* String function changed to guarantee a non-null string return type ([PR #17809](https://github.com/dotnet/fsharp/pull/17809))


### Breaking Changes

15 changes: 10 additions & 5 deletions src/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,16 @@ namespace Microsoft.FSharp.Core
for m = 0 to len4 - 1 do
SetArray4D dst (src1+i) (src2+j) (src3+k) (src4+m) (GetArray4D src i j k m)

let inline defaultIfNull (defaultStr:string) x =
match x with
| null -> defaultStr
| nonNullString -> nonNullString

let inline anyToString nullStr x =
match box x with
| :? IFormattable as f -> f.ToString(null, CultureInfo.InvariantCulture)
| :? IFormattable as f -> defaultIfNull nullStr (f.ToString(null, CultureInfo.InvariantCulture))
| null -> nullStr
| _ -> x.ToString()
| _ -> defaultIfNull nullStr (x.ToString())

let anyToStringShowingNull x = anyToString "null" x

Expand Down Expand Up @@ -5200,8 +5205,8 @@ namespace Microsoft.FSharp.Core
when 'T : Guid = let x = (# "" value : Guid #) in x.ToString(null, CultureInfo.InvariantCulture)
when 'T struct =
match box value with
| :? IFormattable as f -> f.ToString(null, CultureInfo.InvariantCulture)
| _ -> value.ToString()
| :? IFormattable as f -> defaultIfNull "" (f.ToString(null, CultureInfo.InvariantCulture))
| _ -> defaultIfNull "" (value.ToString())

// other common mscorlib reference types
when 'T : StringBuilder =
Expand All @@ -5210,7 +5215,7 @@ namespace Microsoft.FSharp.Core

when 'T : IFormattable =
if value = unsafeDefault<'T> then ""
else let x = (# "" value : IFormattable #) in x.ToString(null, CultureInfo.InvariantCulture)
else let x = (# "" value : IFormattable #) in defaultIfNull "" (x.ToString(null, CultureInfo.InvariantCulture))

[<NoDynamicInvocation(isLegacy=true)>]
[<CompiledName("ToChar")>]
Expand Down
9 changes: 9 additions & 0 deletions tests/FSharp.Core.UnitTests/FSharp.Core/OperatorsModule2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@ type OperatorsModule2() =
let result = Operators.string 123.456M
Assert.AreEqual("123.456", result)

let result = Operators.string { new obj () with override _.ToString () = null }
Assert.AreEqual("", result)

let result = Operators.string { new obj () with override _.ToString () = Operators.string null }
Assert.AreEqual("", result)

let result = Operators.string { new IFormattable with override _.ToString (_, _) = null }
Assert.AreEqual("", result)

// Following tests ensure that InvariantCulture is used if type implements IFormattable

// safe current culture, then switch culture
Expand Down
Loading