KEMBAR78
NativeAOT: Type is not pre-initialized · Issue #83043 · dotnet/runtime · GitHub
Skip to content

NativeAOT: Type is not pre-initialized #83043

@EgorBo

Description

@EgorBo
struct MyStruct
{
    public string Name;
}

class Program
{
    static readonly MyStruct MyStr = new() { Name = "Hey!" };

    [MethodImpl(MethodImplOptions.NoInlining)]
    static MyStruct Test() => MyStr;
}

Codegen for Test():

; Method Program:Test():Program+MyStruct
       4883EC28             sub      rsp, 40
       E800000000           call     CORINFO_HELP_READYTORUN_GCSTATIC_BASE
       488B4010             mov      rax, gword ptr [rax+10H]
       4883C428             add      rsp, 40
       C3                   ret      
; Total bytes of code: 18

It seems that _compilation.HasLazyStaticConstructor(field.OwningType) returns true for Program so MyStr field is not pre-computed despite being a simple struct + frozen string literal.

cc @MichalStrehovsky

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions