Skip to content

Commit

Permalink
[red-knot] better docs for use-def maps (#12357)
Browse files Browse the repository at this point in the history
Add better doc comments and comments, as well as one debug assertion, to
use-def map building.
  • Loading branch information
carljm authored Jul 18, 2024
1 parent 985a999 commit b2a49d8
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 52 deletions.
5 changes: 3 additions & 2 deletions crates/red_knot_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ use crate::semantic_index::expression::Expression;
use crate::semantic_index::symbol::{
FileScopeId, NodeWithScopeKey, NodeWithScopeRef, Scope, ScopeId, ScopedSymbolId, SymbolTable,
};
use crate::semantic_index::use_def::UseDefMap;
use crate::Db;

pub mod ast_ids;
mod builder;
pub mod definition;
pub mod expression;
pub mod symbol;
pub mod use_def;
mod use_def;

pub(crate) use self::use_def::UseDefMap;

type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), ()>;

Expand Down
20 changes: 10 additions & 10 deletions crates/red_knot_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ impl<'db> SemanticIndexBuilder<'db> {
self.current_use_def_map().snapshot()
}

fn flow_set(&mut self, state: &FlowSnapshot) {
self.current_use_def_map().set(state);
fn flow_restore(&mut self, state: FlowSnapshot) {
self.current_use_def_map().restore(state);
}

fn flow_merge(&mut self, state: &FlowSnapshot) {
fn flow_merge(&mut self, state: FlowSnapshot) {
self.current_use_def_map().merge(state);
}

Expand Down Expand Up @@ -397,20 +397,20 @@ where
let mut post_clauses: Vec<FlowSnapshot> = vec![self.flow_snapshot()];
for clause in &node.elif_else_clauses {
// we can only take an elif/else clause if none of the previous ones were taken
self.flow_set(&pre_if);
self.flow_restore(pre_if.clone());
self.visit_elif_else_clause(clause);
post_clauses.push(self.flow_snapshot());
if clause.test.is_none() {
last_clause_is_else = true;
}
}
let mut post_clause_iter = post_clauses.iter();
let mut post_clause_iter = post_clauses.into_iter();
if last_clause_is_else {
// if the last clause was an else, the pre_if state can't directly reach the
// post-state; we have to enter one of the clauses.
self.flow_set(post_clause_iter.next().unwrap());
// post-state; we must enter one of the clauses.
self.flow_restore(post_clause_iter.next().unwrap());
} else {
self.flow_set(&pre_if);
self.flow_restore(pre_if);
}
for post_clause_state in post_clause_iter {
self.flow_merge(post_clause_state);
Expand Down Expand Up @@ -483,9 +483,9 @@ where
let pre_if = self.flow_snapshot();
self.visit_expr(body);
let post_body = self.flow_snapshot();
self.flow_set(&pre_if);
self.flow_restore(pre_if);
self.visit_expr(orelse);
self.flow_merge(&post_body);
self.flow_merge(post_body);
}
_ => {
walk_expr(self, expr);
Expand Down
Loading

0 comments on commit b2a49d8

Please sign in to comment.