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

Refactor RustTarget #2993

Merged
merged 7 commits into from
Nov 28, 2024
Merged

Refactor RustTarget #2993

merged 7 commits into from
Nov 28, 2024

Conversation

pvdrz
Copy link
Contributor

@pvdrz pvdrz commented Nov 27, 2024

This PR refactors the RustTarget type to be more flexible.

The motivation behind this change comes from the current definition of RustTarget as an enum. Which forces users to pick a specific version of Rust that might not match the msrv of their projects exactly.

As an example, let's say that a project using bindgen has an msrv of 1.41.1 and they decide to use the --rust-target flag to guarantee that bindgen won't produce code that breaks their msrv.

Given that bindgen doesn't have a RustTarget variant that specifically matches 1.41.1, they have to use the closest RustTarget that is still compatible. In this case that would be 1.40. If they decide to change their msrv in the future, they have to repeat this search process with their new msrv.

With the changes done in this PR, it would be possible to pass any* Rust version to --rust-target and bindgen will automatically enable the Rust features that are compatible with that version. In our example, that means that it would be possible to use --rust-version=1.41.1 explicitly, or even better, read the value from rust-version so they don't have to manually change it in the future.

The specific changes done in this PR are:

  • Replace the RustTarget enum for an opaque struct that can represent any* Rust version.

  • Update the FromStr implementation for RustTarget so it can parse any* Rust version.

  • Introduce several deprecated constants under RustTarget so people can still use those as if they were still using the variants of the removed enum type.

  • Introduce the builders RustTarget::nightly and RustTarget::stable.

  • Remove all the deprecated RustTargets.

  • Update the expectation tests to accommodate the removal of the deprecated targets.

(*): Unless it's not supported by bindgen anymore. To the date, anything earlier than 1.33 (inclusive)

cc @ojeda

@ojeda
Copy link
Contributor

ojeda commented Nov 28, 2024

Thanks for this, Christian!

Just to confirm: for projects that need to support bindgen releases with and without this PR, it is still possible to pass their current (old) --rust-target, i.e. all previous valid inputs will still be valid ones with the PR and do the same thing, right? (I see the parsing allows minor only, i.e. 1.x, so I think that is the intention, which is great.)

@pvdrz
Copy link
Contributor Author

pvdrz commented Nov 28, 2024

Thanks for this, Christian!

Just to confirm: for projects that need to support bindgen releases with and without this PR, it is still possible to pass their current (old) --rust-target, i.e. all previous valid inputs will still be valid ones with the PR and do the same thing, right? (I see the parsing allows minor only, i.e. 1.x, so I think that is the intention, which is great.)

Yes all the previous valid inputs are still valid, e.g. 1.33 gets parsed as 1.33.0 and so on. The only breaking change I expect to see here (other than the removal of deprecated targets) is if anyone was trying to match the RustTarget enum, as their matches would no longer be exhaustive

@ojeda
Copy link
Contributor

ojeda commented Nov 28, 2024

Sounds great, thanks for confirming!

The RustTarget part wouldn't affect the kernel since we use the CLI so far. Regarding the match, it is true that they wouldn't be able to simply match all without a wildcard; however, in that case, they needed to add new variants anyway when new releases added a new target version, no?

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Would've been nice to send the removal of deprecated targets separately, but looks good! Some minor bits only.

bindgen/features.rs Show resolved Hide resolved
bindgen-tests/tests/headers/layout.h Show resolved Hide resolved
This is done because bindgen is producing bogus code where a single
struct has both `packed` and `align` attributes.
@pvdrz
Copy link
Contributor Author

pvdrz commented Nov 28, 2024

Sounds great, thanks for confirming!

The RustTarget part wouldn't affect the kernel since we use the CLI so far. Regarding the match, it is true that they wouldn't be able to simply match all without a wildcard; however, in that case, they needed to add new variants anyway when new releases added a new target version, no?

Yes, they'd have to add extra match arms everytime we added more variants to RustTarget

@pvdrz pvdrz enabled auto-merge November 28, 2024 19:23
@pvdrz pvdrz added this pull request to the merge queue Nov 28, 2024
Merged via the queue into main with commit 887dc73 Nov 28, 2024
28 checks passed
@pvdrz pvdrz deleted the pvdrz/rust-target-refactor branch December 2, 2024 02:33
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.

3 participants