-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use cairo_lang_defs::ids::{FileIndex, ModuleFileId, ModuleId, NamedLanguageElementId}; | ||
use cairo_lang_defs::plugin::PluginDiagnostic; | ||
use cairo_lang_diagnostics::DiagnosticsBuilder; | ||
use cairo_lang_semantic::db::SemanticGroup; | ||
use cairo_lang_semantic::diagnostic::NotFoundItemType; | ||
use cairo_lang_semantic::expr::inference::InferenceId; | ||
use cairo_lang_semantic::resolve::{ResolvedConcreteItem, ResolvedGenericItem, Resolver}; | ||
use cairo_lang_semantic::{ExprFunctionCall, ExprMatch}; | ||
use cairo_lang_syntax::node::ast::{Expr, ExprLoop, Statement, TerminalIdentifier}; | ||
use cairo_lang_syntax::node::TypedSyntaxNode; | ||
use cairo_lang_utils::try_extract_matches; | ||
|
||
pub fn check_loop_match_pop_front( | ||
db: &dyn SemanticGroup, | ||
loop_expr: &ExprLoop, | ||
diagnostics: &mut Vec<PluginDiagnostic>, | ||
module_id: &ModuleId, | ||
) { | ||
let statements = loop_expr.body(db.upcast()).statements(db.upcast()).elements(db.upcast()); | ||
for statement in statements { | ||
if let Statement::Expr(expr) = statement | ||
&& let Expr::Match(expr_match) = expr.expr(db.upcast()) | ||
{ | ||
let res = match expr_match.expr(db.upcast()) { | ||
Expr::FunctionCall(func_call) => "".to_owned(), | ||
Expr::Binary(binary_expr) => { | ||
if let Expr::FunctionCall(func_call) = | ||
Expr::from_syntax_node(db.upcast(), binary_expr.rhs(db.upcast()).as_syntax_node()) | ||
{ | ||
let mut diagnostics = DiagnosticsBuilder::default(); | ||
if let Expr::Path(pat) = | ||
Expr::from_syntax_node(db.upcast(), binary_expr.lhs(db.upcast()).as_syntax_node()) | ||
{ | ||
let identifier = TerminalIdentifier::from_syntax_node( | ||
db.upcast(), | ||
pat.as_syntax_node().parent().unwrap(), | ||
); | ||
db.upcast().find_lookup_item(pat.as_syntax_node()); | ||
let item = | ||
db.lookup_resolved_generic_item_by_ptr(lookup_item_id, identifier.stable_ptr())?; | ||
Some(format!("`{}`", item.full_path(db))); | ||
println!("Path {}", pat.node.get_text(db.upcast())); | ||
let item = | ||
Resolver::new(db, ModuleFileId(*module_id, FileIndex(0)), InferenceId::NoContext) | ||
.resolve_generic_path( | ||
&mut diagnostics, | ||
&func_call.path(db.upcast()), | ||
NotFoundItemType::Identifier, | ||
) | ||
.unwrap(); | ||
let generic_variant = try_extract_matches!(item, ResolvedGenericItem::Variable).unwrap(); | ||
println!("Func {:?}", generic_variant) | ||
}; | ||
}; | ||
format!( | ||
"Right {}", | ||
binary_expr.rhs(db.upcast()).as_syntax_node().get_text_without_trivia(db.upcast()) | ||
) | ||
} | ||
_ => "".to_owned(), | ||
}; | ||
|
||
println!("{}", res); | ||
} | ||
// println!("Statement: {:?}", statement); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod loops; | ||
pub mod single_match; |
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
11 changes: 11 additions & 0 deletions
11
crates/cairo-lint-core/tests/test_files/loops/loop_match_pop_front
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,11 @@ | ||
//! > simple loop match pop front | ||
//! > cairo_code | ||
fn main() { | ||
let a: Span<felt252> = array![1, 2, 3, 4, 5].span(); | ||
loop { | ||
match a.pop_front() { | ||
Option::Some(val) => println!("{val}"), | ||
Option::None => break, | ||
} | ||
} | ||
} |
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