Skip to content

Commit

Permalink
Added tests of alias reexports, remove alias map from LexicalScope
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed May 1, 2024
1 parent 0856eb9 commit 070f884
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 148 deletions.
22 changes: 11 additions & 11 deletions sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,23 @@ impl CallPath {
let mut is_absolute = false;

if let Some(mod_path) = namespace.module_id(engines).read(engines, |m| {
if let Some((_, path, _)) = m.current_items()
if let Some((_, path, _)) = m
.current_items()
.use_item_synonyms
.get(&self.suffix)
.cloned()
{
Some(path)
}
else if let Some((path, _)) = m.current_items()
{
Some(path)
} else if let Some((path, _)) = m
.current_items()
.use_glob_synonyms
.get(&self.suffix)
.cloned()
{
Some(path)
}
else {
None
}
{
Some(path)
} else {
None
}
}) {
synonym_prefixes = mod_path.clone();
is_absolute = true;
Expand Down
12 changes: 7 additions & 5 deletions sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl ResolvedFunctionDecl {

pub(super) type ParsedSymbolMap = im::OrdMap<Ident, Declaration>;
pub(super) type SymbolMap = im::OrdMap<Ident, ty::TyDecl>;
type TrueIdent = Ident;
type SourceIdent = Ident;
pub(super) type GlobSynonyms = im::HashMap<Ident, (ModulePathBuf, ty::TyDecl)>;
pub(super) type ItemSynonyms = im::HashMap<Ident, (Option<TrueIdent>, ModulePathBuf, ty::TyDecl)>;
pub(super) type ItemSynonyms = im::HashMap<Ident, (Option<SourceIdent>, ModulePathBuf, ty::TyDecl)>;

/// Represents a lexical scope integer-based identifier, which can be used to reference
/// specific a lexical scope.
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct Items {
/// use_item_synonyms contains symbols imported using item imports (`use foo::bar`).
///
/// For aliased item imports `use ::foo::bar::Baz as Wiz` the map key is `Wiz`. `Baz` is stored
/// as the optional true identifier for error reporting purposes.
/// as the optional source identifier for error reporting purposes.
pub(crate) use_glob_synonyms: GlobSynonyms,
pub(crate) use_item_synonyms: ItemSynonyms,
/// If there is a storage declaration (which are only valid in contracts), store it here.
Expand Down Expand Up @@ -284,12 +284,14 @@ impl Items {
append_shadowing_error(ident, decl, false, false, &item, const_shadowing_mode);
}

if let Some((ident, (true_ident, _, decl))) = self.use_item_synonyms.get_key_value(&name) {
if let Some((ident, (imported_ident, _, decl))) =
self.use_item_synonyms.get_key_value(&name)
{
append_shadowing_error(
ident,
decl,
true,
true_ident.is_some(),
imported_ident.is_some(),
&item,
const_shadowing_mode,
);
Expand Down
77 changes: 37 additions & 40 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ impl Root {
.implemented_traits
.extend(implemented_traits, engines);
for symbol_and_decl in symbols_and_decls {
dst_mod.current_items_mut().use_glob_synonyms.insert(
symbol_and_decl.0,
(src.to_vec(), symbol_and_decl.1),
);
dst_mod
.current_items_mut()
.use_glob_synonyms
.insert(symbol_and_decl.0, (src.to_vec(), symbol_and_decl.1));
}

Ok(())
Expand Down Expand Up @@ -167,17 +167,19 @@ impl Root {
};
match alias {
Some(alias) => {
check_name_clash(&alias);
dst_mod.current_items_mut().use_item_synonyms.insert(
alias.clone(),
(Some(item.clone()), src.to_vec(), decl))
},
None => {
check_name_clash(&item);
dst_mod.current_items_mut().use_item_synonyms.insert(
item.clone(),
(None, src.to_vec(), decl))
}
check_name_clash(&alias);
dst_mod
.current_items_mut()
.use_item_synonyms
.insert(alias.clone(), (Some(item.clone()), src.to_vec(), decl))
}
None => {
check_name_clash(item);
dst_mod
.current_items_mut()
.use_item_synonyms
.insert(item.clone(), (None, src.to_vec(), decl))
}
};
}
None => {
Expand Down Expand Up @@ -244,44 +246,43 @@ impl Root {
// import it this way.
let dst_mod = self.module.lookup_submodule_mut(handler, engines, dst)?;
let check_name_clash = |name| {
if dst_mod.current_items().use_item_synonyms.contains_key(name)
{
if dst_mod.current_items().use_item_synonyms.contains_key(name) {
handler.emit_err(CompileError::ShadowsOtherSymbol {
name: name.into(),
});
}
};
match alias {
Some(alias) => {
check_name_clash(&alias);
dst_mod.current_items_mut().use_item_synonyms.insert(
check_name_clash(&alias);
dst_mod.current_items_mut().use_item_synonyms.insert(
alias.clone(),
(
Some(variant_name.clone()),
src.to_vec(),
TyDecl::EnumVariantDecl(ty::EnumVariantDecl {
Some(variant_name.clone()),
src.to_vec(),
TyDecl::EnumVariantDecl(ty::EnumVariantDecl {
enum_ref: enum_ref.clone(),
variant_name: variant_name.clone(),
variant_decl_span: variant_decl.span.clone(),
}),
}),
),
);
},
);
}
None => {
check_name_clash(&variant_name);
dst_mod.current_items_mut().use_item_synonyms.insert(
check_name_clash(variant_name);
dst_mod.current_items_mut().use_item_synonyms.insert(
variant_name.clone(),
(
None,
src.to_vec(),
TyDecl::EnumVariantDecl(ty::EnumVariantDecl {
None,
src.to_vec(),
TyDecl::EnumVariantDecl(ty::EnumVariantDecl {
enum_ref: enum_ref.clone(),
variant_name: variant_name.clone(),
variant_decl_span: variant_decl.span.clone(),
}),
}),
),
);
}
);
}
};
} else {
return Err(handler.emit_err(CompileError::SymbolNotFound {
Expand Down Expand Up @@ -640,9 +641,7 @@ impl Root {
current_mod_path.push(ident.clone());
}
None => {
decl_opt = Some(
self.resolve_symbol_helper(handler, ident, module)?,
);
decl_opt = Some(self.resolve_symbol_helper(handler, ident, module)?);
}
}
}
Expand All @@ -656,8 +655,7 @@ impl Root {
self.module
.lookup_submodule(handler, engines, mod_path)
.and_then(|module| {
let decl =
self.resolve_symbol_helper(handler, symbol, module)?;
let decl = self.resolve_symbol_helper(handler, symbol, module)?;
Ok((decl, mod_path.to_vec()))
})
}
Expand Down Expand Up @@ -815,8 +813,7 @@ impl Root {
return Ok(ResolvedDeclaration::Typed(decl.clone()));
}
// Check item imports
if let Some((_, _, decl)) = module.current_items().use_item_synonyms.get(symbol)
{
if let Some((_, _, decl)) = module.current_items().use_item_synonyms.get(symbol) {
return Ok(ResolvedDeclaration::Typed(decl.clone()));
}
// Check glob imports
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
library;

use ::bar::Bar as Baz; // This is reexported at Baz

pub struct Foo {
pub foo: u64,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ script;
// This tests importing other files.

mod foo;
mod bar;
mod wiz;

use foo::Foo as MyFoo;

use foo::Baz; // Refers to bar::Bar, but foo re-exports using the alias Baz

use wiz::Wiz as WizWiz;

// This is fine - the imported Wiz name is aliased, so no name clash
Expand All @@ -20,17 +17,14 @@ fn main() -> u64 {
let foo = MyFoo {
foo: 42,
};
let bar = Baz {
bar: 64,
};
let wiz = WizWiz {
wiz: 128
};
let local_wiz = Wiz { // This should resolve to the locally defined Wiz
local_wiz: true
};
if local_wiz.local_wiz {
foo.foo + bar.bar + wiz.wiz
foo.foo + wiz.wiz
}
else {
0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
library;

// Dead code analysis disabled due to a bug.
// See https://github.com/FuelLabs/sway/issues/5902#issuecomment-2079212717
#[allow(dead_code)]
pub struct Wiz {
pub wiz: u64,
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "run"
expected_result = { action = "return", value = 234 }
expected_result = { action = "return", value = 170 }
validate_abi = true

0 comments on commit 070f884

Please sign in to comment.