Skip to content

Instance initializers performed in extern instance constructors #1253

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
jjonescz opened this issue Jan 17, 2025 · 1 comment
Open

Instance initializers performed in extern instance constructors #1253

jjonescz opened this issue Jan 17, 2025 · 1 comment
Assignees

Comments

@jjonescz
Copy link
Member

jjonescz commented Jan 17, 2025

Spec of instance variable initializers (§15.11.3) does not seem to exclude extern constructors, i.e., it sounds like extern constructors should perform variable initializers as well.

I think the spec should exclude extern constructors from the wording - the compiler cannot perform the initializers when an extern constructor is used since it doesn't control its code. (Indeed, the roslyn implementation doesn't perform the initializers.)

Note also that if you have a single constructor which is extern, then all variable initializers are not emitted anywhere, so they are useless. But there is no warning about that - should one be added to the language? For example:

class C
{
    int X = 5; // the initializer has no effect on the produced assembly

    extern C();
}

Related issue for static extern constructors: #158

@jskeet jskeet added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jan 17, 2025
@jskeet
Copy link
Contributor

jskeet commented Jan 22, 2025

After discussion... we can't require the extern constructor to do anything in particular; we could potentially warn if there is a field initializer in a type which only has extern constructors. (There are reasons why you might want to have field initializers and extern constructors, if you also have normal constructors.)

We think we don't need to specify such a warning though, as warnings are generally non-normative.

But we agree that 15.11.3 should exclude requirements for extern constructors. I'll see how minimally we can make this change, and try to cover #158 at the same time.

@jskeet jskeet self-assigned this Jan 22, 2025
@RexJaeschke RexJaeschke removed the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jan 22, 2025
jskeet added a commit to jskeet/csharpstandard that referenced this issue Apr 15, 2025
jskeet added a commit that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants