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

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

T-Gro
Copy link
Member

@T-Gro T-Gro commented Feb 20, 2025

Fixes #18314 .

This code printed null when executed:

open System.Runtime.InteropServices

type C =
  static member Foo([<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-Gro T-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 owner February 20, 2025 10:51
Copy link
Contributor

github-actions bot commented Feb 20, 2025

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
LanguageFeatures.fsi docs/release-notes/.Language/preview.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

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