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

Make makedev, major, minor const #4208

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

igankevich
Copy link

Description

This PR marks all makedev, major, minor re-implementations (except Solarish) as const fn and tests major and minor against their C counterparts (macros). This PR closely follows the work that was done in this commit.

The motivation behind this PR is to make Rust code that uses these three functions consistent across different platforms. To give a concrete example: currently one has to wrap major and minor in unsafe blocks on MacOS, whereas on Linux these blocks are not needed. Unnecessary unsafe blocks produce a warning that one silences with #[allow(unused_unsafe)]. All of that confuses code reviewers and creates unnecessary noise. makedev, major and minor are macros on most Unixes, there is no need for them to be unsafe.

As far as I understand these changes do not break existing code: changing unsafe functions to const only produce warnings but not compilation errors.

@rustbot label stable-nominated

Sources

None.

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot
Copy link
Collaborator

rustbot commented Dec 18, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tgross35 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot
Copy link
Collaborator

rustbot commented Dec 18, 2024

Some changes occurred in OpenBSD module

cc @semarie

@rustbot rustbot added the stable-nominated This PR should be considered for cherry-pick to libc's stable release branch label Dec 18, 2024
@igankevich
Copy link
Author

igankevich commented Dec 18, 2024

After testing with the CI I found and fixed a bug in emscripten's major and minor: bit shifts and masking were done in the reverse order. The original macros are here, the same link is in the crate's source code.

@tgross35
Copy link
Contributor

This also changes from unsafe to safe, which should be fine and fixes #3759.

@hoodmane could you double check the emscripten changes?

Comment on lines +27 to +29
assert_eq!(libc::major(dev), major as _);
let minor = unsafe { minor_ffi(dev) };
assert_eq!(libc::minor(dev), minor as _);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you name the types here rather than using _?

Copy link
Author

Choose a reason for hiding this comment

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

I wish I could, but the return type can be c_int, c_uint, major_t, minor_t depending on the platform.

Copy link
Contributor

@hoodmane hoodmane left a comment

Choose a reason for hiding this comment

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

Maybe it would be nice to keep the bitwise or terms of the rust code in the same order as they appear in musl for easier validation?

}

safe_f! {
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
Copy link
Contributor

Choose a reason for hiding this comment

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

Emscripten has actual functions for these in addition to macros so it would be possible to call out to them. But it would be a lot slower than this.

@hoodmane
Copy link
Contributor

Otherwise it looks good to me.

@igankevich
Copy link
Author

Maybe it would be nice to keep the bitwise or terms of the rust code in the same order as they appear in musl for easier validation?

Thanks for pointing this out! I've changed the order. Kindly re-requesting the review 🙏

@igankevich igankevich requested a review from hoodmane December 25, 2024 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-android O-dragonfly O-linux O-macos O-unix S-waiting-on-review stable-nominated This PR should be considered for cherry-pick to libc's stable release branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants