Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string constants for standalone sys #2500

Conversation

Jake-Shadle
Copy link
Contributor

This change fixes two issues described in #2499.

  1. String constants are (currently) specially handled to resolve to PCSTR or PCWSTR based on the constant field, but this was not reflected in type collection. The standalone type collection now ignores string constants since it knows they are special, so HSTRING is no longer added if not used elsewhere.
  2. String constant emission unconditionally emits s! or w! to create the string literal, but in standalone + sys mode this means the macros are not prefixed with the crate, the only special case in the current bindgen for the crate name. Now if in standalone + sys mode it assumes windows_core is used and emits that.

The other way to fix issue 2 is to get rid of the macros altogether in favor of just emitting either a byte string (with null terminator) for ansi, or a precomputed u16 array (with null terminator) directly in the bindings. The byte string would be as readable as the macro is today, and it would be trivial to add a comment on top of the u16 array to say the human readable string that it represents. But I did the simplest fix for now.

Resolves: #2499

@Jake-Shadle Jake-Shadle force-pushed the fix-string-constant-type-collection branch from b90b368 to deb0645 Compare May 10, 2023 23:26
@@ -12,7 +12,11 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream {

if ty == constant_type {
if ty == Type::String {
let crate_name = gen.crate_name();
let crate_name: TokenStream = if gen.sys && !gen.standalone {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit problematic since this assumes that sys-style code gen should assume a dependency on windows-core which is designed for non-sys style code gen. I'd like to avoid adding a "core" crate for windows-sys but this conflates the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I considered this the simpler fix in hopes of merging, but like I said in the PR description I think the better option is just to remove the need for the macros altogether, but I figured that would be harder to pass review.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, it's not a simple fix but I'd prefer to find a good solution rather than fix a problem by introducing another problem. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

// String normally gets resolved to HSTRING, but since macros
// from windows_core/windows_sys::core are used to create the
// string literals we can skip the type collection
if ft != Type::String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a hack. String constants should not be depending on HSTRING.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that they are in fact System.String types while windows-rs assumes that is always an HSTRING. That's a separate issue but I have a plan to address that.

@kennykerr
Copy link
Collaborator

I think the main issue is support for string literals in standalone code generation. I'll think some more on the best way to handle those in sys-style code generation. It should work fine for non-sys code generation.

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.

standalone_sys should support string constants
2 participants