Hone logo
Hone
Problems

Implementing Basic Escape Analysis in Rust

Escape analysis is a crucial optimization technique in compilers that determines whether a value allocated on the stack can be safely moved to the heap. This challenge asks you to implement a simplified version of escape analysis for a small subset of Rust-like code. Successfully implementing this will give you a deeper understanding of memory management and compiler optimizations.

Problem Description

You are tasked with writing a function analyze_escape(borrow: &str) that takes a string representing a simplified Rust-like code snippet as input and determines if a variable "escapes" – meaning it's potentially moved to the heap. The code snippet will only contain variable declarations and assignments. The focus is on identifying if a variable declared within a scope is used outside that scope, indicating a potential escape.

The input string will follow a simplified syntax:

  • let <variable_name> = <expression>; where <expression> can be a literal (e.g., "10", "hello") or another variable name.
  • Variables are case-sensitive.
  • Multiple statements are separated by semicolons.
  • The scope is implicitly defined by the presence of semicolons. Each statement is considered within its own scope.

The function should return true if any variable declared within the input string escapes its scope, and false otherwise. An escape occurs when a variable is declared within a scope and then used (assigned to or referenced) outside that scope.

Key Requirements:

  • Parse the input string to identify variable declarations and assignments.
  • Maintain a set of variables declared within the current scope.
  • Track variables used outside their declared scope.
  • Handle simple variable assignments (e.g., let x = 10; let y = x;).
  • Assume no function calls or complex control flow (e.g., loops, conditionals).

Expected Behavior:

The function should accurately identify escapes based on the simplified syntax. If no escapes are detected, it should return false.

Edge Cases to Consider:

  • Variables declared and immediately used within the same scope should not be considered escapes.
  • Multiple declarations of the same variable should be handled correctly.
  • Empty input string should return false.
  • Input with invalid syntax (though not strictly required to handle) should not cause a panic; return false in such cases.

Examples

Example 1:

Input: "let x = 10; let y = x;"
Output: true
Explanation: 'x' is declared within the first scope and then used in the second scope (assigned to 'y'), indicating an escape.

Example 2:

Input: "let x = 10; let y = 20;"
Output: false
Explanation: Both 'x' and 'y' are declared and used within the same scope. No escape occurs.

Example 3:

Input: "let x = 10; let y = x; let z = 30;"
Output: true
Explanation: 'x' is declared in the first scope and used in the second scope (assigned to 'y').

Example 4:

Input: "let x = 10;"
Output: false
Explanation: 'x' is declared and used within the same scope.

Example 5:

Input: ""
Output: false
Explanation: Empty input.

Constraints

  • The input string will be no longer than 256 characters.
  • Variable names will consist of lowercase letters only.
  • Expressions will be simple literals or variable names.
  • The code should be reasonably efficient; avoid unnecessary iterations or complex data structures. O(n) time complexity is expected, where n is the length of the input string.
  • The code should be written in Rust.

Notes

  • This is a simplified version of escape analysis. Real-world escape analysis is significantly more complex and considers various factors like lifetimes, borrowing, and aliasing.
  • Focus on the core logic of identifying escapes based on scope.
  • You can use a HashSet to efficiently track declared variables within a scope.
  • Consider using string splitting and parsing techniques to extract variable names and expressions.
  • Error handling for invalid syntax is not strictly required, but returning false in such cases is preferred.
  • The scope is delimited by semicolons. Each statement is a new scope.
Loading editor...
rust