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

Enabled feature uses in use statements. #5673

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 4 additions & 7 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::test::test_utils::{assert_eq, assert_ne};
use core::iter::traits::iterator::Iterator;
#[feature("collections-into-iter")]
use core::iter::traits::iterator::{IntoIterator, Iterator};

#[test]
fn test_array() {
Expand Down Expand Up @@ -180,9 +181,7 @@ fn test_span_multi_pop() {

#[test]
fn test_span_iterator() {
let span = array![10, 11, 12, 13_felt252].span();
#[feature("collections-into-iter")]
let mut iter = core::iter::traits::iterator::IntoIterator::into_iter(span);
let mut iter = array![10, 11, 12, 13_felt252].span().into_iter();
let mut i = 10;
while let Option::Some(value) = iter.next() {
assert_eq!(value, @i);
Expand All @@ -192,9 +191,7 @@ fn test_span_iterator() {

#[test]
fn test_array_iterator() {
let array = array![10, 11, 12, 13];
#[feature("collections-into-iter")]
let mut iter = core::iter::traits::iterator::IntoIterator::into_iter(array);
let mut iter = array![10, 11, 12, 13].into_iter();
let mut i = 10;
while let Option::Some(value) = iter.next() {
assert_eq!(value, i);
Expand Down
18 changes: 3 additions & 15 deletions crates/cairo-lang-semantic/src/items/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use cairo_lang_defs::ids::{
};
use cairo_lang_diagnostics::{Diagnostics, DiagnosticsBuilder, Maybe};
use cairo_lang_syntax::attribute::structured::{Attribute, AttributeListStructurize};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{ast, TypedSyntaxNode};
use cairo_lang_syntax::node::ast;
use cairo_lang_syntax::node::helpers::UsePathEx;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
use smol_str::SmolStr;
Expand Down Expand Up @@ -57,19 +57,7 @@ pub fn priv_module_semantic_data(
}
ModuleItemId::Use(item_id) => {
let use_ast = &def_db.module_uses(module_id)?[item_id];
let use_path = ast::UsePath::Leaf(use_ast.clone());
let mut node = use_path.as_syntax_node();
let item = loop {
let Some(parent) = node.parent() else {
unreachable!("UsePath is not under an ItemUse.");
};
match parent.kind(syntax_db) {
SyntaxKind::ItemUse => {
break ast::ItemUse::from_syntax_node(syntax_db, parent);
}
_ => node = parent,
}
};
let item = ast::UsePath::Leaf(use_ast.clone()).get_item(syntax_db);
(item_id.name(def_db), item.attributes(syntax_db), item.visibility(syntax_db))
}
ModuleItemId::FreeFunction(item_id) => {
Expand Down
8 changes: 5 additions & 3 deletions crates/cairo-lang-semantic/src/items/us.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_lang_defs::ids::{LanguageElementId, LookupItemId, ModuleItemId, UseId}
use cairo_lang_diagnostics::{Diagnostics, Maybe, ToMaybe};
use cairo_lang_proc_macros::DebugWithDb;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::UsePathEx;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{ast, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
Expand All @@ -27,16 +28,17 @@ pub struct UseData {
pub fn priv_use_semantic_data(db: &dyn SemanticGroup, use_id: UseId) -> Maybe<UseData> {
let module_file_id = use_id.module_file_id(db.upcast());
let mut diagnostics = SemanticDiagnostics::default();
// TODO(spapini): Add generic args when they are supported on structs.
let inference_id =
InferenceId::LookupItemDeclaration(LookupItemId::ModuleItem(ModuleItemId::Use(use_id)));
let mut resolver = Resolver::new(db, module_file_id, inference_id);
// TODO(spapini): when code changes in a file, all the AST items change (as they contain a path
// to the green root that changes. Once ASTs are rooted on items, use a selector that picks only
// the item instead of all the module data.
let use_ast = db.module_use_by_id(use_id)?.to_maybe()?;
let use_ast = ast::UsePath::Leaf(db.module_use_by_id(use_id)?.to_maybe()?);
let item = use_ast.get_item(db.upcast());
resolver.set_allowed_features(&use_id, &item, &mut diagnostics);
let mut segments = vec![];
get_use_segments(db.upcast(), &ast::UsePath::Leaf(use_ast.clone()), &mut segments)?;
get_use_segments(db.upcast(), &use_ast, &mut segments)?;
let resolved_item =
resolver.resolve_generic_path(&mut diagnostics, segments, NotFoundItemType::Identifier);
let resolver_data = Arc::new(resolver.data);
Expand Down
22 changes: 22 additions & 0 deletions crates/cairo-lang-syntax/src/node/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,25 @@ impl BodyItems for ast::ImplBody {
self.items(db).elements(db)
}
}

/// Helper trait for ast::UsePath.
pub trait UsePathEx {
/// Retrieves the item of a use path.
fn get_item(&self, db: &dyn SyntaxGroup) -> ast::ItemUse;
}
impl UsePathEx for ast::UsePath {
fn get_item(&self, db: &dyn SyntaxGroup) -> ast::ItemUse {
let mut node = self.as_syntax_node();
loop {
let Some(parent) = node.parent() else {
unreachable!("UsePath is not under an ItemUse.");
};
match parent.kind(db) {
SyntaxKind::ItemUse => {
break ast::ItemUse::from_syntax_node(db, parent);
}
_ => node = parent,
}
}
}
}
Loading