rust-lang / rust-lang/rust

`isqrt` treated as a `black_box`

Open
#132,763 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-optimization I-slow T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#[inline(never)]
pub const fn f(n: u8) {
    assert!(n >= 4);
    assert!(2 <= n.isqrt());
}

[!note]
I've tried:

  • removing const
  • replacing u8 by usize
  • replacing 4 by 9 and 2 by 3

Outcome was the same

I expected to see this happen: 2nd assertion non-existent in assembly.

Instead, this happened:

# ... (snip)
.LBB0_4:
	leaq	.L__unnamed_4(%rip), %rdi
	leaq	.L__unnamed_5(%rip), %rdx
	movl	$32, %esi
	callq	*core::panicking::panic@GOTPCREL(%rip)
# ...
.L__unnamed_4:
	.ascii	"assertion failed: 2 <= n.isqrt()"
# ...
Meta

Playground

rustc --version --verbose:

1.84.0-nightly (2024-11-06 8549802939cd01111c46)

No Backtrace

@rustbot label: +I-slow, -C-bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in the linked Rust Playground with the reported nightly compiler and inspect the release assembly. Compare the handling of the const isqrt call with the surrounding assertions and compiler evaluation or optimization behavior. Done means the second assertion is eliminated when its condition is provably true, with appropriate regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.