From 445314d0fd555f8356bac45820d15d4b2ab654cb Mon Sep 17 00:00:00 2001 From: orizi <104711814+orizi@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:36:10 +0500 Subject: [PATCH] Fixed glob uses starting with non-explicit dep crates. (#7011) --- .../src/expr/semantic_test_data/use | 58 +++++++++++++++++++ crates/cairo-lang-semantic/src/resolve/mod.rs | 15 +++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/crates/cairo-lang-semantic/src/expr/semantic_test_data/use b/crates/cairo-lang-semantic/src/expr/semantic_test_data/use index 736a1aa2f68..bcb92c6361a 100644 --- a/crates/cairo-lang-semantic/src/expr/semantic_test_data/use +++ b/crates/cairo-lang-semantic/src/expr/semantic_test_data/use @@ -1362,6 +1362,64 @@ FunctionCall( //! > ========================================================================== +//! > Use glob path starting with `core::`. + +//! > test_runner_name +test_expr_semantics(expect_diagnostics: false) + +//! > crate_settings +edition = "2024_07" + +//! > module_code +use core::starknet::*; + +//! > function_body + +//! > expr_code +{} + +//! > expected_semantics +Block( + ExprBlock { + statements: [], + tail: None, + ty: (), + }, +) + +//! > expected_diagnostics + +//! > ========================================================================== + +//! > Use glob path starting with `starknet::`. + +//! > test_runner_name +test_expr_semantics(expect_diagnostics: false) + +//! > crate_settings +edition = "2024_07" + +//! > module_code +use starknet::*; + +//! > function_body + +//! > expr_code +{} + +//! > expected_semantics +Block( + ExprBlock { + statements: [], + tail: None, + ty: (), + }, +) + +//! > expected_diagnostics + +//! > ========================================================================== + //! > Test using a variant with a generic type. //! > test_runner_name diff --git a/crates/cairo-lang-semantic/src/resolve/mod.rs b/crates/cairo-lang-semantic/src/resolve/mod.rs index 98f9d19bd40..cf3eee0d224 100644 --- a/crates/cairo-lang-semantic/src/resolve/mod.rs +++ b/crates/cairo-lang-semantic/src/resolve/mod.rs @@ -68,6 +68,8 @@ mod item; pub const SELF_TYPE_KW: &str = "Self"; pub const SUPER_KW: &str = "super"; pub const CRATE_KW: &str = "crate"; +// Remove when this becomes an actual crate. +const STARKNET_CRATE_NAME: &str = "starknet"; /// Lookback maps for item resolving. Can be used to quickly check what is the semantic resolution /// of any path segment. @@ -1303,6 +1305,15 @@ impl<'db> Resolver<'db> { return ResolvedBase::Crate(dep_crate_id); } + // If the first segment is `core` - and it was not overridden by a dependency - using it. + if ident == CORELIB_CRATE_NAME { + return ResolvedBase::Crate(CrateId::core(self.db)); + } + // TODO(orizi): Remove when `starknet` becomes a proper crate. + if ident == STARKNET_CRATE_NAME { + // Making sure we don't look for it in `*` modules, to prevent cycles. + return ResolvedBase::Module(self.prelude_submodule()); + } // If an item with this name is found in one of the 'use *' imports, use the module that match self.resolve_path_using_use_star(module_id, identifier) { UseStarResult::UniquePathFound(inner_module_item) => { @@ -1323,10 +1334,6 @@ impl<'db> Resolver<'db> { return ResolvedBase::ItemNotVisible(module_item_id, containing_modules); } } - // If the first segment is `core` - and it was not overridden by a dependency - using it. - if ident == CORELIB_CRATE_NAME { - return ResolvedBase::Crate(CrateId::core(self.db)); - } ResolvedBase::Module(self.prelude_submodule()) }