-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstant.rs
More file actions
120 lines (117 loc) · 3.96 KB
/
Copy pathconstant.rs
File metadata and controls
120 lines (117 loc) · 3.96 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
//! Syntax-level classification for expressions that can be evaluated without
//! runtime state.
use crate::{
ast::{Expr, ExprKind, FunctionId, Program, Stmt},
resolution::ProgramResolutions,
stdlib::{Implementation, StandardLibrary},
};
pub(crate) fn is_constant(
expression: &Expr,
resolutions: &ProgramResolutions,
program: &Program,
library: &StandardLibrary,
) -> bool {
constant_expression(
expression,
resolutions,
program,
library,
&[],
&mut Vec::new(),
)
}
// Constructor functions may only return another constant expression. This is
// deliberately narrower than general purity: globals, host reads, mutation,
// control flow, and recursive evaluation cannot enter provider configuration.
fn constant_expression(
expression: &Expr,
resolutions: &ProgramResolutions,
program: &Program,
library: &StandardLibrary,
parameters: &[&str],
active: &mut Vec<FunctionId>,
) -> bool {
let mut constant = |value: &Expr| {
constant_expression(value, resolutions, program, library, parameters, active)
};
match &expression.kind {
ExprKind::None
| ExprKind::Bool(_)
| ExprKind::Int { .. }
| ExprKind::Float(_)
| ExprKind::String(_) => true,
ExprKind::Array(elements) => elements.iter().all(&mut constant),
ExprKind::Range { start, end, .. } => constant(start) && constant(end),
ExprKind::Struct { fields, .. } => fields.iter().all(|field| constant(&field.value)),
ExprKind::Path(path) => {
resolutions.expression_enum(expression.id).is_some()
|| matches!(path.as_slice(), [name] if parameters.contains(&name.as_str()))
}
ExprKind::Call {
callee,
receiver: None,
args,
..
} => {
if !args.iter().all(&mut constant) {
return false;
}
if resolutions.expression_enum(expression.id).is_some()
|| (callee.as_slice() == ["Some"] && args.len() == 1)
{
return true;
}
let qualified = callee.join(".");
let name = match library
.item_by_name(&qualified)
.map(|item| item.implementation)
{
Some(Implementation::LibraryBody { function_name, .. }) => function_name,
Some(_) => return false,
None if callee.len() == 1 => &qualified,
None => return false,
};
let Some(function) = program
.functions
.iter()
.find(|function| function.name == name)
else {
return false;
};
if function.return_is_async
|| function.return_is_iterator
|| active.contains(&function.id)
|| active.len() >= 64
|| function.params.len() != args.len()
{
return false;
}
let [Stmt::Expression(body)] = function.body.statements.as_slice() else {
return false;
};
let ExprKind::Return(Some(value)) = &body.kind else {
return false;
};
let Some(parameters) = function
.params
.iter()
.map(|parameter| {
parameter
.binding
.simple_binding()
.map(|binding| binding.name.as_str())
})
.collect::<Option<Vec<_>>>()
else {
return false;
};
active.push(function.id);
let result =
constant_expression(value, resolutions, program, library, ¶meters, active);
active.pop();
result
}
ExprKind::Unary { expr, .. } => constant(expr),
_ => false,
}
}