Skip to content

Commit 60e11b3

Browse files
CopilotT-Gro
andcommitted
Fix duplicate .cctor issue by merging instruction bodies
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
1 parent 5e9d765 commit 60e11b3

File tree

1 file changed

+20
-1
lines changed
  • src/Compiler/AbstractIL

1 file changed

+20
-1
lines changed

src/Compiler/AbstractIL/il.fs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4118,7 +4118,26 @@ let cdef_cctorCode2CodeOrCreate tag imports f (cd: ILTypeDef) =
41184118
| [] ->
41194119
let body = mkMethodBody (false, [], 1, nonBranchingInstrsToCode [], tag, imports)
41204120
mkILClassCtor body
4121-
| _ -> failwith "bad method table: more than one .cctor found"
4121+
| multipleCctors ->
4122+
// Handle multiple .cctor methods by merging their instruction bodies
4123+
// Extract the instruction sequences from all .cctor methods (excluding the final 'ret')
4124+
let allInstrs =
4125+
multipleCctors
4126+
|> List.collect (fun mdef ->
4127+
match mdef.Body with
4128+
| MethodBody.IL(il) ->
4129+
let ilCode = il.Value.Code
4130+
// Remove the final 'ret' instruction and collect the rest
4131+
let instrs = ilCode.Instrs |> Array.toList
4132+
match List.rev instrs with
4133+
| I_ret :: rest -> List.rev rest
4134+
| _ -> instrs
4135+
| _ -> [])
4136+
4137+
// Create merged .cctor body with all instructions plus a single 'ret'
4138+
let mergedInstrs = allInstrs @ [I_ret]
4139+
let mergedBody = mkMethodBody (false, [], 8, nonBranchingInstrsToCode mergedInstrs, tag, imports)
4140+
mkILClassCtor mergedBody
41224141

41234142
let methods =
41244143
ILMethodDefs(fun () ->

0 commit comments

Comments
 (0)