Skip to content

Commit

Permalink
Move Root.is_ancestor.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 21, 2024
1 parent a90c8fc commit 6d3e8cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,7 @@ pub fn module_is_submodule_of(
false
}
}

pub fn mod_is_ancestor(src: &ModulePath, dst: &ModulePath) -> bool {
dst.len() >= src.len() && src.iter().zip(dst).all(|(src, dst)| src == dst)
}
16 changes: 8 additions & 8 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::fmt;

use super::{module::Module, trait_map::TraitMap, Ident, ResolvedTraitImplItem};
use super::{
module::{mod_is_ancestor, Module},
trait_map::TraitMap,
Ident, ResolvedTraitImplItem,
};
use crate::{
decl_engine::{DeclEngine, DeclRef},
engine_threading::*,
Expand Down Expand Up @@ -203,7 +207,7 @@ impl Root {

// Collect all items declared in the source module
for (symbol, decl) in src_mod.current_items().symbols.iter() {
if is_ancestor(src, dst) || decl.visibility(engines).is_public() {
if mod_is_ancestor(src, dst) || decl.visibility(engines).is_public() {
decls_and_item_imports.push((symbol.clone(), decl.clone(), src.to_vec()));
}
}
Expand Down Expand Up @@ -287,7 +291,7 @@ impl Root {
let src_items = src_mod.current_items();

let (decl, path, src_visibility) = if let Some(decl) = src_items.symbols.get(item) {
let visibility = if is_ancestor(src, dst) {
let visibility = if mod_is_ancestor(src, dst) {
Visibility::Public
} else {
decl.visibility(engines)
Expand Down Expand Up @@ -690,7 +694,7 @@ impl Root {
) -> Result<(), ErrorEmitted> {
let dst = self.module.mod_path();
// you are always allowed to access your ancestor's symbols
if !is_ancestor(src, dst) {
if !mod_is_ancestor(src, dst) {
// we don't check the first prefix because direct children are always accessible
for prefix in iter_prefixes(src).skip(1) {
let module = self.module.lookup_submodule(handler, engines, prefix)?;
Expand Down Expand Up @@ -999,7 +1003,3 @@ impl From<Module> for Root {
Root { module }
}
}

fn is_ancestor(src: &ModulePath, dst: &ModulePath) -> bool {
dst.len() >= src.len() && src.iter().zip(dst).all(|(src, dst)| src == dst)
}

0 comments on commit 6d3e8cb

Please sign in to comment.