-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathbindings.rs
More file actions
432 lines (410 loc) · 16.2 KB
/
Copy pathbindings.rs
File metadata and controls
432 lines (410 loc) · 16.2 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::collections::HashMap;
use dupe::Dupe;
use starlark_map::small_map::SmallMap;
use starlark_syntax::syntax::ast::AssignOp;
use starlark_syntax::syntax::ast::AssignP;
use starlark_syntax::syntax::ast::AssignTargetP;
use starlark_syntax::syntax::ast::ClauseP;
use starlark_syntax::syntax::ast::DefP;
use starlark_syntax::syntax::ast::ExprP;
use starlark_syntax::syntax::ast::ForClauseP;
use starlark_syntax::syntax::ast::ForP;
use starlark_syntax::syntax::ast::IdentP;
use starlark_syntax::syntax::ast::StmtP;
use starlark_syntax::syntax::def::DefParamKind;
use starlark_syntax::syntax::def::DefParams;
use starlark_syntax::syntax::def::DefRegularParamMode;
use starlark_syntax::syntax::uniplate::Visit;
use crate::codemap::CodeMap;
use crate::codemap::Span;
use crate::codemap::Spanned;
use crate::eval::compiler::scope::BindingId;
use crate::eval::compiler::scope::ResolvedIdent;
use crate::eval::compiler::scope::payload::CstAssignIdent;
use crate::eval::compiler::scope::payload::CstAssignIdentExt;
use crate::eval::compiler::scope::payload::CstAssignTarget;
use crate::eval::compiler::scope::payload::CstExpr;
use crate::eval::compiler::scope::payload::CstPayload;
use crate::eval::compiler::scope::payload::CstStmt;
use crate::eval::compiler::scope::payload::CstTypeExpr;
use crate::typing::ParamSpec;
use crate::typing::TyBasic;
use crate::typing::arc_ty::ArcTy;
use crate::typing::callable_param::ParamIsRequired;
use crate::typing::error::InternalError;
use crate::typing::mode::TypecheckMode;
use crate::typing::tuple::TyTuple;
use crate::typing::ty::Approximation;
use crate::typing::ty::Ty;
use crate::util::arc_str::ArcStr;
#[derive(Clone)]
pub(crate) enum BindExpr<'a> {
Expr(&'a CstExpr),
/// Get this position from the expression
GetIndex(usize, Box<BindExpr<'a>>),
Iter(Box<BindExpr<'a>>),
AssignModify(&'a CstAssignTarget, AssignOp, &'a CstExpr),
/// Set this index in the variable
SetIndex(BindingId, &'a CstExpr, Box<BindExpr<'a>>),
ListAppend(BindingId, &'a CstExpr),
ListExtend(BindingId, &'a CstExpr),
}
impl<'a> BindExpr<'a> {
pub(crate) fn span(&self) -> Span {
match self {
BindExpr::Expr(x) => x.span,
BindExpr::GetIndex(_, x) => x.span(),
BindExpr::Iter(x) => x.span(),
BindExpr::AssignModify(x, _, _) => x.span,
BindExpr::SetIndex(_, x, _) => x.span,
BindExpr::ListAppend(_, x) => x.span,
BindExpr::ListExtend(_, x) => x.span,
}
}
}
#[derive(Default)]
pub(crate) struct Bindings<'a> {
pub(crate) expressions: SmallMap<BindingId, Vec<BindExpr<'a>>>,
/// Non-inferred types of bindings: from `load`,
/// or from variable or function parameter type annotations.
pub(crate) types: HashMap<BindingId, Ty>,
/// Expressions which need to be typechecked, but which are not used
/// in assignments or in other expressions.
/// For example `expr` in:
///
/// ```python
/// if expr: ...
/// ```
pub(crate) check: Vec<&'a CstExpr>,
pub(crate) check_type: Vec<(Span, Option<&'a CstExpr>, Ty)>,
}
/// Basically a child `def`.
pub(crate) struct ChildDef<'a> {
pub(crate) body: &'a CstStmt,
pub(crate) return_type: Ty,
pub(crate) param_types: HashMap<BindingId, Ty>,
}
pub(crate) struct BindingsCollect<'a, 'b> {
pub(crate) bindings: Bindings<'a>,
pub(crate) approximations: &'b mut Vec<Approximation>,
pub(crate) children_sink: &'b mut Vec<ChildDef<'a>>,
}
impl<'a, 'b> BindingsCollect<'a, 'b> {
/// Collect all the assignments to variables in a scope.
///
/// This function only fails on internal errors.
pub(crate) fn collect_scope(
scope: &'a CstStmt,
return_type: &Ty,
visible: &HashMap<BindingId, Ty>,
typecheck_mode: TypecheckMode,
codemap: &CodeMap,
approximations: &'b mut Vec<Approximation>,
children_sink: &'b mut Vec<ChildDef<'a>>,
) -> Result<Bindings<'a>, InternalError> {
let mut res = BindingsCollect {
bindings: Bindings::default(),
approximations,
children_sink,
};
res.bindings.types = visible.clone();
res.visit(Visit::Stmt(scope), return_type, typecheck_mode, codemap)?;
Ok(res.bindings)
}
fn assign(
&mut self,
lhs: &'a CstAssignTarget,
rhs: BindExpr<'a>,
codemap: &CodeMap,
) -> Result<(), InternalError> {
match &**lhs {
AssignTargetP::Identifier(x) => {
self.bindings
.expressions
.entry(x.resolved_binding_id(codemap)?)
.or_default()
.push(rhs);
}
AssignTargetP::Tuple(xs) => {
for (i, x) in xs.iter().enumerate() {
self.assign(x, BindExpr::GetIndex(i, Box::new(rhs.clone())), codemap)?;
}
}
AssignTargetP::Index(array_index) => match &*array_index.0 {
ExprP::Identifier(Spanned {
span: _,
node:
IdentP {
ident: _name,
payload: Some(ResolvedIdent::Slot(_, ident)),
},
}) => {
self.bindings
.expressions
.entry(*ident)
.or_default()
.push(BindExpr::SetIndex(*ident, &array_index.1, Box::new(rhs)));
}
_ => {
self.approximations.push(Approximation::new(
"Underapproximation",
"a.b[x] = .. not handled",
));
}
},
AssignTargetP::Dot(_, _) => {
self.approximations.push(Approximation::new(
"Underapproximation",
"a.b = .. not handled",
));
}
}
Ok(())
}
/// Type must be populated earlier.
fn resolved_ty(
expr: &CstTypeExpr,
typecheck_mode: TypecheckMode,
codemap: &CodeMap,
) -> Result<Ty, InternalError> {
let ty = match typecheck_mode {
TypecheckMode::Lint => expr.payload.typechecker_ty.clone(),
TypecheckMode::Compiler => expr.payload.compiler_ty.clone(),
};
match ty {
Some(ty) => Ok(ty),
None => Err(InternalError::msg(
"Type must be populated earlier",
expr.span,
codemap,
)),
}
}
fn resolve_ty_opt(
expr: Option<&CstTypeExpr>,
typecheck_mode: TypecheckMode,
codemap: &CodeMap,
) -> Result<Ty, InternalError> {
match expr {
Some(expr) => Self::resolved_ty(expr, typecheck_mode, codemap),
None => Ok(Ty::any()),
}
}
/// Insert an explicit type for the binding.
fn type_annotation(
&mut self,
binding: &CstAssignIdent,
ty: Ty,
_error_span: Span,
codemap: &CodeMap,
) -> Result<(), InternalError> {
let resolved_id = binding.resolved_binding_id(codemap)?;
// FIXME: This could be duplicated if you declare the type of a variable twice.
// We would only see the second one.
self.bindings.types.insert(resolved_id, ty);
Ok(())
}
fn visit_def(
&mut self,
def: &'a DefP<CstPayload>,
typecheck_mode: TypecheckMode,
codemap: &CodeMap,
) -> Result<(), InternalError> {
let DefP {
name,
params,
return_type,
..
} = def;
let DefParams { params, indices: _ } =
DefParams::unpack(params, codemap).map_err(InternalError::from_eval_exception)?;
let mut pos_only = Vec::new();
let mut pos_or_named = Vec::new();
let mut args = None;
let mut named_only = Vec::new();
let mut kwargs = None;
let mut param_types = HashMap::new();
for p in params {
let name = &p.node.ident;
let ty = p.node.ty;
let ty = Self::resolve_ty_opt(ty, typecheck_mode, codemap)?;
let ty = match &p.node.kind {
DefParamKind::Regular(mode, default_value) => {
let required = match default_value.is_some() {
true => ParamIsRequired::No,
false => ParamIsRequired::Yes,
};
match mode {
DefRegularParamMode::PosOnly => {
pos_only.push((required, ty.dupe()));
}
DefRegularParamMode::PosOrName => {
pos_or_named.push((
ArcStr::from(name.ident.as_str()),
required,
ty.dupe(),
));
}
DefRegularParamMode::NameOnly => {
named_only.push((
ArcStr::from(name.ident.as_str()),
required,
ty.dupe(),
));
}
}
ty
}
DefParamKind::Args => {
// There is the type we require people calling us use (usually any)
// and then separately the type we are when we are running (always tuple)
args = Some(ty.dupe());
Ty::basic(TyBasic::Tuple(TyTuple::Of(ArcTy::new(ty))))
}
DefParamKind::Kwargs => {
let var_ty = Ty::dict(Ty::string(), ty.clone());
kwargs = Some(ty.dupe());
var_ty
}
};
param_types.insert(name.resolved_binding_id(codemap)?, ty);
}
let params2 = ParamSpec::new_parts(pos_only, pos_or_named, args, named_only, kwargs)
.map_err(|e| InternalError::from_error(e, def.signature_span(), codemap))?;
let ret_ty = Self::resolve_ty_opt(return_type.as_deref(), typecheck_mode, codemap)?;
// Defining a function is like an annotated assignment
//
// foo: Function[...] = lambda x, y: ...
//
// (even if there are no type annotations, because even just having parameters
// is a kind of type annotation).
//
self.type_annotation(
name,
Ty::function(params2, ret_ty.clone()),
name.span,
codemap,
)?;
def.visit_header_err(|x| self.visit(x, &ret_ty, typecheck_mode, codemap))?;
// Function body just gets added to the queue.
self.children_sink.push(ChildDef {
body: &def.body,
param_types,
return_type: ret_ty,
});
Ok(())
}
fn visit(
&mut self,
x: Visit<'a, CstPayload>,
return_type: &Ty,
typecheck_mode: TypecheckMode,
codemap: &CodeMap,
) -> Result<(), InternalError> {
match x {
Visit::Stmt(x) => match &**x {
StmtP::Assign(AssignP { lhs, ty, rhs }) => {
if let Some(ty) = ty {
let ty2 = Self::resolved_ty(ty, typecheck_mode, codemap)?;
self.bindings
.check_type
.push((ty.span, Some(rhs), ty2.clone()));
if let AssignTargetP::Identifier(id) = &**lhs {
self.type_annotation(id, ty2, lhs.span.merge(ty.span), codemap)?;
}
}
self.assign(lhs, BindExpr::Expr(rhs), codemap)?
}
StmtP::AssignModify(lhs, op, rhs) => {
self.assign(lhs, BindExpr::AssignModify(lhs, *op, rhs), codemap)?
}
StmtP::For(ForP { var, over, body: _ }) => {
self.assign(var, BindExpr::Iter(Box::new(BindExpr::Expr(over))), codemap)?
}
StmtP::Def(def) => {
self.visit_def(def, typecheck_mode, codemap)?;
// We do our own visit_children, with a different return type
return Ok(());
}
StmtP::Load(..) => {}
StmtP::Return(ret) => {
self.bindings
.check_type
.push((x.span, ret.as_ref(), return_type.clone()))
}
StmtP::Expression(x) => {
// We want to find ident.append(), ident.extend(), ident.extend()
// to fake up a BindExpr::ListAppend/ListExtend
// so that mutating list operations aren't invisible to us
if let ExprP::Call(fun, args) = &**x {
if let ExprP::Dot(id, attr) = &***fun {
if let ExprP::Identifier(id) = &id.node {
let res = match attr.as_str() {
"append" if args.args.len() == 1 => Some((false, 0)),
"insert" if args.args.len() == 2 => Some((false, 1)),
"extend" if args.args.len() == 1 => Some((true, 0)),
_ => None,
};
if let Some((extend, arg)) = res {
if let ResolvedIdent::Slot(_, id) =
id.node.payload.as_ref().unwrap()
{
let bind = if extend {
BindExpr::ListExtend(*id, args.args[arg].expr())
} else {
BindExpr::ListAppend(*id, args.args[arg].expr())
};
self.bindings.expressions.entry(*id).or_default().push(bind)
}
}
}
}
}
self.bindings.check.push(x)
}
StmtP::If(x, _) => self.bindings.check.push(x),
StmtP::IfElse(x, _) => self.bindings.check.push(x),
_ => {}
},
Visit::Expr(x) => match &**x {
ExprP::ListComprehension(_, for1, clauses)
| ExprP::DictComprehension(_, for1, clauses) => {
fn get_for_clause(x: &ClauseP<CstPayload>) -> Option<&ForClauseP<CstPayload>> {
match x {
ClauseP::For(x) => Some(x),
_ => None,
}
}
for x in
std::iter::once(&**for1).chain(clauses.iter().filter_map(get_for_clause))
{
self.assign(
&x.var,
BindExpr::Iter(Box::new(BindExpr::Expr(&x.over))),
codemap,
)?
}
}
_ => {}
},
}
x.visit_children_err(|x| self.visit(x, return_type, typecheck_mode, codemap))?;
Ok(())
}
}