Skip to content

Commit

Permalink
Inline calculation of common ancestor count
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed Nov 6, 2024
1 parent efc3769 commit c104a66
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,26 @@ impl Root {
Ok(())
}

/// Check that all accessed modules in the src path are visible from the dst path.
/// If src and dst have a common ancestor module that is private, this privacy modifier is
/// ignored for visibility purposes, since src and dst are both behind that private visibility
/// modifier. Additionally, items in a private module are visible to its immediate parent.
fn check_module_privacy(
&self,
handler: &Handler,
engines: &Engines,
src: &ModulePath,
dst: &ModulePath,
) -> Result<(), ErrorEmitted> {
// Calculate the number of src prefixes whose privacy is ignored.
let mut ignored_prefixes = 0;

// Ignore visibility of common ancestors
let mut ignored_prefixes = common_ancestor_count(src, dst);
ignored_prefixes += src
.iter()
.zip(dst)
.position(|(src_id, dst_id)| src_id != dst_id)
.unwrap_or(dst.len());

// Ignore visibility of direct submodules of the destination module
if dst.len() == ignored_prefixes {
Expand Down Expand Up @@ -830,15 +841,6 @@ impl From<Module> for Root {
}
}

/// Iterates through two module paths and returns the number of common prefixes of src and dst,
/// i.e., the number of common ancestors of the src and dst modules.
fn common_ancestor_count(src: &ModulePath, dst: &ModulePath) -> usize {
src.iter()
.zip(dst)
.position(|(src_id, dst_id)| src_id != dst_id)
.unwrap_or(dst.len())
}

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 c104a66

Please sign in to comment.