Skip to content

Add #[overload("Name")] pseudo-attribute for WinRT method overloads in RDL#4184

Open
Copilot wants to merge 4 commits intomasterfrom
copilot/understand-winrt-overload-attribute
Open

Add #[overload("Name")] pseudo-attribute for WinRT method overloads in RDL#4184
Copilot wants to merge 4 commits intomasterfrom
copilot/understand-winrt-overload-attribute

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

WinRT's OverloadAttribute identifies the logical shared name for a group of uniquely-named vtable methods. RDL previously emitted this as a verbose qualified form (#[Windows::Foundation::Metadata::Overload("Method")]), which is awkward and requires Windows.Foundation.Metadata to be loaded as a reference to round-trip.

This introduces a first-class #[overload("Name")] pseudo-attribute—following the same pattern as #[typedef], #[special], and #[no_guid]—that cleanly expresses the overload grouping using the actual WINMD method names:

#[winrt]
mod Contoso {
    interface ISprocketFactory {
        #[overload("Create")]
        fn CreateDefault(&self) -> Sprocket;
        #[overload("Create")]
        fn CreateWithOptions(&self, options: SprocketOptions) -> Sprocket;
    }
}

In RDL the fn identifier is the unique vtable name and #[overload("...")] carries the common logical name. This matches the WinRT metadata binary layout where MethodDef.Name stores the common name and OverloadAttribute.value stores the unique name.

Changes:

  • Writer (crates/libs/rdl/src/writer/interface.rs): write_method detects OverloadAttribute, uses its value as the fn name (unique vtable name), and emits #[overload("common")] from MethodDef.Name. OverloadAttribute is excluded from the general custom-attributes path.
  • Reader (crates/libs/rdl/src/reader/interface.rs): encode_interface detects #[overload("common")], uses the arg as MethodDef.Name, and emits OverloadAttribute with the fn identifier (unique name) as its value. GUID derivation uses MethodDef.Name for consistency.
  • Roundtrip test (crates/tests/libs/rdl/tests/roundtrip/overload-method.rdl): Golden exercising the full RDL → WINMD → RDL cycle.
  • Encoding test (crates/tests/libs/rdl/tests/overload_encoding.rs): Directly reads the produced WINMD and asserts that MethodDef.Name = common name and OverloadAttribute.value = unique name.
  • Docs (crates/libs/rdl/rdl.md): Documents #[overload] under the Interfaces section.

Comment on lines +4 to +11
#[overload("Method")]
fn MethodOne(&self) -> i32;
#[overload("Method")]
fn MethodTwo(&self, a: i32) -> i32;
#[overload("Other")]
fn OtherVariantA(&self, x: f32, y: f32) -> bool;
#[overload("Other")]
fn OtherVariantB(&self, x: f32, y: f32, z: f32) -> bool;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this .rdl looks right but the .winmd representation is such that the MethodDef name stores the common name while the attribute stores the unique name. So keep this .rdl representation but make sure the metadata is actually encoded correctly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 912ca1a. The WINMD encoding is now correct:

  • MethodDef.Name = common/logical name (e.g. "Method") — taken from the #[overload("...")] arg
  • OverloadAttribute.value = unique vtable name (e.g. "MethodOne") — taken from the fn identifier

Added overload_encoding.rs which directly reads the produced WINMD and asserts this layout.

…bute.value = unique name

Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/9c771f77-258b-4888-82d6-2b4c3277c389

Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Copilot AI requested a review from kennykerr April 14, 2026 15:23
@kennykerr
Copy link
Copy Markdown
Collaborator

@copilot fmt code

Agent-Logs-Url: https://github.com/microsoft/windows-rs/sessions/a637526a-a754-4904-b3a3-95c59e4129b6

Co-authored-by: kennykerr <9845234+kennykerr@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 14, 2026

@copilot fmt code

Done in 870a4da.

@kennykerr kennykerr marked this pull request as ready for review April 14, 2026 17:58
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.

2 participants