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

Using generate_cstr(true) breaks output format #3047

Open
gwenn opened this issue Dec 9, 2024 · 8 comments · May be fixed by #3063
Open

Using generate_cstr(true) breaks output format #3047

gwenn opened this issue Dec 9, 2024 · 8 comments · May be fixed by #3063

Comments

@gwenn
Copy link

gwenn commented Dec 9, 2024

To reproduce,

Without the following patch, generated bindings are correctly formatted.
But not with:

diff --git a/libsqlite3-sys/build.rs b/libsqlite3-sys/build.rs
index f96d656..53778b7 100644
--- a/libsqlite3-sys/build.rs
+++ b/libsqlite3-sys/build.rs
@@ -533,6 +533,7 @@ mod bindings {
         let mut bindings = bindgen::builder()
             .default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
             .disable_nested_struct_naming()
+           .generate_cstr(true)
             .trust_clang_mangling(false)
             .header(header.clone())
             .parse_callbacks(Box::new(SqliteTypeChooser));
@ojeda
Copy link
Contributor

ojeda commented Dec 9, 2024

Could you please minimize / give an example of the difference? Thanks!

@nyurik
Copy link
Contributor

nyurik commented Dec 10, 2024

This is due to rustfmt not understanding the c"..." syntax -- which is only available with the 2021+ edition. The issue is that bindgen executes rustfmt without the --edition param, so rustfmt defaults to 2015. I think we can just add the edition parameter, setting it to 2021 if we generate the C-literals

See also https://rust-lang.zulipchat.com/#narrow/channel/469104-wg-bindgen/topic/Rustfmt.20and.20editions

@nyurik
Copy link
Contributor

nyurik commented Dec 10, 2024

A workaround for this issue: create a rustfmt.toml in the root of your project, and add edition = "2021" to it

@pvdrz
Copy link
Contributor

pvdrz commented Dec 10, 2024

A workaround for this issue: create a rustfmt.toml in the root of your project, and add edition = "2021" to it

indeed, if you use a feature only available on edition 2021, you have to use a toolchain with that edition enabled.

I wouldn't say this is a workaround.

@nyurik
Copy link
Contributor

nyurik commented Dec 10, 2024

I think its a workaround because a user would expect this to work automagically - i.e. bindgen output should be proper in every environment - thus we really need to pass --edition param to rustfmt when needed. I may try to hack a patch for that

@pvdrz
Copy link
Contributor

pvdrz commented Dec 10, 2024

I think its a workaround because a user would expect this to work automagically - i.e. bindgen output should be proper in every environment

In that case maybe we should unconditionally use 2018 as the default edition instead.

thus we really need to pass --edition param to rustfmt when needed.

Yes but you can also pass the edition in your rustfmt.toml. I think this is basically the same issue as with the msrv. You have to set your edition everywhere (Cargo.toml, rustfmt.toml and bindgen) otherwise you'll have unexpected outputs.

@nyurik
Copy link
Contributor

nyurik commented Dec 10, 2024

Is there ever a case when bindgen 2018 output can be formatted in some broken way by the 2021 edition formatter? If not, we might as well pass the --edition 2021 to the rustfmt if the MSRV is for the version that supports it. The rest of the user code can still be using 2018 or anything else based on Cargo.toml - bindgen doesn't care about that

@nyurik nyurik linked a pull request Dec 27, 2024 that will close this issue
@nyurik
Copy link
Contributor

nyurik commented Dec 27, 2024

#3063 fixes this issue

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 a pull request may close this issue.

4 participants