by tialaramex · 5y ago · 0 comments
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.
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
int main(void) {
int some_int = 1234567;
char c = some_int;
return c;
}This is GNU's idea of "all".
Contrast to Clang's -Weverything, which will.
I can see the reason behind it, but I feel that this behavior is something you opt into when you use -Werror.
Unfortunately, over the years people baked the semantics of -Wall into their builds so new diagnostics could not be added to that flag.
And clang’s -Weverything shows how the opposite can fail as well
-Wconversion
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-w...
x = { size };