Skip to content

CStr eq acts as an optimization barrier #109674

Closed
@SUPERCILEX

Description

@SUPERCILEX

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions