Effect of cargo profiles on code in std

How do profile settings effect behavior of code compiled in the standard library?

For example the overflow-checks profile option?

I was debugging something today which I incorrectly thought was an overflow from my code calling Duration::add. It turns out this wasn't the bug, but it got me thinking... How do profile settings effect overflow behavior of code defined in the standard library?

My understanding before was: if I compiled with overflow-checks = true all integer overflows (even in std) would cause a panic.

However, I also thought the standard library was pre-built by rustup, so my own compilation flags shouldn't have any effect on std.

By inspecting the source I realized there's no way Duration::add could overflow as it uses checked_add with expect internally.

Anyone know of a function in the standard library which has different behavior depending on overflow compilation flags?

Not all code is compiled to native machine code. Generics and inlineable functions are compiled as MIR.

Overflow checks are not a regular feature flag. They are compiled to special intrinsic, which the compiler is able to change later when the code is imported from libstd.

debug_assert! in libstd also got special instructions recently, which can be turned on or off after compilation.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.