Skip to content

Inform LLVM that &str has length <= isize::MAX - #163014

Draft
Darksonn wants to merge 1 commit into
rust-lang:mainfrom
Darksonn:str-len-info
Draft

Darksonn wants to merge 1 commit into
rust-lang:mainfrom
Darksonn:str-len-info

Conversation

@Darksonn

Copy link
Copy Markdown
Member

When &str appears as a function argument, we can inform LLVM that the length has a restricted range similar to what's already done for slices.

The tests/codegen-llvm/function-arguments.rs file has a test that looks like it intended to check this scenario, but the fn str function uses a &[u8] argument rather than &str. Since there's also another identical fn slice with an &[u8] argument in this test, this appears to be an oversight.

// CHECK: @slice(
// CHECK-SAME: ptr noalias nofree noundef nonnull readonly{{( captures\(address, read_provenance\))?}} %_1.0,
// CHECK-SAME: [[USIZE]] noundef range({{i32 0, -2147483648|i64 0, -9223372036854775808}}) %_1.1)
#[no_mangle]
pub fn slice(_: &[u8]) {}

// CHECK: @str(
// CHECK-SAME: ptr noalias nofree noundef nonnull readonly{{( captures\(address, read_provenance\))?}} %_1.0,
// CHECK-SAME: [[USIZE]] noundef range({{i32 0, -2147483648|i64 0, -9223372036854775808}}) %_1.1)
#[no_mangle]
pub fn str(_: &[u8]) {}

This issue was discovered by an LLM, but the PR was implemented by hand.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants