-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
tree-wide: fix implicit casts #5773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth building the coverage build in the Travis osx job?
ports/unix/modtime.c
Outdated
@@ -61,7 +61,7 @@ static inline int msec_sleep_tv(struct timeval *tv) { | |||
#endif | |||
|
|||
#if defined(MP_CLOCKS_PER_SEC) | |||
#define CLOCK_DIV (MP_CLOCKS_PER_SEC / 1000.0F) | |||
#define CLOCK_DIV (MP_CLOCKS_PER_SEC / (mp_float_t)1000.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a macro MICROPY_FLOAT_CONST(x)
which might be better suited here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
py/objarray.c
Outdated
@@ -430,7 +430,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value | |||
} | |||
|
|||
// TODO: check src/dst compat | |||
mp_int_t len_adj = src_len - (slice.stop - slice.start); | |||
size_t len_adj = src_len - (slice.stop - slice.start); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think len_adj
can be negative
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -218,7 +218,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool | |||
if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { | |||
// inf | |||
str += 3; | |||
dec_val = INFINITY; | |||
dec_val = (mp_float_t)INFINITY; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably also use MICROPY_FLOAT_CONST(x)
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work since MICROPY_FLOAT_CONST either passes the value through untouched or adds an F
. So if mp_float_t is double, we still get a compile error or if mp_float_t is float, then we get undefined INFINITYF
.
IIRC there's more of these, and also a lot of implicit signed/unsigned casts. At least I remember that building with the default warning level with msvc showed more warnings than what gets fixed here. Will get back later today with a list |
Ok so there's these:
Not sure if these should all be fixed. If so let me know. The ones concerning float/int would best be added to this commit so everything is done in one go? If all of the above gets fixed there's only some minor warnings left which should probably be disabled on the compiler and not fixed (mainly 'unary minus operator applied to unsigned type, result still unsigned' like for instance in py\emitbc.c line 724' which is quite uniteresting), and then the msvc port can be built with warning level 3 instead of 1 which would be good overall. |
I suppose if we want to catch compile errors like this |
6b5f22e
to
00df509
Compare
It looks like the test failure is for thread_stacksize1 which has been failing intermittently on other PRs as well. |
@stinos I'm happy to have these fixed, but each one would need to be looked over to see exactly how to fix it (probably just cast up, but in some cases need to be careful with sign). I think all the float-float implicit casts are fixed by this PR. Would be good if the warnings could be gotten on *nix by enabling some compiler flags... is that possible? |
py/objarray.c
Outdated
@@ -430,7 +430,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value | |||
} | |||
|
|||
// TODO: check src/dst compat | |||
mp_int_t len_adj = src_len - (slice.stop - slice.start); | |||
long len_adj = src_len - (slice.stop - slice.start); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure making it long
is the right thing to do... it's not obvious what it should be but how about just keeping it as mp_int_t
because (so far) that seems to work? Otherwise I'd suggest using ssize_t
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried ssize_t
, but that is a posix type, not a standard C type, so it causes issues on some ports with the type not being defined. So yes, we can keep it as mp_int_t
, but will have to add more casts elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this out building the coverage build with clang on Linux and I get the same warnings, and can fix this one simply by adding the size_t cast below, I didn't need to change the type here (it could remain mp_int_t
). So I'd prefer that, to not change this type.
It's a bit peculiar it seems. I started with
Since both compilers agree this would be a good candidate for fixing; I'm not sure what it takes to build with clang on unix so didn't try that, but e.g. the above cases are not fixed in this PR. @dlech is this a 32-bit build perhaps, or any idea what goes on? Then I moved on to |
This PR is based off errors from 64-bit clang on macOS 10.15. To build with clang on Linux, just add |
So mp_float_t is double, right? Will try with clang later. I just don't understand why it would warn for e.g. mp_obj_new_float(x) when x is float. If mp_float_t is float there's no conversion, otherwise it's from float to double which should not cause a (considerable) loss? And on the other hand it does not warn for the opposite direction. Strange. |
Nevermid, after seeing actual warning I understood (please add this to the commit message so it's clear what is being fixed here and why). Full warning is
I'm actually not convinced this needs fixing with casts: idea behind this flag is to show you where you're using doubles on targets which don't have FPU support. I.e. just casting doesn't fix much, but does make the use of Wdouble-promotion useless. Removing the flag seems the better option then. doc:
|
@stinos yes, grep for |
Can you please rebase on latest master because there were changes to |
These were found by buiding the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure.
rebased and changed |
This should help catch more compile errors with the clang compiler.
Sorry but would really have been better if the commit message said what casts exactly get fixed here, because it's not all of them, nor the ones which actually loose precision :[ |
@stinos sorry it got a bit confusing with this and #5805, and I wanted to get this in before moving on to that PR. The changes here fix compilation under clang, and (as you point out) fix If you think some of the changes should be reverted, let me know. |
Well I think it would be ideal to have the situation as you describe (just the float->double promotion fixes in one commit without the changes for symmetry), and then go on and fix other things. But I'm not sure if it's worth messing up git history a bit just for that, your call I'd say. |
Maybe we will arrive at a different solution, depending on what everyone thinks about |
…ries-2021-12-23-7.1.x Update frozen libraries in preparation for 7.1.0-rc.0
These were found by building the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure.