-
Notifications
You must be signed in to change notification settings - Fork 832
Warn when obj is inferred #13298
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
Merged
Merged
Warn when obj is inferred #13298
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
4a24440
First pass at a warning when obj is inferred
Smaug123 ef5090f
Add more tests, some of which are failing
Smaug123 9e1c8c2
A couple more tests
Smaug123 4f0390c
Add test for simpler quotation case
Smaug123 4d48759
Turn diagnostic off by default
Smaug123 5794221
Add a test which is failing, no idea why
Smaug123 c09ac18
Update text
Smaug123 55fd658
Fix up messages
Smaug123 2200ddd
Merge main
Smaug123 b15bd8b
Restructure tests
Smaug123 97ffb68
Speculative commit to restore range information where required
Smaug123 ee81996
Another failing test
Smaug123 e8d64fa
Resolve merge
Smaug123 2235295
Merge main, sloppily
Smaug123 aa155b0
Merge main
Smaug123 030a5cb
Tidy
Smaug123 76ea258
Take 2
Smaug123 849a97f
Add another test
Smaug123 5bfef32
Merge main
Smaug123 25f3ba1
Add prospective test
Smaug123 248b7a5
Merge branch 'obj-inference-warning' into obj-inference-take-2
Smaug123 c47dce2
Merge branch 'main' into obj-inference-warning
Smaug123 c6242c6
Merge remote-tracking branch 'origin/main' into obj-inference-warning
Smaug123 286fe21
Fix build
Smaug123 49db33f
Adjust message
Smaug123 52dc05d
Add extra fallback typar ranges
Smaug123 88b7780
WIP extra tests
Smaug123 8485188
Add fallback range
Smaug123 609fe05
Revert
Smaug123 407040b
Reinstate
Smaug123 d6479c5
Merge branch 'add-fallback-range' into obj-inference-warning
Smaug123 a2e7f1e
More tests
Smaug123 adea31c
Merge branch 'main' into obj-inference-warning
Smaug123 5c991f9
Merge main
Smaug123 3ee4928
Downgrade warning
Smaug123 79e4de5
Merge branch 'obj-inference-warning' of github.com:Smaug123/fsharp-1 …
Smaug123 3356912
Fix tests after merge
Smaug123 d99753b
Merge branch 'main' into obj-inference-warning
Smaug123 862c87d
Gate the diagnostic
Smaug123 2a7d7d9
Fix error
Smaug123 5f4288d
Merge branch 'main' into obj-inference-warning
Smaug123 710bab0
Enable language version in tests
Smaug123 23601d5
Merge branch 'main' into obj-inference-warning
Smaug123 a055b18
Merge branch 'main' into obj-inference-warning
T-Gro ac53fb6
Markups
Smaug123 7cde8eb
Merge branch 'obj-inference-warning' of github.com:Smaug123/fsharp-1 …
Smaug123 c1cb455
Merge remote-tracking branch 'origin/main' into obj-inference-warning
Smaug123 f1404c0
Merge branch 'main' into obj-inference-warning
Smaug123 8beaa75
Merge branch 'main' into obj-inference-warning
Smaug123 5e99aae
Merge branch 'obj-inference-warning' of github.com:Smaug123/fsharp-1 …
Smaug123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/FSharp.Compiler.ComponentTests/ConstraintSolver/ObjInference.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| namespace FSharp.Compiler.ComponentTests.ConstraintSolver | ||
|
|
||
| open Xunit | ||
| open FSharp.Test.Compiler | ||
|
|
||
| module ObjInference = | ||
|
|
||
| let message = "A type has been implicitly inferred as 'obj', which may be unintended. Consider adding explicit type annotations. You can disable this warning by using '#nowarn \"3559\"' or '--nowarn:3559'." | ||
|
|
||
| let quotableWarningCases = | ||
| [ | ||
| """System.Object.ReferenceEquals(null, "hello") |> ignore""", 1, 31, 1, 35 | ||
| """System.Object.ReferenceEquals("hello", null) |> ignore""", 1, 40, 1, 44 | ||
| "([] = []) |> ignore", 1, 7, 1, 9 | ||
| "<@ [] = [] @> |> ignore", 1, 9, 1, 11 | ||
| "let _ = Unchecked.defaultof<_> in ()", 1, 29, 1, 30 | ||
| ] | ||
| |> List.map (fun (str, line1, col1, line2, col2) -> [| box str ; line1 ; col1 ; line2 ; col2 |]) | ||
|
|
||
| let unquotableWarningCases = | ||
| [ | ||
| "let f() = ([] = [])", 1, 17, 1, 19 | ||
| """let f<'b> (x : 'b) : int = failwith "" | ||
| let deserialize<'v> (s : string) : 'v = failwith "" | ||
| let x = deserialize "" |> f""", 3, 9, 3, 28 | ||
| "let f = typedefof<_>", 1, 19, 1, 20 | ||
| """let f<'b> () : 'b = (let a = failwith "" in unbox a)""", 1, 26, 1, 27 | ||
| ] | ||
| |> List.map (fun (str, line1, col1, line2, col2) -> [| box str ; line1 ; col1 ; line2 ; col2 |]) | ||
|
|
||
| let warningCases = | ||
| quotableWarningCases @ unquotableWarningCases | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(warningCases))>] | ||
| let ``Warning is emitted when type Obj is inferred``(code: string, line1: int, col1: int, line2: int, col2: int) = | ||
| FSharp code | ||
| |> withErrorRanges | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Information 3559, Line line1, Col col1, Line line2, Col col2, message) | ||
|
|
||
| let quotableNoWarningCases = | ||
| [ | ||
| "let a = 5 |> unbox<obj> in let b = a in ()" // explicit obj annotation | ||
| "let add x y = x + y in ()" // inferred as int | ||
| "let f() = ([] = ([] : obj list)) in ()" // obj is inferred, but is annotated | ||
| "let f() = (([] : obj list) = []) in ()" // obj is inferred, but is annotated | ||
| "let f () : int = Unchecked.defaultof<_> in ()" // explicitly int | ||
| "let f () = Unchecked.defaultof<int> in ()" // explicitly int | ||
| ] | ||
| |> List.map Array.singleton | ||
|
|
||
| let unquotableNoWarningCases = | ||
| [ | ||
| "let add x y = x + y" // inferred as int | ||
| "let inline add x y = x + y" // inferred with SRTP | ||
| "let inline add< ^T when ^T : (static member (+) : ^T * ^T -> ^T)> (x : ^T) (y : ^T) : ^T = x + y" // with SRTP | ||
| "let f x = string x" // inferred as generic 'a -> string | ||
| "let f() = ([] = ([] : obj list))" // obj is inferred, but is annotated | ||
| "let f() = (([] : obj list) = [])" // obj is inferred, but is annotated | ||
| """let x<[<Measure>]'m> : int<'m> = failwith "" | ||
| let f () = x = x |> ignore""" // measure is inferred as 1, but that's not covered by this warning | ||
| "let f () : int = Unchecked.defaultof<_>" // explicitly int | ||
| "let f () = Unchecked.defaultof<int>" // explicitly int | ||
| "let f () = Unchecked.defaultof<_>" // generic | ||
| ] | ||
| |> List.map Array.singleton | ||
|
|
||
| let noWarningCases = quotableNoWarningCases @ unquotableNoWarningCases | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(noWarningCases))>] | ||
| let ``Warning does not fire unless required``(code: string) = | ||
| FSharp code | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldSucceed | ||
|
|
||
| let nullNoWarningCases = | ||
| [ | ||
| """System.Object.ReferenceEquals("hello", (null: string))""" | ||
| """System.Object.ReferenceEquals((null: string), "hello")""" | ||
| ] | ||
| |> List.map Array.singleton | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(nullNoWarningCases))>] | ||
| let ``Don't warn on an explicitly annotated null``(expr: string) = | ||
| sprintf "%s |> ignore" expr | ||
| |> FSharp | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldSucceed | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(nullNoWarningCases))>] | ||
| let ``Don't warn on an explicitly annotated null, inside quotations``(expr: string) = | ||
| sprintf "<@ %s @> |> ignore" expr | ||
| |> FSharp | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldSucceed | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(quotableWarningCases))>] | ||
| let ``Warn also inside quotations of acceptable code``(expr: string, line1: int, col1: int, line2: int, col2: int) = | ||
| sprintf "<@ %s @> |> ignore" expr | ||
| |> FSharp | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Information 3559, Line line1, Col (col1 + 3), Line line2, Col (col2 + 3), message) | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(quotableNoWarningCases))>] | ||
| let ``Don't warn inside quotations of acceptable code``(expr: string) = | ||
| sprintf "<@ %s @> |> ignore" expr | ||
| |> FSharp | ||
| |> withWarnOn 3559 | ||
| |> withLangVersionPreview | ||
| |> typecheck | ||
| |> shouldSucceed | ||
|
|
||
| [<Theory>] | ||
| [<MemberData(nameof(warningCases))>] | ||
| let ``Warning is off by default``(expr: string, _: int, _: int, _: int, _: int) = | ||
| expr | ||
| |> FSharp | ||
| |> withLangVersionPreview | ||
| |> withOptions ["--warnaserror"] | ||
| |> typecheck | ||
| |> shouldSucceed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.