Skip to content

Commit

Permalink
Merge pull request #695 from Narasimha1997/allow-unsafe-publics
Browse files Browse the repository at this point in the history
Allow public unsafe smart contract functions to be called from outside
  • Loading branch information
g-r-a-n-t authored Apr 25, 2022
2 parents 182d189 + fc74ab6 commit 4aa56e2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 30 deletions.
15 changes: 1 addition & 14 deletions crates/analyzer/src/db/queries/functions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::context::{AnalyzerContext, CallType, FunctionBody};
use crate::db::{Analysis, AnalyzerDb};
use crate::errors::TypeError;
use crate::namespace::items::{
Class, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, TypeDef,
};
use crate::namespace::items::{DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, TypeDef};
use crate::namespace::scopes::{BlockScope, BlockScopeType, FunctionScope, ItemScope};
use crate::namespace::types::{self, Contract, CtxDecl, FixedSize, SelfDecl, Struct, Type};
use crate::traversal::functions::traverse_statements;
Expand All @@ -27,17 +25,6 @@ pub fn function_signature(
let mut scope = ItemScope::new(db, function.module(db));
let fn_parent = function.class(db);

if_chain! {
if let Some(Class::Contract(_)) = fn_parent;
if let Some(pub_span) = function.pub_span(db);
if let Some(unsafe_span) = function.unsafe_span(db);
then {
scope.error("public contract functions can't be unsafe",
pub_span + unsafe_span,
"a contract function can be either `pub` or `unsafe`, but not both");
}
}

let mut self_decl = None;
let mut ctx_decl = None;
let mut names = HashMap::new();
Expand Down
13 changes: 0 additions & 13 deletions crates/analyzer/tests/snapshots/errors__unsafe_misuse.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/analyzer/tests/errors.rs
expression: "error_string(&path, test_files::fixture(path))"

---
error: unsafe function `mod_priv` can only be called in an unsafe function or block
┌─ compile_errors/unsafe_misuse.fe:10:3
Expand All @@ -14,18 +13,6 @@ error: unsafe function `mod_priv` can only be called in an unsafe function or bl
= Hint: put this call in an `unsafe` block if you're confident that it's safe to use here

error: public contract functions can't be unsafe
┌─ compile_errors/unsafe_misuse.fe:18:3
18 │ pub unsafe fn pub_self(self): # BAD
│ ^^^^^^^^^^ a contract function can be either `pub` or `unsafe`, but not both

error: public contract functions can't be unsafe
┌─ compile_errors/unsafe_misuse.fe:20:3
20 │ pub unsafe fn pub_noself(): # BAD
│ ^^^^^^^^^^ a contract function can be either `pub` or `unsafe`, but not both

error: unsafe function `priv_self` can only be called in an unsafe function or block
┌─ compile_errors/unsafe_misuse.fe:38:5
Expand Down
6 changes: 3 additions & 3 deletions crates/test-files/fixtures/compile_errors/unsafe_misuse.fe
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub fn g():
mod_priv() # OK

contract Foo:
# pub unsafe contract functions aren't allowed
pub unsafe fn pub_self(self): # BAD
# pub unsafe contract functions are allowed
pub unsafe fn pub_self(self): # Ok
pass
pub unsafe fn pub_noself(): # BAD
pub unsafe fn pub_noself(): # Ok
pass

# non-pub contract fns can be unsafe
Expand Down

0 comments on commit 4aa56e2

Please sign in to comment.