-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstant.rs
More file actions
32 lines (30 loc) · 1.14 KB
/
Copy pathconstant.rs
File metadata and controls
32 lines (30 loc) · 1.14 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
//! Syntax-level classification for expressions that can be evaluated without
//! runtime state.
use crate::{
ast::{Expr, ExprKind},
resolution::ProgramResolutions,
};
pub(crate) fn is_constant(expression: &Expr, resolutions: &ProgramResolutions) -> bool {
match &expression.kind {
ExprKind::None
| ExprKind::Bool(_)
| ExprKind::Int { .. }
| ExprKind::Float(_)
| ExprKind::String(_) => true,
ExprKind::Array(elements) => elements
.iter()
.all(|element| is_constant(element, resolutions)),
ExprKind::Range { start, end, .. } => {
is_constant(start, resolutions) && is_constant(end, resolutions)
}
ExprKind::Struct { fields, .. } => fields
.iter()
.all(|field| is_constant(&field.value, resolutions)),
ExprKind::Path(_) => resolutions.expression_enum(expression.id).is_some(),
ExprKind::Call { args, .. } => {
args.is_empty() && resolutions.expression_enum(expression.id).is_some()
}
ExprKind::Unary { expr, .. } => is_constant(expr, resolutions),
_ => false,
}
}