Skip to content

Commit

Permalink
Block repeated ABI/trait declarations (FuelLabs#3661)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Dec 30, 2022
1 parent fefc1e9 commit 460c9ca
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ impl ty::TyDeclaration {
.methods
.iter_mut()
.for_each(|method| method.replace_implementing_type(engines, decl.clone()));
ctx.namespace.insert_symbol(name, decl.clone());
check!(
ctx.namespace.insert_symbol(name, decl.clone()),
return err(warnings, errors),
warnings,
errors
);
decl
}
parsed::Declaration::ImplTrait(impl_trait) => {
Expand Down Expand Up @@ -291,7 +296,12 @@ impl ty::TyDeclaration {
.methods
.iter_mut()
.for_each(|method| method.replace_implementing_type(engines, decl.clone()));
ctx.namespace.insert_symbol(name, decl.clone());
check!(
ctx.namespace.insert_symbol(name, decl.clone()),
return err(warnings, errors),
warnings,
errors
);
decl
}
parsed::Declaration::StorageDeclaration(parsed::StorageDeclaration {
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/semantic_analysis/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ impl Items {
if self.symbols.get(&name).is_some() {
match item {
ty::TyDeclaration::EnumDeclaration { .. }
| ty::TyDeclaration::StructDeclaration { .. } => {
| ty::TyDeclaration::StructDeclaration { .. }
| ty::TyDeclaration::AbiDeclaration { .. }
| ty::TyDeclaration::TraitDeclaration { .. } => {
errors.push(CompileError::ShadowsOtherSymbol { name: name.clone() });
}
ty::TyDeclaration::GenericTypeForFunctionScope { .. } => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'repeated_abi_declaration'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "repeated_abi_declaration"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract;

abi MyContract {
fn foo() -> u64;
}

abi MyContract {
fn bar() -> u64;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

# check: $()The name "MyContract" shadows another symbol with the same name.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'repeated_trait_declaration'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "repeated_trait_declaration"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract;

trait MyTrait {
fn foo() -> u64;
}

trait MyTrait {
fn bar() -> u64;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

# check: $()The name "MyTrait" shadows another symbol with the same name.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ trait MyAdd {
fn my_add(self, other: Self) -> Self;
}

trait MyMul {
fn my_mul(self, other: Self) -> Self;
}

impl MyAdd for u8 {
fn my_add(self, other: Self) -> Self {
self + other
Expand Down

0 comments on commit 460c9ca

Please sign in to comment.