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

CStr eq acts as an optimization barrier #109674

Closed
SUPERCILEX opened this issue Mar 27, 2023 · 0 comments · Fixed by #109682
Closed

CStr eq acts as an optimization barrier #109674

SUPERCILEX opened this issue Mar 27, 2023 · 0 comments · Fixed by #109682
Labels
C-bug Category: This is a bug.

Comments

@SUPERCILEX
Copy link
Contributor

I tried this code:

use std::ffi::CStr;

pub fn bytes(a: &CStr) -> bool {
    a.to_bytes() == b".."
}

pub fn cstr(a: &CStr) -> bool {
    a == unsafe { CStr::from_bytes_with_nul_unchecked(b"..\0") }
}

Godbolt: https://rust.godbolt.org/z/nh6nGjPMP

The explicit conversion to bytes lets the compiler figure out that it can compare two integers, but directly comparing CStrs seems to block that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant