Skip to content

Degrade gracefully on C++ pointer-to-member types#771

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-member-pointer-degradation
Jul 14, 2026
Merged

Degrade gracefully on C++ pointer-to-member types#771
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-member-pointer-degradation

Conversation

@tannergooding

Copy link
Copy Markdown
Member

C++ pointer-to-member types (int MyClass::*, void (MyClass::*)()) were unsupported: the type-class switch never mapped CX_TypeClass_MemberPointer, so GetTypeName fell through to the raw Clang spelling (int MyClass::*, which isn't valid C#) and GetTypeSize hard-errored with Unsupported type: 'CX_TypeClass_MemberPointer'. The containing struct then couldn't be laid out.

These have no direct C# equivalent, so this degrades gracefully rather than erroring: GetTypeName emits an opaque void* and preserves the original spelling via [NativeTypeName(...)] plus a single warning, and GetTypeSize treats member pointers as pointer-sized (added to the existing FunctionType or PointerType or ReferenceType branch).

public unsafe partial struct MyStruct
{
    [NativeTypeName("int MyClass::*")]
    public void* field;
}

Caveat, called out in the warning: a member-function pointer can be wider than a data pointer under both the Itanium and MSVC ABIs, so void* may under-size those. Getting the exact width right is ABI-specific and left as a separate follow-up; this change is about not hard-erroring and keeping the struct usable.

Adds a golden regression test (MemberPointerDeclarationTest) covering data- and function-member pointers, with per-OS expected diagnostics (MSVC additionally emits an MSInheritance attribute warning; Itanium does not).

Fixes #511

Emit an opaque 'void*' with the original spelling preserved via NativeTypeName and a
warning, instead of hard-erroring the whole run when a field has a member-pointer type.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit ce9cfb3 into dotnet:main Jul 14, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-member-pointer-degradation branch July 14, 2026 20: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.

Unsupported type: 'CX_TypeClass_MemberPointer'

1 participant