-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
py/obj: Explicitly cast MP_DEFINE_CONST_OBJ_TYPE slot values. #18201
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
base: master
Are you sure you want to change the base?
Conversation
The original from 3ac8b58 was an impressive code golf, but it's not exactly easy to understand or modify. There's really no need for it, see also micropython#14238. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
This resolves an issue compiling with IAR, where an implicit cast from a function pointer to a void pointer is disallowed. Based on comments from @hammondeggs in the MicroPython discord: https://discord.com/channels/574275045187125269/1012726673709469818/1423683501374308392 Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #18201 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 171 171
Lines 22276 22276
=======================================
Hits 21918 21918
Misses 358 358 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code size report:
|
unix/qemu_arm test failure on 568fbb4 is spurious ( |
By explicitly casting to the slot type before casting to a void pointer, this ensures that MP_DEFINE_CONST_OBJ_TYPE definitions can only define function slots with functions of the correct signature, or produce a compile-time error otherwise. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
568fbb4
to
71832a1
Compare
This has fixed this issue that I ran into when attempting to compile in IAR. Thank you! |
Summary
This PR updates the definition of
MP_DEFINE_CONST_OBJ_TYPE
to generate a compile-time error on code that attempts to define a slot function with an incompatible function signature, and rewrites the code generation script that produces its internalMP_DEFINE_CONST_OBJ_TYPE_NARGS_*
macros to make it easier to read and modify in this way.The addition of the explicit
(const void *)
casts also resolves an issue compiling with IAR, where an implicit cast from a function pointer to a void pointer is disallowed, based on comments from @hammondeggs in the MicroPython discord from this conversation.Testing
I've built and run the test suite against the Unix and RP2 ports with these patches applied, with all tests passing.
To verify that this generates the expected error, I've also tested building a defective version of MicroPython where I've added a
bool_attr
method to the objbool type with the mismatched declarationstatic void bool_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest, mp_obj_t extra)
. Applied to master, this defect produces no compilation warnings or test failures! With this PR, the build produces the following error, correctly identifying the problem:This is perhaps a bit wordier than ideal, but for an error generated deep within a nested macro expansion it's still pretty readable.
I lack the infrastructure to test IAR builds myself, but @hammondeggs has reported that this PR does resolve their issue.
Trade-offs and Alternatives
This does increase the source code's bulk and makes the macros themselves even wordier, but with the refactored generator script it's still an improvement to clarity.