Skip to content

15.12 Static constructors (when extern) #158

Open
@gafter

Description

@gafter

See also dotnet/roslyn#34651

The specification requires an implementation to initialize static fields before calling the static constructor. However, existing implementations compile the field initializations into the body of the generated static constructor.

When the static constructor is itself declared extern, who is supposed to run the field initializers? Existing implementations simply produce no code for those initializations (and produce a warning on the static constructor declaration). Is this a bug in the compiler? Iif so, what could it possibly do instead? Or is this a bug in the (external) body of the static constructor, which should perform the initialization?

The current wording would seem to make the current behavior a compiler bug (that cannot be fixed).

To initialize a new closed class type, first a new set of static fields (§15.5.2) for that particular closed type is created. Each of the static fields is initialized to its default value (§15.5.5). Next, the static field initializers (§15.5.6.2) are executed for those static fields. Finally, the static constructor is executed.

I propose rewording to say that the static initializers are performed on entry to the static initializer (which is analogous to what we say for instance initializers vs instance constructors). Alternatively, or in addition, in the paragraph that describes the behavior for an external static constructor should say that it is responsible for executing the field initializers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugThe Standard does not describe the language as intended or implemented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions