Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect unreachable code after fail() #353

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 78 additions & 27 deletions crates/starpls_hir/src/def/codeflow.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,10 @@ pub(crate) enum FlowNode {
Loop {
antecedents: Vec<FlowNodeId>,
},
Call {
expr: ExprId,
antecedent: FlowNodeId,
},
Unreachable,
}

@@ -49,7 +53,7 @@ pub(crate) struct CodeFlowGraph {
}

#[allow(unused)]
struct CodeFlowLowerCtx<'a> {
struct CodeFlowLoweringContext<'a> {
module: &'a Module,
scopes: &'a Scopes,
result: CodeFlowGraph,
@@ -59,7 +63,7 @@ struct CodeFlowLowerCtx<'a> {
curr_continue_target: Option<FlowNodeId>,
}

impl<'a> CodeFlowLowerCtx<'a> {
impl<'a> CodeFlowLoweringContext<'a> {
fn new(module: &'a Module, scopes: &'a Scopes) -> Self {
let mut flow_nodes = Arena::new();
let unreachable_node = flow_nodes.alloc(FlowNode::Unreachable);
@@ -68,7 +72,7 @@ impl<'a> CodeFlowLowerCtx<'a> {
flow_nodes,
hir_to_flow_node: Default::default(),
};
CodeFlowLowerCtx {
CodeFlowLoweringContext {
module,
scopes,
result: cfg,
@@ -228,6 +232,15 @@ impl<'a> CodeFlowLowerCtx<'a> {
self.lower_comp_clauses(comp_clauses);
self.lower_expr(*expr);
}
node @ Expr::Call { .. } => {
node.walk_child_exprs(|expr| {
self.lower_expr(expr);
});
self.curr_node = self.new_flow_node(FlowNode::Call {
expr,
antecedent: self.curr_node,
})
}
expr => expr.walk_child_exprs(|expr| {
self.lower_expr(expr);
}),
@@ -242,14 +255,13 @@ impl<'a> CodeFlowLowerCtx<'a> {
fn lower_assignment_target(&mut self, expr: ExprId, source: ExprId) {
match &self.module[expr] {
Expr::Name { ref name } => {
let assign_node = self.new_flow_node(FlowNode::Assign {
self.curr_node = self.new_flow_node(FlowNode::Assign {
expr,
name: name.clone(),
execution_scope: self.scopes.execution_scope_for_hir_id(expr).unwrap(),
source,
antecedent: self.curr_node,
});
self.curr_node = assign_node;
self.result
.hir_to_flow_node
.insert(expr.into(), self.curr_node);
@@ -331,7 +343,7 @@ impl<'a> CodeFlowLowerCtx<'a> {
}

pub(crate) fn lower_to_code_flow_graph(module: &Module, scopes: &Scopes) -> CodeFlowGraph {
let mut cx = CodeFlowLowerCtx::new(module, scopes);
let mut cx = CodeFlowLoweringContext::new(module, scopes);
cx.lower_stmts(&module.top_level);
cx.result
.hir_to_flow_node
@@ -515,7 +527,7 @@ y = 4
fn test_list_comp() {
check(
r#"
nums = [x for x in range(10)]
nums = [x for x in [1, 2, 3]]
"#,
expect![[r#"
def main():
@@ -530,12 +542,12 @@ nums = [x for x in range(10)]
}
'bb2: {
data: Assign { expr: Id { idx: 5 }, name: Name("x"), execution_scope: Comp(Id { idx: 6 }), source: Id { idx: 4 }, antecedent: Id { idx: 1 } }
data: Assign { expr: Id { idx: 6 }, name: Name("x"), execution_scope: Comp(Id { idx: 7 }), source: Id { idx: 5 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
'bb3: {
data: Assign { expr: Id { idx: 0 }, name: Name("nums"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 2 } }
data: Assign { expr: Id { idx: 0 }, name: Name("nums"), execution_scope: Module, source: Id { idx: 7 }, antecedent: Id { idx: 2 } }
antecedents: ['bb2]
}
@@ -547,8 +559,8 @@ nums = [x for x in range(10)]
fn test_for_stmt() {
check(
r#"
for x, y in [[1, 2], [3, 4]]:
nums = [(x * y * i) for i in range(5)]
for x, y in [[1, 2, 3], [3, 4]]:
nums = [(x * y * i) for i in [1, 2, 3]]
"#,
expect![[r#"
def main():
@@ -563,12 +575,12 @@ for x, y in [[1, 2], [3, 4]]:
}
'bb2: {
data: Assign { expr: Id { idx: 7 }, name: Name("x"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 1 } }
data: Assign { expr: Id { idx: 8 }, name: Name("x"), execution_scope: Module, source: Id { idx: 7 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
'bb3: {
data: Assign { expr: Id { idx: 8 }, name: Name("y"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 2 } }
data: Assign { expr: Id { idx: 9 }, name: Name("y"), execution_scope: Module, source: Id { idx: 7 }, antecedent: Id { idx: 2 } }
antecedents: ['bb2]
}
@@ -583,12 +595,12 @@ for x, y in [[1, 2], [3, 4]]:
}
'bb6: {
data: Assign { expr: Id { idx: 19 }, name: Name("i"), execution_scope: Comp(Id { idx: 20 }), source: Id { idx: 18 }, antecedent: Id { idx: 4 } }
data: Assign { expr: Id { idx: 21 }, name: Name("i"), execution_scope: Comp(Id { idx: 22 }), source: Id { idx: 20 }, antecedent: Id { idx: 4 } }
antecedents: ['bb4]
}
'bb7: {
data: Assign { expr: Id { idx: 9 }, name: Name("nums"), execution_scope: Module, source: Id { idx: 20 }, antecedent: Id { idx: 6 } }
data: Assign { expr: Id { idx: 10 }, name: Name("nums"), execution_scope: Module, source: Id { idx: 22 }, antecedent: Id { idx: 6 } }
antecedents: ['bb6]
}
@@ -600,7 +612,7 @@ for x, y in [[1, 2], [3, 4]]:
fn test_for_stmt_simple() {
check(
r#"
for x in range(1, 5):
for x in [1, 2, 3]:
pass
"#,
expect![[r#"
@@ -638,7 +650,7 @@ for x in range(1, 5):
fn test_break_stmt() {
check(
r#"
for x in range(1, 5):
for x in [1, 2, 3]:
break
"#,
expect![[r#"
@@ -676,7 +688,7 @@ for x in range(1, 5):
fn test_break_stmt_with_unreachable() {
check(
r#"
for x in range(1, 5):
for x in [1, 2, 3]:
y = 1
break
z = 1
@@ -728,8 +740,8 @@ a = 1
fn test_break_stmt_nested() {
check(
r#"
for x in range(5):
for y in range(5):
for x in [1, 2, 3]:
for y in [4, 5, 6]:
break
a = 1
break
@@ -748,7 +760,7 @@ for x in range(5):
}
'bb2: {
data: Assign { expr: Id { idx: 3 }, name: Name("x"), execution_scope: Module, source: Id { idx: 2 }, antecedent: Id { idx: 1 } }
data: Assign { expr: Id { idx: 4 }, name: Name("x"), execution_scope: Module, source: Id { idx: 3 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
@@ -763,7 +775,7 @@ for x in range(5):
}
'bb5: {
data: Assign { expr: Id { idx: 7 }, name: Name("y"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 3 } }
data: Assign { expr: Id { idx: 9 }, name: Name("y"), execution_scope: Module, source: Id { idx: 8 }, antecedent: Id { idx: 3 } }
antecedents: ['bb3]
}
@@ -785,7 +797,7 @@ for x in range(5):
fn test_continue_stmt() {
check(
r#"
for x in range(5):
for x in [1, 2, 3]:
y = 1
continue
z = 2
@@ -803,7 +815,7 @@ for x in range(5):
}
'bb2: {
data: Assign { expr: Id { idx: 3 }, name: Name("x"), execution_scope: Module, source: Id { idx: 2 }, antecedent: Id { idx: 1 } }
data: Assign { expr: Id { idx: 4 }, name: Name("x"), execution_scope: Module, source: Id { idx: 3 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
@@ -818,7 +830,7 @@ for x in range(5):
}
'bb5: {
data: Assign { expr: Id { idx: 4 }, name: Name("y"), execution_scope: Module, source: Id { idx: 5 }, antecedent: Id { idx: 3 } }
data: Assign { expr: Id { idx: 5 }, name: Name("y"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 3 } }
antecedents: ['bb3]
}
@@ -830,7 +842,7 @@ for x in range(5):
fn test_if_else_continue() {
check(
r#"
for i in range(1):
for i in [1, 2, 3]:
if 1 < 2:
continue
else:
@@ -850,7 +862,7 @@ for i in range(1):
}
'bb2: {
data: Assign { expr: Id { idx: 3 }, name: Name("i"), execution_scope: Module, source: Id { idx: 2 }, antecedent: Id { idx: 1 } }
data: Assign { expr: Id { idx: 4 }, name: Name("i"), execution_scope: Module, source: Id { idx: 3 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
@@ -872,4 +884,43 @@ for i in range(1):
"#]],
);
}

#[test]
fn test_call() {
check(
r#"
x = 123
fail("foo")
y = 456
"#,
expect![[r#"
def main():
'bb0: {
data: Unreachable
antecedents: []
}
'bb1: {
data: Start
antecedents: []
}
'bb2: {
data: Assign { expr: Id { idx: 0 }, name: Name("x"), execution_scope: Module, source: Id { idx: 1 }, antecedent: Id { idx: 1 } }
antecedents: ['bb1]
}
'bb3: {
data: Call { expr: Id { idx: 4 }, antecedent: Id { idx: 2 } }
antecedents: []
}
'bb4: {
data: Assign { expr: Id { idx: 5 }, name: Name("y"), execution_scope: Module, source: Id { idx: 6 }, antecedent: Id { idx: 3 } }
antecedents: ['bb3]
}
"#]],
);
}
}
Loading