6 ms·
Right, this is a small infelicity in Rust, it is easier to write let x = size as i32; ... even if what you meant was closer to let x: i32 = size.try_into
by tialaramex 5y ago
Right, this is a small infelicity in Rust, it is easier to write
let x = size as i32;
... even if what you meant was closer to
let x: i32 = size.try_into().expect("We are 100% sure size is small enough to fit into x");
But at least it isn't C or C++ where you might accidentally write
x = size;
... and the compiler doesn't even warn you that size is bigger than x and you need to think about what you intended.
It's really hard to fix this in C++. Some of the Epoch proponents want to do so using epochs to get there, basically you'd have a "new" epoch of C++ in which narrowing must be explicit, and old code would continue to have implicit narrowing so it doesn't break.
- dkirill 5y ago> and the compiler doesn't even warn you that size is bigger than x That's not true tho, compiler with reasonable flags set will definitely warn you and if you really don't like this kind of code you can force compiler to issue an error instead
- simias 5y agoWhat flags you have in mind? Because this code doesn't generate any warning with GCC 11.1.0 with -Wall -Wextra: int main(void) { int some_int = 1234567; char c = some_int; return c; }
- ridiculous_fish 5y ago-Wconversion will do it.
- aaronmdjones 5y ago-Wall -Wextra -Wpedantic does not enable all diagnostics. This is GNU's idea of "all". Contrast to Clang's -Weverything, which will.
- 130e13a 5y agokeep in mind though that -Weverything is not intended to be used in production: https://quuxplusone.github.io/blog/2018/12/06/dont-use-weverything/ https://quuxplusone.github.io/blog/2018/12/06/dont-use-wever...
- account42 5y ago-Weverything is great for CI though, in compination with lots of -Wno-... flags to disable warnings you don't want. Instead of having to manually look out for new warning flags you will get all automatically.
- aaronmdjones 5y agoYep, this is what I do; throw in -Weverything followed by a few things like -Wno-packed -Wno-padded -Wno-unused-parameter.
- rocqua 5y agoThe reason for this decision is so that compiler upgrades with -Wall and -Werror don't break builds. I can see the reason behind it, but I feel that this behavior is something you opt into when you use -Werror.
- derefr 5y ago> The reason for this decision is so that compiler upgrades with -Wall and -Werror don't break builds. It feels like the "right thing" here would instead be for the compiler to allow build scripts to reference a specific point-in-time semantics for -Wall. For example, `-Wall=9.3.0` could be used to mean "all the error checks that GCC v9.3.0 knew how to run". Or better yet (for portability), a date, e.g. `-Wall=20210720` to mean "all the error checks built into the compiler as of builds up-to-and-including [date]." To implement this, compilers would just need to know what version/date each of their error checks was first introduced. Errors newer than the user's specifier, could then be filtered out of -Wall, before -Wall is applied. With such a flag, you could "lock" your CI buildscript to a specific snapshot of warnings, just like you "lock" dependencies to a specific set of resolved versions. And just like dependency locking, if you have some time on your hands one day, you could "unlock" the error-check-suite snapshot, resolve all the new error-checks introduced, and then re-lock to the new error-check-suite timestamp.
- kllrnohj 5y agoClang-tidy has a check for it: https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.html https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidel...
- rualca 5y ago>What flags you have in mind? -Wconversion https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html This is common knowledge for ages. Any cursory Google search returns countless answers. Take this post made over a decade ago. https://stackoverflow.com/questions/1730255/gcc-shouldnt-a-warning-be-issued-when-assigning-an-int-to-a-char https://stackoverflow.com/questions/1730255/gcc-shouldnt-a-w...
- stjohnswarts 5y ago-Wconversion will catch this
- tialaramex 5y agoFair point although it seems "reasonable" varies from one platform to another, it doesn't warn out of the box for me but people have reported MSVC gets warnings here.
- nly 5y agoNarrowing within { } initialization is forbidden C++ now
- glaze 5y agoIf you initialize it like this you get a warning: x = { size };
- jacoblambda 5y agoIt can impact compile time performance but Boost Safe Numerics provides some nice wrappers to prevent narrowing (or restrict it to specific classes of narrowing) and throw warnings or errors at compile time similar to what you see in Rust.
- deleted 5y ago[deleted]