Skip to content

Change memcmp and bcmp return type to c_int #980

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

Merged
merged 4 commits into from
Jul 18, 2025

Conversation

supersurviveur
Copy link
Contributor

This PR fix the return type of memcmp and bcmp builtin function on target with a pointer_width equals to 16.
Linked issue: rust-lang/rust#144076

@@ -384,13 +384,13 @@ pub unsafe fn set_bytes(mut s: *mut u8, c: u8, mut n: usize) {
}

#[inline(always)]
pub unsafe fn compare_bytes(s1: *const u8, s2: *const u8, n: usize) -> i32 {
pub unsafe fn compare_bytes(s1: *const u8, s2: *const u8, n: usize) -> crate::mem::c_int {
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 import core::ffi::c_int and use c_int here instead?

I didn't even realize we had crate::mem::c_int, I'll probably remove that to just use core::ffi instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in compiler-builtins/src/mem/mod.rs still need a full path core::ffi::c_int though, unfortunately the macro makes imports a bit weird.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't even realize we had crate::mem::c_int, I'll probably remove that to just use core::ffi instead.

Doing this here #981

let mut i = 0;
while i < n {
let a = *s1.wrapping_add(i);
let b = *s2.wrapping_add(i);
if a != b {
return a as i32 - b as i32;
return a as crate::mem::c_int - b as crate::mem::c_int;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return a as crate::mem::c_int - b as crate::mem::c_int;
return crate::mem::c_int::from(a) - crate::mem::c_int::from(b);

To make it clearer the conversions are lossless.

@supersurviveur
Copy link
Contributor Author

Ouuups pushed too early, I will do the other changes.
I already changed to core::ffi::c_int

@supersurviveur
Copy link
Contributor Author

Should I revert my change for the memset function since @tgross35 did it in another PR ?

@tgross35
Copy link
Contributor

No need, it figured things out with a rebase. One nit though, in compiler-builtins/src/mem/impls.rs could you import core::ffi::c_int? Since it's used a few times might as well simplify the path.

Sorry, should have noticed before doing the rebase

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Great, thanks for the fix!

@tgross35 tgross35 enabled auto-merge (squash) July 18, 2025 18:10
@tgross35 tgross35 merged commit 556be9b into rust-lang:master Jul 18, 2025
48 of 72 checks passed
@supersurviveur supersurviveur deleted the memcmp-use-c_int branch July 18, 2025 19:49
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