Dead Code Elimination in Rust
Dead code elimination is a crucial optimization technique in compilers. It identifies and removes code that has no effect on the program's output, reducing binary size and potentially improving performance. This challenge asks you to implement a simplified dead code elimination pass for a small subset of Rust code.
Problem Description
You are tasked with creating a function eliminate_dead_code that takes a vector of Rust expressions as input and returns a new vector with dead code removed. A dead code expression is one that is never executed. For simplicity, we'll define "dead code" as expressions that are unconditionally unreachable (e.g., after a return statement, panic!, or break within a loop). The expressions will be represented as strings. Your function should analyze the input expressions and filter out any that are deemed dead.
Key Requirements:
- Expression Representation: Expressions are represented as strings.
- Dead Code Detection: Identify expressions that are unconditionally unreachable. This includes expressions following:
returnstatementspanic!callsbreakstatements within loops (indicated byloop { ... }orwhile { ... }orfor { ... })
- Output: Return a new vector containing only the non-dead code expressions.
- No Code Modification: Do not modify the original expressions; create a new vector.
- Simplified Scope: Assume a single function scope. No nested functions or complex control flow beyond the listed constructs.
Expected Behavior:
The function should accurately identify and remove dead code based on the presence of return, panic!, or break statements within loops. It should handle multiple instances of these keywords and correctly identify the expressions that follow them as dead.
Edge Cases to Consider:
- Empty input vector.
- Expressions containing
return,panic!, orbreakbut not in the contexts described above (these should not be considered dead). - Expressions with multiple keywords (e.g.,
return panic!). Treat the entire expression as dead if any of the keywords are present. - Expressions containing comments. Comments should be ignored when determining dead code.
Examples
Example 1:
Input: ["let x = 5;", "return;", "let y = 10;"]
Output: ["let x = 5;"]
Explanation: "return;" and "let y = 10;" are dead code because they follow a `return` statement.
Example 2:
Input: ["let x = 5;", "panic!();", "let y = 10;", "println!(\"Hello\");"]
Output: ["let x = 5;", "println!(\"Hello\");"]
Explanation: "panic!();" and "let y = 10;" are dead code because they follow a `panic!` call.
Example 3:
Input: ["loop {", " let x = 5;", " break;", " let y = 10;", "}"]
Output: ["loop {", " let x = 5;", "}"]
Explanation: "break;" and "let y = 10;" are dead code because they follow a `break` statement within a loop.
Example 4:
Input: ["let x = 5;", "return; // comment", "let y = 10;"]
Output: ["let x = 5;"]
Explanation: The comment should be ignored, and "return; // comment" and "let y = 10;" are dead code.
Constraints
- Input Size: The input vector will contain at most 100 expressions.
- Expression Length: Each expression string will be at most 200 characters long.
- Keywords: The keywords
return,panic!,break,loop,while, andforare case-sensitive. - Performance: The solution should complete within 1 second for the given constraints. While not a primary focus, avoid excessively inefficient algorithms.
Notes
- You do not need to parse the Rust expressions into an Abstract Syntax Tree (AST). String manipulation is sufficient for this simplified problem.
- Focus on identifying the presence of the keywords and the expressions that immediately follow them.
- Consider using simple string searching techniques (e.g.,
contains()) to locate the keywords. - Regular expressions could be used, but are not strictly necessary and might add unnecessary complexity.
- The loop detection is simplified. Assume that
loop,while, andforkeywords are always followed by a code block enclosed in curly braces{}. - The goal is to demonstrate an understanding of dead code elimination principles, not to create a fully functional Rust compiler.