-
Notifications
You must be signed in to change notification settings - Fork 21
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
Compiler warning when obj
is inferred as a type
#696
Comments
I know where you're coming from with this one. But wouldn't you typically catch this as an error almost immediately afterwards when you try to bind the result to e.g. input of another function that expects a value of a specific type? |
Typically, yes. However, in the example above, where we pass into |
I agree with the spirit of this - just today, in fact, I was helping someone with their code constraining to However, this is a breaking change, and there are some valid use cases for |
@cartermp This is why I propose making the warning be off by default: so that users must opt into the breaking change. |
I looked at this, by the way, and got something "working" (caught a couple of bugs in our existing codebase!) However, there turns out to be some information dropped by the compiler related to inferred type variables: in some situations, the compiler will simply forget where in the code the type was inferred. This means you very often just get a warning of the form "Inferred obj somewhere in this .fsproj", with no further information like file or line number. That makes this all but useless in practice. (I asked Don about this, and he said this was a perf optimisation at some point in the past, but that it was unclear whether the optimisation is actually needed.) I hacked the extra information in, in some situations, but never had the time to find a real solution to this. There are also a couple of instances where the warning is completely unhelpful, primarily inside quotations (where every generic is instantiated to obj anyway). The compiler itself won't build with this warning enabled as an error, since there is some automatically generated code somewhere in the process which contains an explicit use of |
@Smaug123, awesome that you're working on this! I've been coaching a team of programmers (relatively) new to F# and this is one of those cases that confuses the heck out of them. Adding that warning would certainly help alleviate some of that burden. |
The PR is sort of ready, except that it has a few quirks.
|
I spoke too soon: it works only by coincidence. In fact it's broken in the presence of SRTPs (e.g. Maybe the false positives arising from this are acceptable? |
An error message is now reserved for this (dotnet/fsharp#14642). |
I propose we create a compiler warning, off-by-default, that warns when the typechecker infers a type to be
obj
without annotation.Ideally, if there is some reason to expect that something is of type
obj
, then we would not warn. For example, the following code should not generate a warning, becausea
is constrained to beobj
by the type argument tounbox
, andb
is constrained to beobj
because it is of the same type asa
.However, we would warn in the following case, where
a
is inferred to beobj
because that's the most general thing it could be:The existing way of approaching this problem in F# is to ignore it and assume that you intended
obj
wheneverobj
is inferred (unless there's some other reason to think that you didn't mean it, such as unnecessary specialisation of a generic type as indicated by FS0064).Pros and Cons
The advantages of making this adjustment to F# are that it causes us to catch a class of bug we currently can't catch. I have never encountered an instance where the compiler has inferred
obj
and my code was correct; usingobj
at all is sufficiently weird that it seems reasonable to expect it to be signposted with a type annotation somewhere (much likemutable
is a required signpost for mutability). However, since this is a feature of F#, it seems reasonable to disable the warning by default.The disadvantages of making this adjustment to F# are a slight increase in complexity during typecheck, since we need to work out whether a type has been specialised.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: none
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: