Implement loading namespaces with the using keyword (#338)
* Add using namespace as to scanner and parser * Change the alias from id() to con() * Add using namespace to AST type inference * Allow using namespace to appear in the top level * Allow using namespace to appear inside functions * Add a compiler test for using namespace * Handle name collisions * Implement mk_error for ambiguous_name * Add failing test for ambiguous names * Limit the scope of the used namespaces * Add test for wrong scope of using namespace * Use a single using declaration * Split long line * Forbid using undefined namespaces * Add a test for using undefined namespaces * Change the type of used_namespaces * Add using namespace parts to scanner and parser * Add using namespace parts to ast type inference * Add tests for using namespace parts * Update CHANGELOG.md * Code cleaning * Update the docs * Update the docs about the same alias for multiple namespaces
This commit is contained in:
@@ -200,6 +200,7 @@ compilable_contracts() ->
|
||||
"clone_simple",
|
||||
"create",
|
||||
"child_contract_init_bug",
|
||||
"using_namespace",
|
||||
"test" % Custom general-purpose test file. Keep it last on the list.
|
||||
].
|
||||
|
||||
@@ -781,6 +782,30 @@ failing_contracts() ->
|
||||
[<<?Pos(1,6)
|
||||
"Only one main contract can be defined.">>
|
||||
])
|
||||
, ?TYPE_ERROR(using_namespace_ambiguous_name,
|
||||
[ <<?Pos(2,3)
|
||||
"Ambiguous name: Xa.f at line 2, column 3\nXb.f at line 5, column 3">>
|
||||
, <<?Pos(13,23)
|
||||
"Unbound variable A.f at line 13, column 23">>
|
||||
])
|
||||
, ?TYPE_ERROR(using_namespace_wrong_scope,
|
||||
[ <<?Pos(19,5)
|
||||
"Unbound variable f at line 19, column 5">>
|
||||
, <<?Pos(21,23)
|
||||
"Unbound variable f at line 21, column 23">>
|
||||
])
|
||||
, ?TYPE_ERROR(using_namespace_undefined,
|
||||
[<<?Pos(2,3)
|
||||
"Cannot use undefined namespace MyUndefinedNamespace">>
|
||||
])
|
||||
, ?TYPE_ERROR(using_namespace_undefined_parts,
|
||||
[<<?Pos(5,3)
|
||||
"The namespace Nsp does not define the following names: a">>
|
||||
])
|
||||
, ?TYPE_ERROR(using_namespace_hidden_parts,
|
||||
[<<?Pos(8,23)
|
||||
"Unbound variable g at line 8, column 23">>
|
||||
])
|
||||
].
|
||||
|
||||
-define(Path(File), "code_errors/" ??File).
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
include "Option.aes"
|
||||
include "Pair.aes"
|
||||
include "String.aes"
|
||||
include "Triple.aes"
|
||||
|
||||
using Pair
|
||||
using Triple hiding [fst, snd]
|
||||
|
||||
namespace Nsp =
|
||||
using Option
|
||||
|
||||
function h() =
|
||||
let op = Some((2, 3, 4))
|
||||
if (is_some(op))
|
||||
thd(force(op)) == 4
|
||||
else
|
||||
false
|
||||
|
||||
contract Cntr =
|
||||
using Nsp
|
||||
|
||||
entrypoint init() = ()
|
||||
|
||||
function f() =
|
||||
let p = (1, 2)
|
||||
if (h())
|
||||
fst(p)
|
||||
else
|
||||
snd(p)
|
||||
|
||||
function g() =
|
||||
using String for [concat]
|
||||
|
||||
let s1 = "abc"
|
||||
let s2 = "def"
|
||||
concat(s1, s2)
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Xa =
|
||||
function f() = 1
|
||||
|
||||
namespace Xb =
|
||||
function f() = 2
|
||||
|
||||
contract Cntr =
|
||||
using Xa as A
|
||||
using Xb as A
|
||||
|
||||
type state = int
|
||||
|
||||
entrypoint init() = A.f()
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Nsp =
|
||||
function f() = 1
|
||||
function g() = 2
|
||||
|
||||
contract Cntr =
|
||||
using Nsp for [f]
|
||||
|
||||
entrypoint init() = g()
|
||||
@@ -0,0 +1,4 @@
|
||||
contract C =
|
||||
using MyUndefinedNamespace
|
||||
|
||||
entrypoint init() = ()
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Nsp =
|
||||
function f() = 1
|
||||
|
||||
contract Cntr =
|
||||
using Nsp for [a]
|
||||
|
||||
entrypoint init() = f()
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Nsp1 =
|
||||
function f() = 1
|
||||
|
||||
namespace Nsp2 =
|
||||
using Nsp1
|
||||
|
||||
function g() = 1
|
||||
|
||||
contract Cntr =
|
||||
using Nsp2
|
||||
|
||||
type state = int
|
||||
|
||||
function x() =
|
||||
using Nsp1
|
||||
f()
|
||||
|
||||
function y() =
|
||||
f()
|
||||
|
||||
entrypoint init() = f()
|
||||
Reference in New Issue
Block a user