Skip to content

Warn when 'unit' is passed to an 'obj' argument - #18330

Merged
psfinaki merged 9 commits into
mainfrom
18314-type-mismatched-defaultparametervalue-is-not-ignored-enough
Feb 24, 2025
Merged

Warn when 'unit' is passed to an 'obj' argument#18330
psfinaki merged 9 commits into
mainfrom
18314-type-mismatched-defaultparametervalue-is-not-ignored-enough

Conversation

@T-Gro

Copy link
Copy Markdown
Member

Fixes#18314 .

This code printed null when executed:

openSystem.Runtime.InteropServicestypeC=static memberFoo([<Optional>][<DefaultParameterValue("hello")>]obj:obj)=
printfn "%A" obj
C.Foo()

The root cause of the linked issue has not been in the usage of C#-style optionality.
There is a warning about the mismatch between obj and [<DefaultParameterValue("hello")>], and C#-style optionality is ignored. The member remains a 1-argument method.

However, the method application C.Foo() is then treated as if passing (), the unit argument, to an obj expecting method.
Which fits the .NET inheritance model, and unit becomes null at runtime.

This PR adds a new languageFeature-guarded warning to notify about () being subsumed with obj in method argument application.

@T-GroT-Gro linked an issue Feb 20, 2025 that may be closed by this pull request
@T-Gro
T-Gro marked this pull request as ready for review February 20, 2025 10:51
@T-Gro
T-Gro requested a review from a team as a code ownerFebruary 20, 2025 10:51
@github-actions

github-actionsBot commented Feb 20, 2025

Copy link
Copy Markdown
Contributor

❗ Release notes required


✅ Found changes and release notes in following paths:

Change pathRelease notes pathDescription
src/Compilerdocs/release-notes/.FSharp.Compiler.Service/9.0.300.md
LanguageFeatures.fsidocs/release-notes/.Language/preview.md

Comment threadtests/FSharp.Core.UnitTests/FSharp.Core/OperatorsModule1.fs Outdated
Comment threadtests/FSharp.Core.UnitTests/FSharp.Core/OperatorsModule2.fs Outdated
@psfinaki
psfinaki enabled auto-merge (squash) February 24, 2025 14:00
@psfinaki
psfinaki merged commit 4f0c320 into mainFeb 24, 2025
@T-Gro
T-Gro deleted the 18314-type-mismatched-defaultparametervalue-is-not-ignored-enough branch February 24, 2025 15:33
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Type-mismatched [<DefaultParameterValue>] is not ignored enough

3 participants

@T-Gro@psfinaki@abonie