-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/parse: Recognise const int assignments with type hints. #17643
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
Code size report:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #17643 +/- ##
==========================================
+ Coverage 98.23% 98.41% +0.17%
==========================================
Files 171 171
Lines 22140 22219 +79
==========================================
+ Hits 21749 21866 +117
+ Misses 391 353 -38 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Forgot to add tests... thank you, codecov! I've also had the chance to reword the commit message as it wasn't entirely clear. I'll push the lot once the current test run is over to avoid having to receive loads of cancelled run reports. Anyway, I wonder how difficult it would be to have something like |
Would it work to do this in the Python code instead? _MY_CONST: int
_MY_CONST = const(123) If it works, it might be worth it to save those 80+ bytes. |
I've just tried, and it does indeed work - after all the type annotations are discarded anyway. Also, I've simplified the code and turned out the final overhead is bigger, go figure. I'll revert the PR to what it looked like then. From an end user's point of view, having annotated variables exhibit the same behaviour as non-annotated ones is probably a reasonable expectation, isn't it? |
This commit extends the parser's behaviour to also recognise constant integer assignments even when the assignment itself has an "int" type annotation. Now declarations like "var: int = const(val)" can be treated as constants by the compiler and thus be folded in the rest of the program as it sees fit. Before this change, that line would generate a variable creation and its value assignment, without any folding being performed. This fixes micropython#15608. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Yes, a reasonable expectation. And the compiler can be disabled on super-constrained systems. So the code size isn't as much of a concern as I initially made it to be. |
Summary
This PR extends the parser's behaviour to also recognise constant integer assignments even when the assignment itself has an "int" type annotation.
Now declarations like "var: int = const(val)" can be treated as constants by the compiler and thus be folded in the rest of the program as it sees fit. Before this change, that line would generate a variable creation and its value assignment, without any folding being performed.
This fixes #15608.
Testing
This was tested using the same verification methodology described in #15608, and the Unix test suite was ran locally without errors with this change applied.
Trade-offs and Alternatives
I've limited the type checking to
int
types, which I hope it's all that's needed. It can probably be improved style-wise, but from what I reckon the logic should be correct - I haven't looked at the parser since the rv32 inline asm days.