-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add end locations to all nodes #4192
Conversation
430985f
to
3a18132
Compare
3924854
to
6ab8d68
Compare
Please feel free to let me know if you need any help |
6ab8d68
to
0f070bc
Compare
@youknowone - Just rebased. I've been using this code successfully in Ruff for a while now, would love to get it merged if you're open to it! Let me know what needs fixing / addressing :) |
@charliermarsh I didn't check the details yet, but could you check the failing tests? |
@youknowone - Absolutely, will do, thanks. |
f4359ee
to
790e557
Compare
@youknowone - Tests passing! |
790e557
to
b132ede
Compare
Because we are trying to leave our own footprints to CPython codes as less as possible, I reverted the last commit but tried to change end_location to be optional. |
I can change it to |
Or are you suggesting something else? Using |
Specifically, I could change the struct to: #[derive(Debug, PartialEq)]
pub struct Located<T, U = ()> {
pub location: Location,
pub end_location: Option<Location>,
pub custom: U,
pub node: T,
} ...then propagate that throughout the codebase. |
I am ok with either way unless it works well with CPython test codes. |
I can try to make that change today. |
In practice, does CPython ever omit |
b132ede
to
b2148b3
Compare
@charliermarsh because I made incomplete commit last push, you can work on the new one as a base. I am sorry, I don't know well about CPython ast that much. |
b2148b3
to
ff8e947
Compare
@youknowone - Hoping I didn't override your work. This is ready for review. |
@@ -2307,8 +2307,6 @@ class C: | |||
cdef = ast.parse(s).body[0] | |||
self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method) | |||
|
|||
# TODO: RUSTPYTHON | |||
@unittest.expectedFailure |
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.
@youknowone - All other test changes were now able to be reverted.
pub custom: U, | ||
pub node: T, | ||
} | ||
|
||
impl<T> Located<T> { | ||
pub fn new(location: Location, node: T) -> Self { | ||
pub fn new(location: Location, end_location: Location, node: T) -> Self { |
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 kept Location
as a required field in ::new
for now. (We can omit it in special cases by going through the Struct directly, as in ast.rs
.)
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 could see arguments either way here. On the one hand, it's odd that the field you pass in differs from what's stored on the struct. On the other hand, this encourages good behavior and reduces the number of code changes required.
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 agree this approach and the idea encouraging good behavior.
00238ba
to
f2a7ae6
Compare
compiler/parser/src/context.rs
Outdated
@@ -0,0 +1,173 @@ | |||
use rustpython_ast::{Expr, ExprContext, ExprKind}; | |||
|
|||
pub fn set_context(expr: Expr, ctx: ExprContext) -> Expr { |
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.
is this necessary part of end_location?
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.
Oh no, sorry, I need to revert this. Not sure how it got included.
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.
(Removed.)
f2a7ae6
to
4082b17
Compare
Awesome! Thank you! |
Summary
I'm finding myself needing end locations for AST nodes (similar to in the Python AST). Is there interest in enabling this? If so, happy to clean up and test further.