Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework and fix associated const visibility for impl items #6785

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sway-ast/src/item/item_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::priv_prelude::*;

#[derive(Clone, Debug, Serialize)]
pub struct ItemConst {
pub visibility: Option<PubToken>,
pub pub_token: Option<PubToken>,
pub const_token: ConstToken,
pub name: Ident,
pub ty_opt: Option<(ColonToken, Ty)>,
Expand All @@ -13,7 +13,7 @@ pub struct ItemConst {

impl Spanned for ItemConst {
fn span(&self) -> Span {
let start = match &self.visibility {
let start = match &self.pub_token {
Some(pub_token) => pub_token.span(),
None => self.const_token.span(),
};
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn default_with_contract_id_inner(
handler,
engines,
const_item,
Visibility::Private,
attributes,
true,
)?;
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
Loading