/
visitor.rs
145 lines (135 loc) · 4.99 KB
/
visitor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_ast::{Expr, ExprKind, Location, Stmt, StmtKind};
use crate::ast::visitor;
use crate::ast::visitor::Visitor;
#[derive(Default)]
pub struct Stack<'a> {
pub returns: Vec<(&'a Stmt, Option<&'a Expr>)>,
pub ifs: Vec<&'a Stmt>,
pub elifs: Vec<&'a Stmt>,
pub refs: FxHashMap<&'a str, Vec<Location>>,
pub non_locals: FxHashSet<&'a str>,
pub assigns: FxHashMap<&'a str, Vec<Location>>,
pub loops: Vec<(Location, Location)>,
pub tries: Vec<(Location, Location)>,
}
#[derive(Default)]
pub struct ReturnVisitor<'a> {
pub stack: Stack<'a>,
// If we're in an f-string, the location of the defining expression.
in_f_string: Option<Location>,
}
impl<'a> ReturnVisitor<'a> {
fn visit_assign_target(&mut self, expr: &'a Expr) {
match &expr.node {
ExprKind::Tuple { elts, .. } => {
for elt in elts {
self.visit_assign_target(elt);
}
return;
}
ExprKind::Name { id, .. } => {
self.stack
.assigns
.entry(id)
.or_insert_with(Vec::new)
.push(self.in_f_string.unwrap_or(expr.location));
return;
}
_ => {}
}
visitor::walk_expr(self, expr);
}
}
impl<'a> Visitor<'a> for ReturnVisitor<'a> {
fn visit_stmt(&mut self, stmt: &'a Stmt) {
match &stmt.node {
StmtKind::Global { names } | StmtKind::Nonlocal { names } => {
self.stack
.non_locals
.extend(names.iter().map(std::string::String::as_str));
}
StmtKind::FunctionDef { .. } | StmtKind::AsyncFunctionDef { .. } => {
// Don't recurse.
}
StmtKind::Return { value } => {
self.stack
.returns
.push((stmt, value.as_ref().map(|expr| &**expr)));
visitor::walk_stmt(self, stmt);
}
StmtKind::If { orelse, .. } => {
if orelse.len() == 1 && matches!(orelse.first().unwrap().node, StmtKind::If { .. })
{
self.stack.elifs.push(stmt);
} else {
self.stack.ifs.push(stmt);
}
visitor::walk_stmt(self, stmt);
}
StmtKind::Assign { targets, value, .. } => {
if let ExprKind::Name { id, .. } = &value.node {
self.stack
.refs
.entry(id)
.or_insert_with(Vec::new)
.push(self.in_f_string.unwrap_or(value.location));
}
visitor::walk_expr(self, value);
if let Some(target) = targets.first() {
// Skip unpacking assignments, like `x, y = my_object`.
if matches!(target.node, ExprKind::Tuple { .. })
&& !matches!(value.node, ExprKind::Tuple { .. })
{
return;
}
self.visit_assign_target(target);
}
}
StmtKind::For { .. } | StmtKind::AsyncFor { .. } | StmtKind::While { .. } => {
self.stack
.loops
.push((stmt.location, stmt.end_location.unwrap()));
visitor::walk_stmt(self, stmt);
}
StmtKind::Try { .. } => {
self.stack
.tries
.push((stmt.location, stmt.end_location.unwrap()));
visitor::walk_stmt(self, stmt);
}
_ => {
visitor::walk_stmt(self, stmt);
}
}
}
fn visit_expr(&mut self, expr: &'a Expr) {
match &expr.node {
ExprKind::Call { .. } => {
// Arbitrary function calls can have side effects, so we conservatively treat
// every function call as a reference to every known variable.
for name in self.stack.assigns.keys() {
self.stack
.refs
.entry(name)
.or_insert_with(Vec::new)
.push(self.in_f_string.unwrap_or(expr.location));
}
}
ExprKind::Name { id, .. } => {
self.stack
.refs
.entry(id)
.or_insert_with(Vec::new)
.push(self.in_f_string.unwrap_or(expr.location));
}
ExprKind::JoinedStr { .. } => {
let prev_in_f_string = self.in_f_string;
self.in_f_string = Some(expr.location);
visitor::walk_expr(self, expr);
self.in_f_string = prev_in_f_string;
}
_ => visitor::walk_expr(self, expr),
}
}
}