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.
Rust does not support method overloads so windows-bindgen handles overloads assumed by many Windows APIs in a way that those APIs can be both called, and if necessary implemented, in Rust. Today I came across some metadata for a new project that broke in some surprising ways. Unfortunately, developers may not think to test their APIs with languages that lack overload support and tools like MIDLRT don't help to catch such mistakes.
Consider a simple IDL example:
This class has an overloaded method named "Method". MIDLRT will generate the
Overload
attribute to provide alternative method names for languages that lack overload support. You might not realize this but while C++ supports overloads, COM does not. So this impacts even C++ code using these APIs, although C++/WinRT will smooth things over for you. The metadata will look something like this (I've elided some unimportant details):The overloads are actually defined by the exclusive
IA
interface synthesized by MIDLRT, but it's convenient to think of them as class methods, logically speaking. Languages like Rust that don't support overloads can use the method namesMethod
andMethod2
to call this API.So far so good. Consider a second IDL example:
This class defines the overload names explicitly so MIDLRT doesn't have to. The metadata will look something like this:
Languages like Rust that don't support overloads can use the method names
MethodOne
andMethodTwo
to use this API. This is always preferred because the API author can decide what the overloads should be called and give them more descriptive and meaningful names.But it's not just about making things easier for Rust developers. Consider the following IDL example:
This class defines the overloads of
Method
across two different exclusive interfaces. The trouble is that MIDLRT will now, regrettably, add theOverload
attributes for each interface without considering the fact that these interfaces have to overload with each other in the class. The metadata will look something like this:See the problem? There are two overloads called "Method" and two overloads called "Method2".
In summary, don't rely on MIDLRT to deal with overloads. At best it will make it inconvenient for developers. At worst it will make your API unusable in some languages. Always specify the overloads explicitly. Something like this:
This update to
windows-bindgen
just further hardens the code generator to deal with this pathological case by detecting these machine-generated overload names and contextualizing them properly for APIs.