Better IL output for property/field initializers
- Dominant language
- F#
- Stars
- 4.3k
- Forks
- 876
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 131
Description
F#:
```fsharp
open System.Runtime.CompilerServices
type Test =
[]
val mutable X : int
new() = { }
[]
let test() =
Test(X = 1)
[]
let main _ =
0
```
Currently, F# compiler produces this IL:
```
.method public static class Program/Test
test() cil managed noinlining
{
// Code size 15 (0xf)
.maxstack 4
.locals init (class Program/Test V_0)
IL_0000: newobj instance void Program/Test::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ldc.i4.1
IL_0008: stfld int32 Program/Test::X
IL_000d: ldloc.0
IL_000e: ret
} // end of method Program::test
```
However, the IL could be simplified to:
```
.method public static class Program/Test
test() cil managed noinlining
{
.maxstack 4
IL_0xxx: newobj instance void Program/Test::.ctor()
IL_0xxx: dup
IL_0xxx: ldc.i4.1
IL_0xxx: stfld int32 Program/Test::X
IL_0xxx: ret
}
```
No locals are produced and we have less instructions emitted.
@kerams - pinging you as I know you are interested in this stuff :)
I have some ideas on how to tackle this, but there may be better approaches that I'm not aware of.
Contributor guide
Assessment
This issue has not been assessed yet.