Skip to content

Commit 4f0c320

Browse files
T-Groaboniepsfinaki
authored
Warn when 'unit' is passed to an 'obj' argument (#18330)
* Warn when 'unit' is passed to an 'obj' argument * fantomas of the opera * notes + fix build of tests * Apply suggestions from code review Co-authored-by: Adam Boniecki <[email protected]> * Address test warnings * Fix tests * revert to stable warning when arg is other than obj --------- Co-authored-by: Adam Boniecki <[email protected]> Co-authored-by: Petr <[email protected]>
1 parent 3397985 commit 4f0c320

File tree

27 files changed

+188
-6
lines changed

27 files changed

+188
-6
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Update `Obsolete` attribute checking to account for `DiagnosticId` and `UrlFormat` properties. ([PR #18224](https://github.com/dotnet/fsharp/pull/18224))
2727
* Remove `Cancellable.UsingToken` from tests ([PR #18276](https://github.com/dotnet/fsharp/pull/18276))
2828
* Added nullability annotations to `.Using` builder method for `async`, `task` and compiler-internal builders ([PR #18292](https://github.com/dotnet/fsharp/pull/18292))
29+
* Warn when `unit` is passed to an `obj`-typed argument ([PR #18330](https://github.com/dotnet/fsharp/pull/18330))
2930
* Warning for "useless null handling" works with piped syntax constructs now ([PR #18331](https://github.com/dotnet/fsharp/pull/18331))
3031

3132
### Breaking Changes

docs/release-notes/.Language/preview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Deprecate places where `seq` can be omitted. ([Language suggestion #1033](https://github.com/fsharp/fslang-suggestions/issues/1033), [PR #17772](https://github.com/dotnet/fsharp/pull/17772))
55
* Added type conversions cache, only enabled for compiler runs ([PR#17668](https://github.com/dotnet/fsharp/pull/17668))
66
* Support ValueOption + Struct attribute as optional parameter for methods ([Language suggestion #1136](https://github.com/fsharp/fslang-suggestions/issues/1136), [PR #18098](https://github.com/dotnet/fsharp/pull/18098))
7+
* Warn when `unit` is passed to an `obj`-typed argument ([PR #18330](https://github.com/dotnet/fsharp/pull/18330))
78

89
### Fixed
910

src/Compiler/Checking/ConstraintSolver.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,7 @@ and ArgsMustSubsumeOrConvert
31773177
trackErrors {
31783178
let g = csenv.g
31793179
let m = callerArg.Range
3180+
let callerTy = callerArg.CallerArgumentType
31803181
let calledArgTy, usesTDC, eqn = AdjustCalledArgType csenv.InfoReader ad isConstraint enforceNullableOptionalsKnownTypes calledArg callerArg
31813182

31823183
match eqn with
@@ -3188,8 +3189,10 @@ and ArgsMustSubsumeOrConvert
31883189
match usesTDC with
31893190
| TypeDirectedConversionUsed.Yes(warn, _, _) -> do! WarnD(warn csenv.DisplayEnv)
31903191
| TypeDirectedConversionUsed.No -> ()
3191-
do! SolveTypeSubsumesTypeWithReport csenv ndeep m trace cxsln (Some calledArg.CalledArgumentType) calledArgTy callerArg.CallerArgumentType
3192-
if calledArg.IsParamArray && isArray1DTy g calledArgTy && not (isArray1DTy g callerArg.CallerArgumentType) then
3192+
do! SolveTypeSubsumesTypeWithReport csenv ndeep m trace cxsln (Some calledArg.CalledArgumentType) calledArgTy callerTy
3193+
if g.langVersion.SupportsFeature(LanguageFeature.WarnWhenUnitPassedToObjArg) && isUnitTy g callerTy && isObjTyAnyNullness g calledArgTy then
3194+
do! WarnD(Error(FSComp.SR.tcUnitToObjSubsumption(), m))
3195+
if calledArg.IsParamArray && isArray1DTy g calledArgTy && not (isArray1DTy g callerTy) then
31933196
return! ErrorD(Error(FSComp.SR.csMethodExpectsParams(), m))
31943197
else
31953198
return usesTDC

src/Compiler/FSComp.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,7 @@ forFormatInvalidForInterpolated4,"Interpolated strings used as type IFormattable
16831683
3394,parsNewExprMemberAccess,"This member access is ambiguous. Please use parentheses around the object creation, e.g. '(new SomeType(args)).MemberName'"
16841684
3395,tcImplicitConversionUsedForMethodArg,"This expression uses the implicit conversion '%s' to convert type '%s' to type '%s'."
16851685
3396,tcLiteralAttributeCannotUseActivePattern,"A [<Literal>] declaration cannot use an active pattern for its identifier"
1686+
3397,tcUnitToObjSubsumption,"This expression uses 'unit' for an 'obj'-typed argument. This will lead to passing 'null' at runtime. This warning may be disabled using '#nowarn \"3397\"."
16861687
3401,ilxgenInvalidConstructInStateMachineDuringCodegen,"The resumable code construct '%s' may only be used in inlined code protected by 'if __useResumableCode then ...' and the overall composition must form valid resumable code."
16871688
3402,tcInvalidResumableConstruct,"The construct '%s' may only be used in valid resumable code."
16881689
3501,tcResumableCodeFunctionMustBeInline,"Invalid resumable code. Any method of function accepting or returning resumable code must be marked 'inline'"
@@ -1794,3 +1795,4 @@ featureDontWarnOnUppercaseIdentifiersInBindingPatterns,"Don't warn on uppercase
17941795
3874,tcExpectedTypeParamMarkedWithUnitOfMeasureAttribute,"Expected unit-of-measure type parameter must be marked with the [<Measure>] attribute."
17951796
featureDeprecatePlacesWhereSeqCanBeOmitted,"Deprecate places where 'seq' can be omitted"
17961797
featureSupportValueOptionsAsOptionalParameters,"Support ValueOption as valid type for optional member parameters"
1798+
featureSupportWarnWhenUnitPassedToObjArg,"Warn when unit is passed to a member accepting `obj` argument, e.g. `Method(o:obj)` will warn if called via `Method()`."

src/Compiler/Facilities/LanguageFeatures.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type LanguageFeature =
9898
| UseTypeSubsumptionCache
9999
| DeprecatePlacesWhereSeqCanBeOmitted
100100
| SupportValueOptionsAsOptionalParameters
101+
| WarnWhenUnitPassedToObjArg
101102

102103
/// LanguageVersion management
103104
type LanguageVersion(versionText) =
@@ -227,6 +228,7 @@ type LanguageVersion(versionText) =
227228
LanguageFeature.DontWarnOnUppercaseIdentifiersInBindingPatterns, previewVersion
228229
LanguageFeature.DeprecatePlacesWhereSeqCanBeOmitted, previewVersion
229230
LanguageFeature.SupportValueOptionsAsOptionalParameters, previewVersion
231+
LanguageFeature.WarnWhenUnitPassedToObjArg, previewVersion
230232
]
231233

232234
static let defaultLanguageVersion = LanguageVersion("default")
@@ -388,6 +390,7 @@ type LanguageVersion(versionText) =
388390
| LanguageFeature.UseTypeSubsumptionCache -> FSComp.SR.featureUseTypeSubsumptionCache ()
389391
| LanguageFeature.DeprecatePlacesWhereSeqCanBeOmitted -> FSComp.SR.featureDeprecatePlacesWhereSeqCanBeOmitted ()
390392
| LanguageFeature.SupportValueOptionsAsOptionalParameters -> FSComp.SR.featureSupportValueOptionsAsOptionalParameters ()
393+
| LanguageFeature.WarnWhenUnitPassedToObjArg -> FSComp.SR.featureSupportWarnWhenUnitPassedToObjArg ()
391394

392395
/// Get a version string associated with the given feature.
393396
static member GetFeatureVersionString feature =

src/Compiler/Facilities/LanguageFeatures.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type LanguageFeature =
8989
| UseTypeSubsumptionCache
9090
| DeprecatePlacesWhereSeqCanBeOmitted
9191
| SupportValueOptionsAsOptionalParameters
92+
| WarnWhenUnitPassedToObjArg
9293

9394
/// LanguageVersion management
9495
type LanguageVersion =

src/Compiler/xlf/FSComp.txt.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)