py/parse: Recognise const int assignments with type hints.#17643
Conversation
|
Code size report: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #17643 +/- ##
=======================================
Coverage 98.46% 98.46%
=======================================
Files 176 176
Lines 22800 22802 +2
=======================================
+ Hits 22451 22453 +2
Misses 349 349 ☔ 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? |
|
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. |
Actually, const supports bool and tuple. And also now float! So the type annotation could be rather complicated if you have nested tuples etc. So... the parser could just support any type annotation, ie any expression. Or just any identifier.
There are tests for generated bytecode, in particular one for const generation. See |
ef3ef6f to
a16cdd1
Compare
Indeed. Luckily I limited myself to just handle I can revisit this at another time though, so both
Thanks, I didn't know this was possible. Although what I had in mind was more or less a MicroPython implementation of CPython's |
|
Why do we need to check the annotation value and limit it to only |
|
Gosh, I missed that - sorry! I'm all for it, let me update the PR right away then. |
6077b54 to
71fc010
Compare
|
Thanks for updating, the code changes look good now. Regarding the tests: it looks like you've copied two existing tests and added annotations. I think that's overkill and makes it harder to maintain the tests. Instead I suggest adding one new test called, eg, # Test type annotations in combination with const.
from micropython import const
_X0: bool = const(True)
_X1: int = const(123)
_X2: str = const("test")
_X3: tuple = const((1, 2))
_X4: tuple[bool, int] = const((True, 4))(Keep in mind not to use float because not all targets support floats.) |
|
Thanks for the test template. The previous iteration duplicated the original tests because they only dealt with integers in the first place, so that would fit with the initial reduced scope of these changes. Anyway, I've added a missing const-able type ( |
dpgeorge
left a comment
There was a problem hiding this comment.
Thanks for updating the test.
This commit extends the parser's behaviour to also recognise constant integer assignments even when the assignment itself has a 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>
e6b9c0d to
41137f0
Compare
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
inttypes, 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.