-
-
Notifications
You must be signed in to change notification settings - Fork 931
perf(linter): detect diverging match blocks for node type skipping #14631
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
Merged
graphite-app
merged 1 commit into
main
from
10-15-perf_linter_detect_diverging_match_blocks_for_node_type_skipping
Oct 15, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use syn::{Arm, Expr, Pat, Stmt}; | ||
|
|
||
| use crate::{ | ||
| CollectionResult, NodeTypeSet, | ||
| utils::{astkind_variant_from_path, is_node_kind_call}, | ||
| }; | ||
|
|
||
| /// Detects various kinds of diverging statements that narrow by more than one AST node type. | ||
| pub struct EarlyDivergeDetector { | ||
| node_types: NodeTypeSet, | ||
| } | ||
|
|
||
| impl EarlyDivergeDetector { | ||
| pub fn from_run_func(run_func: &syn::ImplItemFn) -> Option<NodeTypeSet> { | ||
| // Only look at cases where the body has more than one top-level statement. | ||
| let block = &run_func.block; | ||
| if block.stmts.len() <= 1 { | ||
| return None; | ||
| } | ||
|
|
||
| // Look at the first statement in the function body. | ||
| let stmt = block.stmts.first()?; | ||
|
|
||
| // Check if it's `let something = match node.kind() { ... }` that diverges | ||
| if let Stmt::Local(local) = stmt | ||
| && let Some(init) = &local.init | ||
| && let Expr::Match(match_expr) = &*init.expr | ||
| && is_node_kind_call(&match_expr.expr) | ||
| { | ||
| let mut detector = Self { node_types: NodeTypeSet::new() }; | ||
| let result = detector.extract_variants_from_diverging_match_expr(match_expr); | ||
| if result == CollectionResult::Incomplete || detector.node_types.is_empty() { | ||
| return None; | ||
| } | ||
| return Some(detector.node_types); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn extract_variants_from_diverging_match_expr( | ||
| &mut self, | ||
| match_expr: &syn::ExprMatch, | ||
| ) -> CollectionResult { | ||
| let mut overall_result = CollectionResult::Complete; | ||
| for arm in &match_expr.arms { | ||
| let result = self.extract_variants_from_diverging_match_arm(arm); | ||
| if result == CollectionResult::Incomplete { | ||
| overall_result = CollectionResult::Incomplete; | ||
| } | ||
| } | ||
| overall_result | ||
| } | ||
|
|
||
| fn extract_variants_from_diverging_match_arm(&mut self, arm: &Arm) -> CollectionResult { | ||
| let pat = &arm.pat; | ||
| match pat { | ||
| Pat::TupleStruct(ts) => { | ||
| if let Some(variant) = astkind_variant_from_path(&ts.path) { | ||
| // NOTE: If there is a guard, we assume that it may or may not be taken and collect all AST kinds | ||
| // regardless of the guard condition. | ||
| self.node_types.insert(variant); | ||
| CollectionResult::Complete | ||
| } else { | ||
| CollectionResult::Incomplete | ||
| } | ||
| } | ||
camchenry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Pat::Wild(_) => { | ||
| // Body must be diverging (i.e., returning from function) | ||
| if let Expr::Return(_) = *arm.body { | ||
| return CollectionResult::Complete; | ||
| } | ||
| CollectionResult::Incomplete | ||
| } | ||
| _ => CollectionResult::Incomplete, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small note here:
IMO it'd likely be preferable to use
block.stmts.first().unwrap()..unwrap()will often cause the compiler to backtrack and try to prove that theunwrapcan't fail. In this case I'd assume it can prove this fairly easily as theblock.stmts.len() <= 1check is right above. So it'll remove the branch. Whereas with?it probably won't backtrack and won't figure this out.Someone taught me this surprising fact last year - sometimes a well-placed
assert!can result in less code, rather than more.