-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Add support for unpacking for target
#15058
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ use crate::semantic_index::use_def::{ | |
| FlowSnapshot, ScopedConstraintId, ScopedVisibilityConstraintId, UseDefMapBuilder, | ||
| }; | ||
| use crate::semantic_index::SemanticIndex; | ||
| use crate::unpack::Unpack; | ||
| use crate::unpack::{Unpack, UnpackValue}; | ||
| use crate::visibility_constraints::VisibilityConstraint; | ||
| use crate::Db; | ||
|
|
||
|
|
@@ -810,7 +810,7 @@ where | |
| unsafe { | ||
| AstNodeRef::new(self.module.clone(), target) | ||
| }, | ||
| value, | ||
| UnpackValue::Assign(value), | ||
| countme::Count::default(), | ||
| )), | ||
| }) | ||
|
|
@@ -1021,18 +1021,47 @@ where | |
| orelse, | ||
| }, | ||
| ) => { | ||
| self.add_standalone_expression(iter); | ||
| debug_assert_eq!(&self.current_assignments, &[]); | ||
|
|
||
| let iter_expr = self.add_standalone_expression(iter); | ||
| self.visit_expr(iter); | ||
|
|
||
| self.record_ambiguous_visibility(); | ||
|
|
||
| let pre_loop = self.flow_snapshot(); | ||
| let saved_break_states = std::mem::take(&mut self.loop_break_states); | ||
|
|
||
| debug_assert_eq!(&self.current_assignments, &[]); | ||
| self.push_assignment(for_stmt.into()); | ||
| let current_assignment = match &**target { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is basically the same logic as in the assignment statement, I'm going to wait until I implement unpacking support in other places to get an overview of what can be simplified to avoid repetition. |
||
| ast::Expr::List(_) | ast::Expr::Tuple(_) => Some(CurrentAssignment::For { | ||
| node: for_stmt, | ||
| first: true, | ||
| unpack: Some(Unpack::new( | ||
| self.db, | ||
| self.file, | ||
| self.current_scope(), | ||
| #[allow(unsafe_code)] | ||
| unsafe { | ||
| AstNodeRef::new(self.module.clone(), target) | ||
| }, | ||
| UnpackValue::Iterable(iter_expr), | ||
| countme::Count::default(), | ||
| )), | ||
| }), | ||
| ast::Expr::Name(_) => Some(CurrentAssignment::For { | ||
| node: for_stmt, | ||
| unpack: None, | ||
| first: false, | ||
| }), | ||
| _ => None, | ||
| }; | ||
|
|
||
| if let Some(current_assignment) = current_assignment { | ||
| self.push_assignment(current_assignment); | ||
| } | ||
| self.visit_expr(target); | ||
| self.pop_assignment(); | ||
| if current_assignment.is_some() { | ||
| self.pop_assignment(); | ||
| } | ||
|
|
||
| // TODO: Definitions created by loop variables | ||
| // (and definitions created inside the body) | ||
|
|
@@ -1283,12 +1312,18 @@ where | |
| Some(CurrentAssignment::AugAssign(aug_assign)) => { | ||
| self.add_definition(symbol, aug_assign); | ||
| } | ||
| Some(CurrentAssignment::For(node)) => { | ||
| Some(CurrentAssignment::For { | ||
| node, | ||
| first, | ||
| unpack, | ||
| }) => { | ||
| self.add_definition( | ||
| symbol, | ||
| ForStmtDefinitionNodeRef { | ||
| unpack, | ||
| first, | ||
| iterable: &node.iter, | ||
| target: name_node, | ||
| name: name_node, | ||
| is_async: node.is_async, | ||
| }, | ||
| ); | ||
|
|
@@ -1324,7 +1359,9 @@ where | |
| } | ||
| } | ||
|
|
||
| if let Some(CurrentAssignment::Assign { first, .. }) = self.current_assignment_mut() | ||
| if let Some( | ||
| CurrentAssignment::Assign { first, .. } | CurrentAssignment::For { first, .. }, | ||
| ) = self.current_assignment_mut() | ||
| { | ||
| *first = false; | ||
| } | ||
|
|
@@ -1566,7 +1603,11 @@ enum CurrentAssignment<'a> { | |
| }, | ||
| AnnAssign(&'a ast::StmtAnnAssign), | ||
| AugAssign(&'a ast::StmtAugAssign), | ||
| For(&'a ast::StmtFor), | ||
| For { | ||
| node: &'a ast::StmtFor, | ||
| first: bool, | ||
| unpack: Option<Unpack<'a>>, | ||
| }, | ||
| Named(&'a ast::ExprNamed), | ||
| Comprehension { | ||
| node: &'a ast::Comprehension, | ||
|
|
@@ -1590,12 +1631,6 @@ impl<'a> From<&'a ast::StmtAugAssign> for CurrentAssignment<'a> { | |
| } | ||
| } | ||
|
|
||
| impl<'a> From<&'a ast::StmtFor> for CurrentAssignment<'a> { | ||
| fn from(value: &'a ast::StmtFor) -> Self { | ||
| Self::For(value) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<&'a ast::ExprNamed> for CurrentAssignment<'a> { | ||
| fn from(value: &'a ast::ExprNamed) -> Self { | ||
| Self::Named(value) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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've intentionally left out certain test cases, refer to the "Question" in PR description.
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 the tests you have here look good!