Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 5, 2025

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 Copilot AI assigned Copilot and T-Gro Aug 5, 2025
@T-Gro T-Gro assigned Copilot and unassigned Copilot and T-Gro Aug 15, 2025
@T-Gro T-Gro closed this Aug 15, 2025
@dotnet dotnet deleted a comment from Copilot AI Aug 15, 2025
@T-Gro T-Gro reopened this Aug 15, 2025
@T-Gro T-Gro closed this Aug 15, 2025
@T-Gro T-Gro deleted the copilot/fix-fc81f659-8de7-40f1-a95e-1a8d108600fa branch August 15, 2025 08:14
@T-Gro T-Gro restored the copilot/fix-fc81f659-8de7-40f1-a95e-1a8d108600fa branch August 15, 2025 08:14
@T-Gro T-Gro reopened this Aug 15, 2025
@T-Gro T-Gro changed the title [WIP] Detect and error on duplicate static extension member container names in the same module (same simple name, different namespaces) [WIP] Detect and error on duplicate static extension member container names in the same module (same name, different namespaces) Aug 15, 2025
Copy link
Contributor

✅ No release notes required

@T-Gro
Copy link
Member

T-Gro commented Aug 15, 2025

@copilot : Please proceed

Copy link
Contributor Author

Copilot AI commented Aug 15, 2025

@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!

Sign up for free to 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.

2 participants