Skip to content

Commit

Permalink
Merge d1670bb into 4608878
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn authored Jun 25, 2024
2 parents 4608878 + d1670bb commit 9397563
Show file tree
Hide file tree
Showing 119 changed files with 3,648 additions and 212 deletions.
8 changes: 5 additions & 3 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,20 +1652,22 @@ pub fn dependency_namespace(
let mut root = namespace::Root::from(root_module);

if core_added {
let _ = root.star_import_with_reexports(
let _ = root.star_import(
&Handler::default(),
engines,
&[CORE, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
);
Visibility::Private,
);
}

if has_std_dep(graph, node) {
let _ = root.star_import_with_reexports(
let _ = root.star_import(
&Handler::default(),
engines,
&[STD, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
Visibility::Private,
);
}

Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl CallPath {
if let Some(mod_path) = namespace.program_id(engines).read(engines, |m| {
if m.current_items().symbols().contains_key(&self.suffix) {
None
} else if let Some((_, path, _)) = m
} else if let Some((_, path, _, _)) = m
.current_items()
.use_item_synonyms
.get(&self.suffix)
Expand Down
8 changes: 7 additions & 1 deletion sway-core/src/language/parsed/use_statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::parsed::Span;
use crate::{
parsed::Span,
language::Visibility,
};
use sway_types::ident::Ident;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -17,5 +20,8 @@ pub struct UseStatement {
// If `is_absolute` is true, then this use statement is an absolute path from
// the project root namespace. If not, then it is relative to the current namespace.
pub is_absolute: bool,
// If `reexport` is Visibility::Public, then this use statement reexports its imported binding.
// If not, then the import binding is private to the importing module.
pub reexport: Visibility,
pub alias: Option<Ident>,
}
20 changes: 11 additions & 9 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn handle_item_trait_imports(
let root_mod = &ctx.namespace().root().module;
let dst_mod = ctx.namespace.module(engines);

for (_, (_, src, decl)) in dst_mod.current_items().use_item_synonyms.iter() {
for (_, (_, src, decl, _)) in dst_mod.current_items().use_item_synonyms.iter() {
let decl = decl.expect_typed_ref();

let src_mod = root_mod.lookup_submodule(handler, engines, src)?;
Expand Down Expand Up @@ -223,7 +223,7 @@ fn collect_use_statement(
ImportType::Star => {
// try a standard starimport first
let star_import_handler = Handler::default();
let import = ctx.star_import(&star_import_handler, engines, &path);
let import = ctx.star_import(&star_import_handler, engines, &path, stmt.reexport);
if import.is_ok() {
handler.append(star_import_handler);
import
Expand All @@ -232,7 +232,7 @@ fn collect_use_statement(
if let Some((enum_name, path)) = path.split_last() {
let variant_import_handler = Handler::default();
let variant_import =
ctx.variant_star_import(&variant_import_handler, engines, path, enum_name);
ctx.variant_star_import(&variant_import_handler, engines, path, enum_name, stmt.reexport);
if variant_import.is_ok() {
handler.append(variant_import_handler);
variant_import
Expand All @@ -246,12 +246,12 @@ fn collect_use_statement(
}
}
}
ImportType::SelfImport(_) => ctx.self_import(handler, engines, &path, stmt.alias.clone()),
ImportType::SelfImport(_) => ctx.self_import(handler, engines, &path, stmt.alias.clone(), stmt.reexport),
ImportType::Item(ref s) => {
// try a standard item import first
let item_import_handler = Handler::default();
let import =
ctx.item_import(&item_import_handler, engines, &path, s, stmt.alias.clone());
ctx.item_import(&item_import_handler, engines, &path, s, stmt.alias.clone(), stmt.reexport);

if import.is_ok() {
handler.append(item_import_handler);
Expand All @@ -267,6 +267,7 @@ fn collect_use_statement(
enum_name,
s,
stmt.alias.clone(),
stmt.reexport
);
if variant_import.is_ok() {
handler.append(variant_import_handler);
Expand Down Expand Up @@ -308,7 +309,7 @@ fn handle_use_statement(
ImportType::Star => {
// try a standard starimport first
let star_import_handler = Handler::default();
let import = ctx.star_import(&star_import_handler, &path);
let import = ctx.star_import(&star_import_handler, &path, stmt.reexport);
if import.is_ok() {
handler.append(star_import_handler);
import
Expand All @@ -317,7 +318,7 @@ fn handle_use_statement(
if let Some((enum_name, path)) = path.split_last() {
let variant_import_handler = Handler::default();
let variant_import =
ctx.variant_star_import(&variant_import_handler, path, enum_name);
ctx.variant_star_import(&variant_import_handler, path, enum_name, stmt.reexport);
if variant_import.is_ok() {
handler.append(variant_import_handler);
variant_import
Expand All @@ -331,11 +332,11 @@ fn handle_use_statement(
}
}
}
ImportType::SelfImport(_) => ctx.self_import(handler, &path, stmt.alias.clone()),
ImportType::SelfImport(_) => ctx.self_import(handler, &path, stmt.alias.clone(), stmt.reexport),
ImportType::Item(ref s) => {
// try a standard item import first
let item_import_handler = Handler::default();
let import = ctx.item_import(&item_import_handler, &path, s, stmt.alias.clone());
let import = ctx.item_import(&item_import_handler, &path, s, stmt.alias.clone(), stmt.reexport);

if import.is_ok() {
handler.append(item_import_handler);
Expand All @@ -350,6 +351,7 @@ fn handle_use_statement(
enum_name,
s,
stmt.alias.clone(),
stmt.reexport
);
if variant_import.is_ok() {
handler.append(variant_import_handler);
Expand Down
14 changes: 10 additions & 4 deletions sway-core/src/semantic_analysis/collection_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ impl SymbolCollectionContext {
handler: &Handler,
engines: &Engines,
src: &ModulePath,
visibility: Visibility,
) -> Result<(), ErrorEmitted> {
let mod_path = self.namespace().mod_path.clone();
self.namespace_mut()
.root
.star_import(handler, engines, src, &mod_path)
.star_import(handler, engines, src, &mod_path, visibility)
}

/// Short-hand for performing a [Module::variant_star_import] with `mod_path` as the destination.
Expand All @@ -122,11 +123,12 @@ impl SymbolCollectionContext {
engines: &Engines,
src: &ModulePath,
enum_name: &Ident,
visibility: Visibility,
) -> Result<(), ErrorEmitted> {
let mod_path = self.namespace().mod_path.clone();
self.namespace_mut()
.root
.variant_star_import(handler, engines, src, &mod_path, enum_name)
.variant_star_import(handler, engines, src, &mod_path, enum_name, visibility)
}

/// Short-hand for performing a [Module::self_import] with `mod_path` as the destination.
Expand All @@ -136,11 +138,12 @@ impl SymbolCollectionContext {
engines: &Engines,
src: &ModulePath,
alias: Option<Ident>,
visibility: Visibility,
) -> Result<(), ErrorEmitted> {
let mod_path = self.namespace().mod_path.clone();
self.namespace_mut()
.root
.self_import(handler, engines, src, &mod_path, alias)
.self_import(handler, engines, src, &mod_path, alias, visibility)
}

/// Short-hand for performing a [Module::item_import] with `mod_path` as the destination.
Expand All @@ -151,11 +154,12 @@ impl SymbolCollectionContext {
src: &ModulePath,
item: &Ident,
alias: Option<Ident>,
visibility: Visibility,
) -> Result<(), ErrorEmitted> {
let mod_path = self.namespace().mod_path.clone();
self.namespace_mut()
.root
.item_import(handler, engines, src, item, &mod_path, alias)
.item_import(handler, engines, src, item, &mod_path, alias, visibility)
}

/// Short-hand for performing a [Module::variant_import] with `mod_path` as the destination.
Expand All @@ -168,6 +172,7 @@ impl SymbolCollectionContext {
enum_name: &Ident,
variant_name: &Ident,
alias: Option<Ident>,
visibility: Visibility,
) -> Result<(), ErrorEmitted> {
let mod_path = self.namespace().mod_path.clone();
self.namespace_mut().root.variant_import(
Expand All @@ -178,6 +183,7 @@ impl SymbolCollectionContext {
variant_name,
&mod_path,
alias,
visibility,
)
}
}
21 changes: 12 additions & 9 deletions sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
language::{
parsed::{Declaration, FunctionDeclaration},
ty::{self, StructAccessInfo, TyDecl, TyStorageDecl},
CallPath,
CallPath, Visibility,
},
namespace::*,
semantic_analysis::{ast_node::ConstShadowingMode, GenericShadowingMode},
Expand Down Expand Up @@ -36,10 +36,11 @@ impl ResolvedFunctionDecl {
}

pub(super) type SymbolMap = im::OrdMap<Ident, ResolvedDeclaration>;

type SourceIdent = Ident;
pub(super) type GlobSynonyms = im::HashMap<Ident, Vec<(ModulePathBuf, ResolvedDeclaration)>>;
pub(super) type ItemSynonyms =
im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ResolvedDeclaration)>;

pub(super) type GlobSynonyms = im::HashMap<Ident, Vec<(ModulePathBuf, ResolvedDeclaration, Visibility)>>;
pub(super) type ItemSynonyms = im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ResolvedDeclaration, Visibility)>;

/// Represents a lexical scope integer-based identifier, which can be used to reference
/// specific a lexical scope.
Expand Down Expand Up @@ -464,7 +465,7 @@ impl Items {
);
}

if let Some((ident, (imported_ident, _, decl))) =
if let Some((ident, (imported_ident, _, decl, _))) =
self.use_item_synonyms.get_key_value(&name)
{
append_shadowing_error(
Expand Down Expand Up @@ -494,12 +495,14 @@ impl Items {
symbol: Ident,
src_path: ModulePathBuf,
decl: &ResolvedDeclaration,
visibility: Visibility,
) {
if let Some(cur_decls) = self.use_glob_synonyms.get_mut(&symbol) {
// Name already bound. Check if the decl is already imported
let ctx = PartialEqWithEnginesContext::new(engines);
match cur_decls.iter().position(|(cur_path, cur_decl)| {
match cur_decls.iter().position(|(cur_path, cur_decl, _)| {
cur_decl.eq(decl, &ctx)
// TODO: This shouldn't be necessary anymore
// For some reason the equality check is not sufficient. In some cases items that
// are actually identical fail the eq check, so we have to add heuristics for these
// cases.
Expand All @@ -522,15 +525,15 @@ impl Items {
// This appears to be an issue with the core prelude, and will probably no
// longer be necessary once reexports are implemented:
// https://github.com/FuelLabs/sway/issues/3113
cur_decls[index] = (src_path.to_vec(), decl.clone());
cur_decls[index] = (src_path.to_vec(), decl.clone(), visibility);
}
None => {
// New decl for this name. Add it to the end
cur_decls.push((src_path.to_vec(), decl.clone()));
cur_decls.push((src_path.to_vec(), decl.clone(), visibility));
}
}
} else {
let new_vec = vec![(src_path.to_vec(), decl.clone())];
let new_vec = vec![(src_path.to_vec(), decl.clone(), visibility)];
self.use_glob_synonyms.insert(symbol, new_vec);
}
}
Expand Down
Loading

0 comments on commit 9397563

Please sign in to comment.