Skip to content

15.12 Static constructors (when extern) #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gafter opened this issue Apr 1, 2019 · 4 comments
Open

15.12 Static constructors (when extern) #158

gafter opened this issue Apr 1, 2019 · 4 comments
Labels
type: bug The Standard does not describe the language as intended or implemented

Comments

@gafter
Copy link
Member

gafter commented Apr 1, 2019

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.

@Nigel-Ecma
Copy link
Contributor

Nigel-Ecma commented Apr 3, 2019 via email

@gafter
Copy link
Member Author

gafter commented Apr 9, 2019

@Nigel-Ecma Do you believe there are some C# implementations that obey the specification in this area, in a way that the Roslyn compiler does not? Unless there is, I would prefer that we enshrine existing industry practice rather than describing behavior that exists nowhere. It would be a breaking change to our customers to change what IL method they need to implement externally when a static constructor is declared extern.

@Nigel-Ecma
Copy link
Contributor

Nigel-Ecma commented Apr 9, 2019 via email

@jskeet jskeet transferred this issue from another repository Jan 11, 2021
@jskeet jskeet added the type: bug The Standard does not describe the language as intended or implemented label Jan 11, 2021
@jskeet
Copy link
Contributor

jskeet commented Jan 11, 2021

Related: #132, #73, #152

(We're getting a little cluster of these type initialization bugs...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug The Standard does not describe the language as intended or implemented
Projects
None yet
Development

No branches or pull requests

3 participants