Skip to content

Commit

Permalink
Improve type inference w.r.t. nullness in printing (#16681)
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Gro authored Mar 4, 2024
1 parent 6827b1c commit ba69485
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Compiler/Checking/CheckFormatStrings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ let parseFormatStringInternal
checkOtherFlags ch
collectSpecifierLocation fragLine fragCol 1
let i = skipPossibleInterpolationHole (i+1)
let stringTy = if g.checkNullness && g.langFeatureNullness then g.string_ty_withNull else g.string_ty
let stringTy = if g.checkNullness && g.langFeatureNullness then g.string_ty_ambivalent else g.string_ty
parseLoop ((posi, stringTy) :: acc) (i, fragLine, fragCol+1) fragments

| 'O' ->
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/TypedTree/TcGlobals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ type TcGlobals(
let v_FormattableStringFactory_tcref = findSysTyconRef sysCompilerServices "FormattableStringFactory"
let v_FormattableStringFactory_ty = mkNonGenericTy v_FormattableStringFactory_tcref
let v_string_ty = mkNonGenericTy v_string_tcr
let v_string_ty_withNull = mkNonGenericTyWithNullness v_string_tcr KnownWithNull
let v_string_ty_ambivalent = mkNonGenericTyWithNullness v_string_tcr KnownAmbivalentToNull
let v_decimal_ty = mkSysNonGenericTy sys "Decimal"
let v_unit_ty = mkNonGenericTy v_unit_tcr_nice
let v_system_Type_ty = mkSysNonGenericTy sys "Type"
Expand Down Expand Up @@ -1341,7 +1341,7 @@ type TcGlobals(
member _.bool_ty = v_bool_ty
member _.int_ty = v_int_ty
member _.string_ty = v_string_ty
member _.string_ty_withNull = v_string_ty_withNull
member _.string_ty_ambivalent = v_string_ty_ambivalent
member _.system_IFormattable_tcref = v_IFormattable_tcref
member _.system_FormattableString_tcref = v_FormattableString_tcref
member _.system_FormattableStringFactory_tcref = v_FormattableStringFactory_tcref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let maybeNull : string | null = null
let nonNullString = "abc"
let printedValueNotNull = sprintf "This is not null: %s" nonNullString
let printedValueNull = sprintf "This is null: %s" maybeNull
let interpolated = $"This is fine {maybeNull}"
let interpolated = $"This is fine %s{maybeNull}"
let interpolatedAnnotatedNotNull = $"This is fine %s{nonNullString}"
let interpolatedAnnotatedNullable = $"This is not null %s{maybeNull}"
let interpolateNullLiteral = $"This is not null %s{null}"
Expand Down Expand Up @@ -96,6 +96,19 @@ let printViaA = sprintf "This is null: %A and this has null inside %A" maybeArra
|> typeCheckWithStrictNullness
|> shouldSucceed

[<Fact>]
let ``Type inference with sprintfn`` () =
FSharp """module MyLibrary
let needsString(x:string) = ()
let myTopFunction inferedVal =
printfn "This is it %s" inferedVal // There was a regression infering this to be (string | null)
needsString inferedVal
"""
|> asLibrary
|> typeCheckWithStrictNullness
|> shouldSucceed

[<Fact>]
let ``WhatIf the format itself is null`` () =
FSharp """module MyLibrary
Expand Down

0 comments on commit ba69485

Please sign in to comment.