logicaffeine_compile/analysis/
escape.rs1use std::collections::HashMap;
28use crate::ast::stmt::{Stmt, Expr, Block};
29use crate::intern::{Interner, Symbol};
30use crate::token::Span;
31
32#[derive(Debug, Clone)]
34pub struct EscapeError {
35 pub kind: EscapeErrorKind,
36 pub span: Span,
37}
38
39#[derive(Debug, Clone)]
40pub enum EscapeErrorKind {
41 ReturnEscape {
43 variable: String,
44 zone_name: String,
45 },
46 AssignmentEscape {
48 variable: String,
49 target: String,
50 zone_name: String,
51 },
52}
53
54impl std::fmt::Display for EscapeError {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 match &self.kind {
57 EscapeErrorKind::ReturnEscape { variable, zone_name } => {
58 write!(
59 f,
60 "Reference '{}' cannot escape zone '{}'.\n\n\
61 Variables allocated inside a zone are deallocated when the zone ends.\n\
62 Returning them would create a dangling reference.\n\n\
63 Tip: Copy the data if you need it outside the zone.",
64 variable, zone_name
65 )
66 }
67 EscapeErrorKind::AssignmentEscape { variable, target, zone_name } => {
68 write!(
69 f,
70 "Reference '{}' cannot escape zone '{}' via assignment to '{}'.\n\n\
71 Variables allocated inside a zone are deallocated when the zone ends.\n\
72 Assigning them to outer scope variables would create a dangling reference.\n\n\
73 Tip: Copy the data if you need it outside the zone.",
74 variable, zone_name, target
75 )
76 }
77 }
78 }
79}
80
81impl std::error::Error for EscapeError {}
82
83pub struct EscapeChecker<'a> {
85 zone_depth: HashMap<Symbol, usize>,
87 current_depth: usize,
89 zone_stack: Vec<Symbol>,
91 interner: &'a Interner,
93}
94
95impl<'a> EscapeChecker<'a> {
96 pub fn new(interner: &'a Interner) -> Self {
98 Self {
99 zone_depth: HashMap::new(),
100 current_depth: 0,
101 zone_stack: Vec::new(),
102 interner,
103 }
104 }
105
106 pub fn check_program(&mut self, stmts: &[Stmt<'_>]) -> Result<(), EscapeError> {
108 self.check_block(stmts)
109 }
110
111 fn check_block(&mut self, stmts: &[Stmt<'_>]) -> Result<(), EscapeError> {
113 for stmt in stmts {
114 self.check_stmt(stmt)?;
115 }
116 Ok(())
117 }
118
119 fn check_stmt(&mut self, stmt: &Stmt<'_>) -> Result<(), EscapeError> {
121 match stmt {
122 Stmt::Zone { name, body, .. } => {
123 self.current_depth += 1;
125 self.zone_stack.push(*name);
126
127 self.check_block(body)?;
129
130 self.zone_stack.pop();
132 self.current_depth -= 1;
133 }
134
135 Stmt::Let { var, .. } => {
136 self.zone_depth.insert(*var, self.current_depth);
138 }
139
140 Stmt::Return { value: Some(expr) } => {
141 self.check_no_escape(expr, 0)?;
143 }
144
145 Stmt::Set { target, value } => {
146 let target_depth = self.zone_depth.get(target).copied().unwrap_or(0);
148 self.check_no_escape_with_target(value, target_depth, *target)?;
149 }
150
151 Stmt::If { then_block, else_block, .. } => {
153 self.check_block(then_block)?;
154 if let Some(else_b) = else_block {
155 self.check_block(else_b)?;
156 }
157 }
158
159 Stmt::While { body, .. } => {
160 self.check_block(body)?;
161 }
162
163 Stmt::Repeat { body, .. } => {
164 self.check_block(body)?;
165 }
166
167 Stmt::Inspect { arms, .. } => {
168 for arm in arms {
169 self.check_block(arm.body)?;
170 }
171 }
172
173 Stmt::Escape { .. } => {}
175
176 _ => {}
178 }
179 Ok(())
180 }
181
182 fn check_no_escape(&self, expr: &Expr<'_>, max_depth: usize) -> Result<(), EscapeError> {
184 match expr {
185 Expr::Identifier(sym) => {
186 if let Some(&depth) = self.zone_depth.get(sym) {
187 if depth > max_depth && depth > 0 {
188 let zone_name = self.zone_stack.get(depth - 1)
190 .map(|s| self.interner.resolve(*s).to_string())
191 .unwrap_or_else(|| "unknown".to_string());
192 let var_name = self.interner.resolve(*sym).to_string();
193 return Err(EscapeError {
194 kind: EscapeErrorKind::ReturnEscape {
195 variable: var_name,
196 zone_name,
197 },
198 span: Span::default(),
199 });
200 }
201 }
202 }
203
204 Expr::BinaryOp { left, right, .. } => {
206 self.check_no_escape(left, max_depth)?;
207 self.check_no_escape(right, max_depth)?;
208 }
209
210 Expr::Call { args, .. } => {
211 for arg in args {
212 self.check_no_escape(arg, max_depth)?;
213 }
214 }
215
216 Expr::FieldAccess { object, .. } => {
217 self.check_no_escape(object, max_depth)?;
218 }
219
220 Expr::Index { collection, index } => {
221 self.check_no_escape(collection, max_depth)?;
222 self.check_no_escape(index, max_depth)?;
223 }
224
225 Expr::Slice { collection, start, end } => {
226 self.check_no_escape(collection, max_depth)?;
227 self.check_no_escape(start, max_depth)?;
228 self.check_no_escape(end, max_depth)?;
229 }
230
231 Expr::Copy { expr } | Expr::Give { value: expr } | Expr::Length { collection: expr }
232 | Expr::Not { operand: expr } => {
233 self.check_no_escape(expr, max_depth)?;
234 }
235
236 Expr::List(items) | Expr::Tuple(items) => {
237 for item in items {
238 self.check_no_escape(item, max_depth)?;
239 }
240 }
241
242 Expr::Range { start, end } => {
243 self.check_no_escape(start, max_depth)?;
244 self.check_no_escape(end, max_depth)?;
245 }
246
247 Expr::New { init_fields, .. } => {
248 for (_, expr) in init_fields {
249 self.check_no_escape(expr, max_depth)?;
250 }
251 }
252
253 Expr::NewVariant { fields, .. } => {
254 for (_, expr) in fields {
255 self.check_no_escape(expr, max_depth)?;
256 }
257 }
258
259 Expr::ManifestOf { zone } => {
260 self.check_no_escape(zone, max_depth)?;
261 }
262
263 Expr::ChunkAt { index, zone } => {
264 self.check_no_escape(index, max_depth)?;
265 self.check_no_escape(zone, max_depth)?;
266 }
267
268 Expr::Contains { collection, value } => {
269 self.check_no_escape(collection, max_depth)?;
270 self.check_no_escape(value, max_depth)?;
271 }
272
273 Expr::Union { left, right } | Expr::Intersection { left, right } => {
274 self.check_no_escape(left, max_depth)?;
275 self.check_no_escape(right, max_depth)?;
276 }
277
278 Expr::WithCapacity { value, capacity } => {
279 self.check_no_escape(value, max_depth)?;
280 self.check_no_escape(capacity, max_depth)?;
281 }
282
283 Expr::OptionSome { value } => {
284 self.check_no_escape(value, max_depth)?;
285 }
286 Expr::OptionNone => {}
287
288 Expr::Escape { .. } => {}
290
291 Expr::Closure { body, .. } => {
292 match body {
293 crate::ast::stmt::ClosureBody::Expression(expr) => {
294 self.check_no_escape(expr, max_depth)?;
295 }
296 crate::ast::stmt::ClosureBody::Block(_) => {
297 }
301 }
302 }
303
304 Expr::CallExpr { callee, args } => {
305 self.check_no_escape(callee, max_depth)?;
306 for arg in args {
307 self.check_no_escape(arg, max_depth)?;
308 }
309 }
310
311 Expr::InterpolatedString(parts) => {
312 for part in parts {
313 if let crate::ast::stmt::StringPart::Expr { value, .. } = part {
314 self.check_no_escape(value, max_depth)?;
315 }
316 }
317 }
318
319 Expr::Literal(_) => {}
321 }
322 Ok(())
323 }
324
325 fn check_no_escape_with_target(
327 &self,
328 expr: &Expr<'_>,
329 max_depth: usize,
330 target: Symbol,
331 ) -> Result<(), EscapeError> {
332 match expr {
333 Expr::Identifier(sym) => {
334 if let Some(&depth) = self.zone_depth.get(sym) {
335 if depth > max_depth && depth > 0 {
336 let zone_name = self.zone_stack.get(depth - 1)
337 .map(|s| self.interner.resolve(*s).to_string())
338 .unwrap_or_else(|| "unknown".to_string());
339 let var_name = self.interner.resolve(*sym).to_string();
340 let target_name = self.interner.resolve(target).to_string();
341 return Err(EscapeError {
342 kind: EscapeErrorKind::AssignmentEscape {
343 variable: var_name,
344 target: target_name,
345 zone_name,
346 },
347 span: Span::default(),
348 });
349 }
350 }
351 }
352 _ => self.check_no_escape(expr, max_depth)?,
354 }
355 Ok(())
356 }
357}
358
359#[cfg(test)]
360mod tests {
361 use super::*;
362
363 #[test]
367 fn test_escape_checker_basic() {
368 use crate::intern::Interner;
369
370 let mut interner = Interner::new();
371 let checker = EscapeChecker::new(&interner);
372
373 assert_eq!(checker.current_depth, 0);
375 assert!(checker.zone_depth.is_empty());
376 }
377}