forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Add control flow support for match statement (astral-sh#13241
) ## Summary This PR adds support for control flow for match statement. It also adds the necessary infrastructure required for narrowing constraints in case blocks and implements the logic for `PatternMatchSingleton` which is either `None` / `True` / `False`. Even after this the inferred type doesn't get simplified completely, there's a TODO for that in the test code. ## Test Plan Add test cases for control flow for (a) when there's a wildcard pattern and (b) when there isn't. There's also a test case to verify the narrowing logic. --------- Co-authored-by: Carl Meyer <[email protected]>
- Loading branch information
1 parent
6f53aaf
commit 62c7d8f
Showing
9 changed files
with
321 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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
39 changes: 39 additions & 0 deletions
39
crates/red_knot_python_semantic/src/semantic_index/constraint.rs
This file contains 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,39 @@ | ||
use ruff_db::files::File; | ||
use ruff_python_ast as ast; | ||
|
||
use crate::ast_node_ref::AstNodeRef; | ||
use crate::db::Db; | ||
use crate::semantic_index::expression::Expression; | ||
use crate::semantic_index::symbol::{FileScopeId, ScopeId}; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub(crate) enum Constraint<'db> { | ||
Expression(Expression<'db>), | ||
Pattern(PatternConstraint<'db>), | ||
} | ||
|
||
#[salsa::tracked] | ||
pub(crate) struct PatternConstraint<'db> { | ||
#[id] | ||
pub(crate) file: File, | ||
|
||
#[id] | ||
pub(crate) file_scope: FileScopeId, | ||
|
||
#[no_eq] | ||
#[return_ref] | ||
pub(crate) subject: AstNodeRef<ast::Expr>, | ||
|
||
#[no_eq] | ||
#[return_ref] | ||
pub(crate) pattern: AstNodeRef<ast::Pattern>, | ||
|
||
#[no_eq] | ||
count: countme::Count<PatternConstraint<'static>>, | ||
} | ||
|
||
impl<'db> PatternConstraint<'db> { | ||
pub(crate) fn scope(self, db: &'db dyn Db) -> ScopeId<'db> { | ||
self.file_scope(db).to_scope_id(db, self.file(db)) | ||
} | ||
} |
This file contains 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 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 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.