Registry Mirror
Skip to content

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #803

A nullptr macro has pointer type, so it's classified as ValueKind.Unmanaged. Under GenerateUnmanagedConstants that routed it through the ref readonly constant path, but null has no backing storage to return by reference -- the getter body emitted a bare, mis-indented null and produced uncompilable code:

public static ref readonly void* MY_NULL_HANDLE
{
    get
    {
null    }
}

A null pointer literal is effectively a copied scalar value, so treat it like the primitive-copy constants already do and emit an expression-bodied member instead:

[NativeTypeName("#define MY_NULL_HANDLE nullptr")]
public static void* MY_NULL_HANDLE => null;

  • PInvokeGenerator.VisitVarDecl.cs -- mark the value ValueFlags.Copy when the initializer is a null pointer literal.
  • CSharpOutputBuilder.VisitDecl.cs -- gate the ref readonly/ReadOnlySpan type-writing on !desc.IsCopy and suppress the readonly modifier for the copy-under-unmanaged-constants case, mirroring the existing Primitive copy handling.

Adds NullPtrGenerateUnmanagedConstantsMacroTest as a regression test with its baseline. This is a separate edge case from the one fixed in #601, which only covered the default (non-unmanaged-constants) path.

…ants

A null pointer literal has no backing storage to return by reference, so
treat it as a copied value and emit an expression-bodied member rather than
an invalid ref readonly constant.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit 35f452e into dotnet:main Jul 16, 2026
12 checks passed
@tannergooding
tannergooding deleted the tannergooding-fix-nullptr-unmanaged-constant branch July 16, 2026 06:56
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.

VK_NULL_HANDLE is not generated correctly when GenerateUnmanagedConstants is specified

1 participant