Proposal: Replace autoTypes.json DECLARE_HANDLE entries with header annotations#2256
Draft
Proposal: Replace autoTypes.json DECLARE_HANDLE entries with header annotations#2256
Conversation
…nnotations Adds docs/copilot/plans/handle-annotation-proposal.md: a validated proposal to shift ~166 DECLARE_HANDLE entries from autoTypes.json into header annotations using the same conditional pattern as the enum proposal (PR #2247). The approach defines DECLARE_HANDLE_WIN32METADATA(name, ...) and sibling _Close_handle_with_/_Invalid_handle_/_Also_usable_for_ macros. Under -D_WIN32METADATA_=1, DECLARE_HANDLE expands to a wrapper struct ClangSharp v18 emits directly with [NativeAnnotation] attributes the emitter translates into the existing [RAIIFree], [InvalidHandleValue], [AlsoUsableFor], and [NativeTypedef]. Validated end-to-end with ClangSharp v18.1.0.4; test files in obj/handle-annotation-test/. Includes: - Projection-behavior section confirming both CsWin32 and windows-rs read these attributes and require no projection-side changes - Out-of-scope section sketching DECLARE_TYPEDEF_WIN32METADATA for the ~80 remaining typedef-style handles and value wrappers (BCRYPT, EVT, MSI, BOOL, HRESULT, NTSTATUS, BSTR, etc.) with concrete per-header examples - Recommendation to drop the dormant DEVPROPKEY=[AlsoUsableFor(...)] entry from emitter.settings.rsp Also updates annotation-validation-results.md with a summary of the remaining shift-left categories after the enum proposal landed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- handle-annotation-proposal.md: expand to cover ALL ~280 entries in autoTypes.json (5 categories), not just DECLARE_HANDLE-style. Both macros (DECLARE_HANDLE_WIN32METADATA and DECLARE_TYPEDEF_WIN32METADATA) are now first-class. Migration removes autoTypes.json, NativeTypedefStructsCreator.cs, AutoType.cs, and PrepSettingsForAutoTypes.cs. - Resolve all open questions inline: - Phasing: land as one proposal (avoids dual-pipeline transition state) - Overlay headers in Phase 1, SDK patches in Phase 2 (matches enum proposal) - Keep both macros distinct: their #else branches preserve different SDK forms - Caveat 2 (HRESULT/NTSTATUS SAL): resolved — pipeline already drops typedef SAL; success contracts come from emitter.settings.rsp per-function entries. - spec.md: update typedef-style handles section to describe full removal, not follow-up. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a validated proposal to shift the ~166
DECLARE_HANDLEentries fromautoTypes.jsoninto header annotations, mirroring the conditional-declaration pattern from the enum remapping proposal (#2247).The approach defines
DECLARE_HANDLE_WIN32METADATA(name, ...)plus_Close_handle_with_/_Invalid_handle_/_Also_usable_for_macros. Under-D_WIN32METADATA_=1,DECLARE_HANDLEexpands to a wrapper struct that ClangSharp v18 emits directly with[NativeAnnotation]attributes;MetadataSyntaxTreeCleanertranslates them into the existing[RAIIFree],[InvalidHandleValue],[AlsoUsableFor], and[NativeTypedef]. The winmd output is byte-identical to today.Validated end-to-end with ClangSharp v18.1.0.4 (test files in
obj/handle-annotation-test/).What's in the proposal
DECLARE_HANDLEredefinition + SAL-style annotation macrosstruct [[attr]] NAME { ... }shape preserves annotations on the right declaration. C++11 attributes cannot precedestructat namespace scope, which is why we need a macro that takes attributes via__VA_ARGS__.From+CanInto) for[NativeTypedef]structs, and require no projection-side changes.DECLARE_TYPEDEF_WIN32METADATA(name, type, ...)macro sketched for the remaining ~83 typedef-style handles and value wrappers (BCRYPT, EVT, MSI, BOOL, HRESULT, NTSTATUS, BSTR, etc.) as a follow-up.DEVPROPKEY=[AlsoUsableFor("PROPERTYKEY")]fromemitter.settings.rsp; both CsWin32 and windows-rs only generateAlsoUsableForcode for[NativeTypedef]-shaped structs, so this entry is dormant.Open questions called out in the proposal
[InvalidHandleValueAttribute]constructor takeslong— annotation strings need the right parsingNativeTypedefStructsCreatordoes todayCloseApi's namespace; with shift-left it comes from header partition placementCompanion changes
Also updates
annotation-validation-results.mdwith a summary table of the remaining shift-left categories after the enum proposal landed, so future readers can see where this fits in the larger workstream.