-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[WIP] Cfg loops #17175
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
base: main
Are you sure you want to change the base?
[WIP] Cfg loops #17175
Conversation
I think there are a few lifetimes that can be simplified in the mermaid testing code Subject: [PATCH] Align server indexing with CLI behavior
---
Index: crates/ruff_python_semantic/src/cfg/graph.rs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/crates/ruff_python_semantic/src/cfg/graph.rs b/crates/ruff_python_semantic/src/cfg/graph.rs
--- a/crates/ruff_python_semantic/src/cfg/graph.rs (revision dca1340348d73b1bf1cd10de593b1ef01d9363ec)
+++ b/crates/ruff_python_semantic/src/cfg/graph.rs (date 1743696804592)
@@ -49,7 +49,7 @@
/// Returns the [`Edges`] going out of the basic block at the given index
pub fn outgoing(&self, block: BlockId) -> &Edges {
- &self.blocks[block].out
+ self.blocks[block].out
}
/// Returns an iterator over the indices of the direct predecessors of the block at the given index
@@ -132,7 +132,7 @@
}
/// Returns iterator over [`Condition`]s which must be satisfied to traverse corresponding edge
- pub fn conditions(&self) -> impl ExactSizeIterator<Item = &Condition> {
+ pub fn conditions(&self) -> impl ExactSizeIterator<Item = &Condition<'stmt>> {
self.conditions.iter()
}
@@ -140,7 +140,7 @@
self.targets.is_empty()
}
- pub fn filter_targets_by_conditions<'a, T: FnMut(&Condition) -> bool + 'a>(
+ pub fn filter_targets_by_conditions<'a: 'stmt, T: FnMut(&Condition) -> bool + 'a>(
&'a self,
mut predicate: T,
) -> impl Iterator<Item = BlockId> + 'a {
Index: crates/ruff_python_semantic/src/cfg/visualize.rs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/crates/ruff_python_semantic/src/cfg/visualize.rs b/crates/ruff_python_semantic/src/cfg/visualize.rs
--- a/crates/ruff_python_semantic/src/cfg/visualize.rs (revision dca1340348d73b1bf1cd10de593b1ef01d9363ec)
+++ b/crates/ruff_python_semantic/src/cfg/visualize.rs (date 1743696538214)
@@ -10,7 +10,7 @@
CFGWithSource::new(graph, source).draw_graph()
}
-trait MermaidGraph<'a>: DirectedGraph<'a> {
+trait MermaidGraph: DirectedGraph {
fn draw_node(&self, node: Self::Node) -> MermaidNode;
fn draw_edges(&self, node: Self::Node) -> impl Iterator<Item = (Self::Node, MermaidEdge)>;
@@ -146,7 +146,7 @@
}
}
-pub trait DirectedGraph<'a> {
+pub trait DirectedGraph {
type Node: Idx;
fn num_nodes(&self) -> usize;
@@ -154,18 +154,18 @@
fn successors(&self, node: Self::Node) -> impl ExactSizeIterator<Item = Self::Node> + '_;
}
-struct CFGWithSource<'stmt> {
+struct CFGWithSource<'source, 'stmt> {
cfg: ControlFlowGraph<'stmt>,
- source: &'stmt str,
+ source: &'source str,
}
-impl<'stmt> CFGWithSource<'stmt> {
- fn new(cfg: ControlFlowGraph<'stmt>, source: &'stmt str) -> Self {
+impl<'source, 'stmt> CFGWithSource<'source, 'stmt> {
+ fn new(cfg: ControlFlowGraph<'stmt>, source: &'source str) -> Self {
Self { cfg, source }
}
}
-impl<'stmt> DirectedGraph<'stmt> for CFGWithSource<'stmt> {
+impl DirectedGraph for CFGWithSource<'_, '_> {
type Node = BlockId;
fn num_nodes(&self) -> usize {
@@ -181,7 +181,7 @@
}
}
-impl<'stmt> MermaidGraph<'stmt> for CFGWithSource<'stmt> {
+impl MermaidGraph for CFGWithSource<'_, '_> {
fn draw_node(&self, node: Self::Node) -> MermaidNode {
let statements: Vec<String> = self
.cfg But I don't see an obvious work around for the lifetime issues other than using an |
Hmm... not sure what I did wrong but I applied your patch exactly and the code did not compile. This seems to be the problem: /// Returns the [`Edges`] going out of the basic block at the given index
pub fn outgoing(&self, block: BlockId) -> &Edges {
- &self.blocks[block].out
+ self.blocks[block].out
} This doesn't agree with the return signature. If we remove the borrow we get a lifetime issue. It all works fine if we switch from |
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PLW0101 | 167 | 166 | 1 | 0 | 0 |
WIP