fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Detect and error on duplicate static extension member container names in the same module (same name, different namespaces)

Open Copilot opened this issue 5 months ago • 4 comments

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original description:

Context and Motivation

This PR introduces a new compile-time error to the F# compiler to prevent ambiguities when defining static extension members in the same module that extend types with the same simple name but from different namespaces. This is needed because the F# compiler currently emits all static extension members into a synthetic container with the simple name and a .Static suffix. If multiple types with the same simple name (but from different namespaces) are extended in the same module, this will result in a name clash in the generated IL, leading to confusing errors or runtime issues.

Related Issue

This change implements the guidance from the following draft issue:

Raise a compile error if static extension members in the same module extend types with the same simple name but from different namespaces, and tell the user to put them in different modules.

Implementation Steps

1. Where the Check Happens

  • The check is added to src/Compiler/Checking/PostInferenceChecks.fs, specifically within the CheckDefnInModule function. This function is responsible for running post-inference checks on module definitions after type checking is complete. (Ref: PostInferenceChecks.fs:2631-2695)

2. How the Check Works

  • A new function (CheckForDuplicateExtensionMemberNames) is added. It:
    • Collects all extension members defined in a module.
    • Groups them by the simple name of the type they extend.
    • For each group, checks if there are members extending different fully qualified types (i.e., different namespaces).
    • If such a conflict is found, emits a compiler error with a clear message to the user, instructing them to split their extensions into different modules.

3. Error Reporting

  • A new error message is added to src/Compiler/FSComp.txt:
    • Extension members extending types with the same name but different namespaces must be defined in separate modules. Consider moving one of the extension definitions to a different module.

4. Regression Test

  • A new test is added to tests/FSharp.Compiler.ComponentTests/StaticExtensions/DuplicateExtensionMemberName.fs (or similar location) to ensure that the error is raised when expected.
  • The test defines two extension members in the same module, each targeting a type with the same simple name but from different namespaces. It expects the new compile error.

5. Summary of Impact

  • This change causes the compiler to catch the ambiguity early, before code generation, presenting a clear diagnostic to the user and preventing confusing IL name clashes.

References

  • Code search: CheckDefnInModule logic in PostInferenceChecks.fs
  • Code search: Extension member construction and naming in IlxGen.fs
  • Related discussions: [user issue request]

The following files will be modified or added:

  • src/Compiler/Checking/PostInferenceChecks.fs: Add the check and call it from CheckDefnInModule.
  • src/Compiler/FSComp.txt: Add the new error message.
  • tests/FSharp.Compiler.ComponentTests/StaticExtensions/DuplicateExtensionMemberName.fs: Add regression test.

Please review the implementation and let us know if there are questions, or if further documentation is needed.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Aug 05 '25 14:08 Copilot

:heavy_exclamation_mark: Release notes required

@Copilot,

[!CAUTION] No release notes found for the changed paths (see table below).

Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format.

The following format is recommended for this repository:

* <Informative description>. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX))

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

You can open this PR in browser to add release notes: open in github.dev

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.0.md No release notes found or release notes format is not correct

github-actions[bot] avatar Aug 15 '25 08:08 github-actions[bot]

@copilot : Please proceed

T-Gro avatar Aug 15 '25 08:08 T-Gro

@T-Gro Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub.

You can ask me to try again later by mentioning me in a new comment.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 2010f003-e2ba-4723-bcca-5e804543cd4e

Sorry for the inconvenience!

Copilot avatar Aug 15 '25 08:08 Copilot

@copilot : Please proceed

T-Gro avatar Sep 22 '25 13:09 T-Gro