Skip to content

Commit

Permalink
Rework visibility level while doing visibility checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Dec 16, 2024
1 parent 3fa112a commit 12cbb05
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ impl ty::TyExpression {
&storage_key_ident.into(),
None,
VisibilityCheck::No,
Visibility::Public,
)?;

let storage_key_struct_decl_id = storage_key_decl
Expand Down
18 changes: 18 additions & 0 deletions sway-core/src/semantic_analysis/symbol_resolve_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct SymbolResolveContext<'a> {
///
/// This is `Disallow` everywhere except while checking type parameters bounds in struct instantiation.
generic_shadowing_mode: GenericShadowingMode,
/// Visibility checking level for the current scope.
visibility_level: Visibility,
}

impl<'a> SymbolResolveContext<'a> {
Expand All @@ -57,6 +59,7 @@ impl<'a> SymbolResolveContext<'a> {
self_type: None,
const_shadowing_mode: ConstShadowingMode::ItemStyle,
generic_shadowing_mode: GenericShadowingMode::Disallow,
visibility_level: Visibility::Public,
}
}

Expand All @@ -75,6 +78,7 @@ impl<'a> SymbolResolveContext<'a> {
self_type: self.self_type,
const_shadowing_mode: self.const_shadowing_mode,
generic_shadowing_mode: self.generic_shadowing_mode,
visibility_level: Visibility::Public,
}
}

Expand Down Expand Up @@ -156,6 +160,15 @@ impl<'a> SymbolResolveContext<'a> {
}
}

/// Map this `SymbolResolveContext` instance to a new one with the given visibility level.
#[allow(unused)]
pub(crate) fn with_visibility_level(self, visibility_level: Visibility) -> Self {
Self {
visibility_level,
..self
}
}

// A set of accessor methods. We do this rather than making the fields `pub` in order to ensure
// that these are only updated via the `with_*` methods that produce a new `SymbolResolveContext`.
#[allow(unused)]
Expand All @@ -173,6 +186,10 @@ impl<'a> SymbolResolveContext<'a> {
self.generic_shadowing_mode
}

pub(crate) fn visibility_level(&self) -> Visibility {
self.visibility_level
}

/// Get the engines needed for engine threading.
pub(crate) fn engines(&self) -> &'a Engines {
self.engines
Expand All @@ -192,6 +209,7 @@ impl<'a> SymbolResolveContext<'a> {
call_path,
self.self_type(),
VisibilityCheck::Yes,
self.visibility_level(),
)
}
}
20 changes: 20 additions & 0 deletions sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pub struct TypeCheckContext<'a> {
// In some nested places of the first pass we want to disable the first pass optimizations
// To disable those optimizations we can set this to false.
code_block_first_pass: bool,

/// Visibility checking level for the current scope.
visibility_level: Visibility,
}

impl<'a> TypeCheckContext<'a> {
Expand Down Expand Up @@ -139,6 +142,7 @@ impl<'a> TypeCheckContext<'a> {
experimental,
collecting_unifications: false,
code_block_first_pass: false,
visibility_level: Visibility::Public,
}
}

Expand Down Expand Up @@ -183,6 +187,7 @@ impl<'a> TypeCheckContext<'a> {
experimental,
collecting_unifications: false,
code_block_first_pass: false,
visibility_level: Visibility::Public,
}
}

Expand Down Expand Up @@ -214,6 +219,7 @@ impl<'a> TypeCheckContext<'a> {
experimental: self.experimental,
collecting_unifications: self.collecting_unifications,
code_block_first_pass: self.code_block_first_pass,
visibility_level: self.visibility_level,
}
}

Expand Down Expand Up @@ -251,6 +257,7 @@ impl<'a> TypeCheckContext<'a> {
experimental: self.experimental,
collecting_unifications: self.collecting_unifications,
code_block_first_pass: self.code_block_first_pass,
visibility_level: self.visibility_level,
};
with_scoped_ctx(ctx)
},
Expand All @@ -275,6 +282,7 @@ impl<'a> TypeCheckContext<'a> {
experimental: self.experimental,
collecting_unifications: self.collecting_unifications,
code_block_first_pass: self.code_block_first_pass,
visibility_level: self.visibility_level,
};
with_scoped_ctx(ctx)
}
Expand Down Expand Up @@ -315,6 +323,7 @@ impl<'a> TypeCheckContext<'a> {
experimental: self.experimental,
collecting_unifications: self.collecting_unifications,
code_block_first_pass: self.code_block_first_pass,
visibility_level: self.visibility_level,
};
Ok((with_scoped_ctx(ctx)?, namespace))
},
Expand All @@ -339,6 +348,7 @@ impl<'a> TypeCheckContext<'a> {
experimental: self.experimental,
collecting_unifications: self.collecting_unifications,
code_block_first_pass: self.code_block_first_pass,
visibility_level: self.visibility_level,
};
Ok((with_scoped_ctx(ctx)?, namespace))
}
Expand Down Expand Up @@ -565,6 +575,10 @@ impl<'a> TypeCheckContext<'a> {
self.code_block_first_pass
}

pub(crate) fn visibility_level(&self) -> Visibility {
self.visibility_level
}

/// Get the engines needed for engine threading.
pub(crate) fn engines(&self) -> &'a Engines {
self.engines
Expand Down Expand Up @@ -672,6 +686,7 @@ impl<'a> TypeCheckContext<'a> {
self.self_type(),
&self.subst_ctx(),
VisibilityCheck::Yes,
Visibility::Public,
)
}

Expand All @@ -689,6 +704,7 @@ impl<'a> TypeCheckContext<'a> {
self.self_type(),
&self.subst_ctx(),
VisibilityCheck::Yes,
self.visibility_level(),
)
.map(|d| d.expect_typed())
}
Expand All @@ -707,6 +723,7 @@ impl<'a> TypeCheckContext<'a> {
&symbol.clone().into(),
self.self_type(),
VisibilityCheck::No,
Visibility::Public,
)
.map(|d| d.expect_typed())
}
Expand All @@ -725,6 +742,7 @@ impl<'a> TypeCheckContext<'a> {
call_path,
self.self_type(),
VisibilityCheck::Yes,
self.visibility_level(),
)
.map(|d| d.expect_typed())
}
Expand All @@ -743,6 +761,7 @@ impl<'a> TypeCheckContext<'a> {
call_path,
self.self_type(),
VisibilityCheck::No,
self.visibility_level(),
)
.map(|d| d.expect_typed())
}
Expand Down Expand Up @@ -789,6 +808,7 @@ impl<'a> TypeCheckContext<'a> {
self.self_type(),
&self.subst_ctx(),
VisibilityCheck::Yes,
Visibility::Public,
)
.unwrap_or_else(|err| type_engine.id_of_error_recovery(err));

Expand Down
Loading

0 comments on commit 12cbb05

Please sign in to comment.