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

Nullness :: reduce false warnings, do not infer a value to be (string|null) when used within 'printfn "%s"' #16681

Merged
merged 3 commits into from
Mar 4, 2024
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
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
Loading