Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 31, 2025

  • Fix FS2014 duplicate .cctor error by renaming and delegating static constructors
  • Updated cdef_cctorCode2CodeOrCreate function in il.fs to handle multiple .cctor methods
  • Created delegating .cctor that calls renamed static constructors
  • Added test case in Misc.fs to verify the fix generates proper IL
  • Applied fantomas code formatting
  • Removed unused DuplicateCctorFix.fs file (test code is embedded in Misc.fs)

Problem Resolved

Fixed discriminated unions with both static members and nullary cases failing compilation with "FS2014: duplicate entry '.cctor' in method table". The solution renames duplicate .cctor methods to unique static methods and creates a single .cctor that calls both, preserving all initialization semantics while ensuring only one .cctor exists in the final IL.

Changes Made

  • Modified cdef_cctorCode2CodeOrCreate function to detect and rename duplicate .cctor methods
  • Uses generic naming pattern (cctor_renamed_0, cctor_renamed_1) to avoid hardcoded assumptions
  • Generated IL includes single .cctor method marked as rtspecialname that delegates to renamed methods
  • All original initialization logic preserved through clean delegation pattern

This pull request was created as a result of the following prompt from Copilot chat.

Merge duplicate static constructors (.cctor) for discriminated unions with generic statics in F# compiler backend. When assembling ILTypeDef for a type, if multiple .cctor methods are present (one from incremental class typechecking, one from union erasure), rename each to a unique static method (e.g., cctor_IncrClass, cctor_UnionErasure), then synthesize a single .cctor that calls both. Ensure only one .cctor is marked as rtspecialname and static constructor. Add a test using verifyIL to assert that the generated IL contains a single .cctor which calls both renamed initializers. Example IL:
.method private specialname rtspecialname static void .cctor() {
call void cctor_IncrClass()
call void cctor_UnionErasure()
ret
}
.method private static void cctor_IncrClass() { ... }
.method private static void cctor_UnionErasure() { ... }
This resolves #18767.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Merge duplicate .cctor methods for DUs with generic statics and ensure single static constructor Fix FS2014 duplicate .cctor error for discriminated unions with generic statics Jul 31, 2025
Copilot AI requested a review from T-Gro July 31, 2025 10:31
Copilot finished work on behalf of T-Gro July 31, 2025 10:31
Copilot AI changed the title Fix FS2014 duplicate .cctor error for discriminated unions with generic statics Fix FS2014 duplicate .cctor error by renaming and delegating static constructors Jul 31, 2025
Copilot AI requested a review from T-Gro July 31, 2025 15:29
Copilot finished work on behalf of T-Gro July 31, 2025 15:29
Copilot finished work on behalf of T-Gro August 4, 2025 10:51
Copilot AI requested a review from T-Gro September 8, 2025 15:06
Copilot finished work on behalf of T-Gro September 8, 2025 15:06
@T-Gro
Copy link
Member

T-Gro commented Sep 18, 2025

/run fantomas

@github-actions
Copy link
Contributor

❗ Release notes required


✅ Found changes and release notes in following paths:

Warning

No PR link found in some release notes, please consider adding it.

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/10.0.100.md No current pull request URL (#18801) found, please consider adding it

@github-actions
Copy link
Contributor

🔧 CLI Command Report

  • Command: /run fantomas
  • Outcome: success

✅ Patch applied:
- Files changed: 1
- Lines changed: 47

@T-Gro T-Gro marked this pull request as ready for review September 19, 2025 08:53
@T-Gro T-Gro requested a review from a team as a code owner September 19, 2025 08:53
@T-Gro T-Gro enabled auto-merge (squash) September 19, 2025 08:53
auto-merge was automatically disabled September 19, 2025 10:26

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Sep 19, 2025

/run fantomas

Done. Applied fantomas formatting in commit 063261c.

@T-Gro T-Gro merged commit 984837b into main Sep 19, 2025
38 checks passed
@T-Gro T-Gro deleted the copilot/fix-3269fd31-514e-486d-924d-1ed90880afed branch September 19, 2025 14:01
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.

FS2014 duplicate entry '.cctor' in method table

4 participants