Skip to content

Commit

Permalink
Fixed use block items to use their alias. (#6366)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Sep 9, 2024
1 parent 6679166 commit 645ef14
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ fn test_use_usage() {
assert_eq!(A, 1);
}

#[test]
fn test_use_usage_with_alias() {
use X::A as B;
assert_eq!(B, 1);
}

#[test]
fn test_use_constant_shadowing() {
use X::A;
Expand Down
10 changes: 1 addition & 9 deletions crates/cairo-lang-defs/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,15 +1041,7 @@ fn module_item_name_stable_ptr(
Ok(match &item_id {
ModuleItemId::Constant(id) => data.constants[id].name(db).stable_ptr().untyped(),
ModuleItemId::Submodule(id) => data.submodules[id].name(db).stable_ptr().untyped(),
ModuleItemId::Use(id) => {
let use_leaf = &data.uses[id];
match use_leaf.alias_clause(db) {
ast::OptionAliasClause::Empty(_) => use_leaf.ident(db).stable_ptr().untyped(),
ast::OptionAliasClause::AliasClause(alias) => {
alias.alias(db).stable_ptr().untyped()
}
}
}
ModuleItemId::Use(id) => data.uses[id].name_stable_ptr(db),
ModuleItemId::FreeFunction(id) => {
data.free_functions[id].declaration(db).name(db).stable_ptr().untyped()
}
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-defs/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,9 @@ impl StatementItemId {
StatementItemId::Constant(id) => {
id.lookup_intern(db).1.lookup(db.upcast()).name(db.upcast()).stable_ptr().untyped()
}
StatementItemId::Use(id) => id.stable_ptr(db).into(),
StatementItemId::Use(id) => {
id.lookup_intern(db).1.lookup(db.upcast()).name_stable_ptr(db.upcast())
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3540,13 +3540,11 @@ pub fn compute_statement_semantic(
segments,
NotFoundItemType::Identifier,
)?;

let path_segment = leaf.ident(db.upcast());
let name = path_segment.identifier(db.upcast());
let var_def_id = StatementItemId::Use(
StatementUseLongId(ctx.resolver.module_file_id, leaf.stable_ptr())
.intern(db),
);
let name = var_def_id.name(db.upcast());
match resolved_item {
ResolvedGenericItem::GenericConstant(const_id) => {
let const_value = db.constant_const_value(const_id)?;
Expand Down
14 changes: 12 additions & 2 deletions crates/cairo-lang-syntax/src/node/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use super::ast::{
ItemUse, Member, Modifier, ModuleItem, OptionArgListParenthesized, Statement, StatementBreak,
StatementContinue, StatementExpr, StatementLet, StatementReturn, TerminalIdentifierGreen,
TokenIdentifierGreen, TraitItem, TraitItemConstant, TraitItemFunction, TraitItemFunctionPtr,
TraitItemImpl, TraitItemType, Variant, WrappedArgList,
TraitItemImpl, TraitItemType, UsePathLeaf, Variant, WrappedArgList,
};
use super::db::SyntaxGroup;
use super::ids::SyntaxStablePtrId;
use super::kind::SyntaxKind;
use super::{SyntaxNode, Terminal, TypedSyntaxNode};
use super::{SyntaxNode, Terminal, TypedStablePtr, TypedSyntaxNode};
use crate::node::ast::{Attribute, AttributeList};
use crate::node::green::GreenNodeDetails;

Expand Down Expand Up @@ -620,3 +620,13 @@ impl UsePathEx for ast::UsePath {
}
}
}

impl UsePathLeaf {
/// Retrieves the stable pointer of the name of the leaf.
pub fn name_stable_ptr(&self, db: &dyn SyntaxGroup) -> SyntaxStablePtrId {
match self.alias_clause(db) {
ast::OptionAliasClause::Empty(_) => self.ident(db).stable_ptr().untyped(),
ast::OptionAliasClause::AliasClause(alias) => alias.alias(db).stable_ptr().untyped(),
}
}
}

0 comments on commit 645ef14

Please sign in to comment.