Skip to content

Conversation

@EFanZh
Copy link
Contributor

@EFanZh EFanZh commented Nov 22, 2025

This PR adds hints that the return value of T::ilog10 will never exceed T::MAX.ilog10().

This works because ilog10 is a monotonically nondecreasing function, the maximum return value is reached at the max input value.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 22, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@Noratrieb
Copy link
Member

Is this motivated by concrete codegen improvements (the codegen tests are quite artificial)? assume/assert_unchecked always has the potential to go either way wrt performance, so it would be good to know whether you've confirmed that this improves real-world code

@jhorstmann
Copy link
Contributor

Interesting that llvm can already infer some bounds, but apparently not the tightest ones. Looking at the following functions in compiler explorer, there are no panics:

pub fn test_u8(i: std::num::NonZeroU8, a: &[u32; 3]) -> u32 {
    a[i.ilog10() as usize]
}

pub fn test_u16(i: std::num::NonZeroU16, a: &[u32; 8]) -> u32 {
    a[i.ilog10() as usize]
}

pub fn test_u32(i: std::num::NonZeroU32, a: &[u32; 13]) -> u32 {
    a[i.ilog10() as usize]
}

pub fn test_u64(i: std::num::NonZeroU64, a: &[u32; 23]) -> u32 {
    a[i.ilog10() as usize]
}

These functions with tighter bounds would make for nicer codegen test.

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants