Skip to content

Fix missing semicolon on non-const global struct initializers#726

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-global-struct-init-semicolon
Jul 13, 2026
Merged

Fix missing semicolon on non-const global struct initializers#726
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-global-struct-init-semicolon

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #503.

A non-const global variable initialized with a struct initializer list was emitted with a closing } but no terminating ;, which broke the rest of the generated file:

public static Point MyGlobalPoint = new Point
{
    x = 10,
    y = 20,
}   // <-- missing ';'

Root cause

CSharpOutputBuilder.EndValue only wrote the terminating semicolon for ValueKind.Unmanaged when desc.IsConstant was true. const globals (emitted as static readonly) and arrays (flagged constant via IsTypeConstantOrIncompleteArray) therefore terminated correctly, but a plain non-const global struct fell through and emitted nothing. ForRecordType had already set NeedsSemicolon, but nothing flushed it on this path.

Fix

Emit the semicolon for the non-const case as well, matching the constant branch and the Primitive/String/GuidMember value kinds.

XML output is unaffected -- XmlOutputBuilder.EndValue closes <value> unconditionally. The fix is language/platform-invariant (the struct renders identically across all C# variants), so a single ClangSharp.PInvokeGenerator.UnitTests regression test (GlobalStructInitializerTest) covers it; generate-helper-types is not involved.

Validation

  • Build: 0 warnings / 0 errors (Release).
  • Tests: 3709 passed, 0 failed (3708 existing byte-identical + 1 new).

Note

This PR description and the change were drafted with Copilot.

The EndValue path for ValueKind.Unmanaged only emitted the terminating
semicolon when the value was constant, so a non-const global initialized
with a struct initializer list (e.g. Point p = { .x = 10, .y = 20 };) was
emitted as a closing '}' with no ';', corrupting the rest of the file.
Emit the semicolon for the non-const case as well, matching the constant
path and the primitive/string/guid value kinds.

Fixes dotnet#503

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding tannergooding merged commit 0a52e43 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding tannergooding deleted the fix-global-struct-init-semicolon branch July 13, 2026 04:57
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

Successfully merging this pull request may close these issues.

Struct initialization in global variable emits invalid C#

1 participant