Using for with global binding.

This commit is contained in:
chriseth
2022-03-14 17:39:14 +01:00
parent 7f360e61fc
commit 9188519f11
26 changed files with 307 additions and 6 deletions
@@ -0,0 +1,21 @@
==== Source: A ====
using {f} for S global;
struct S { uint x; }
function gen() pure returns (S memory) {}
function f(S memory _x) pure returns (uint) { return _x.x; }
==== Source: B ====
contract C {
using {fun} for S;
// Adds the same function again with the same name,
// so it's fine.
using {A.f} for S;
function test() pure public
{
uint p = g().f();
p = g().fun();
}
}
import {gen as g, f as fun, S} from "A";
import "A" as A;
// ----
@@ -0,0 +1,5 @@
using {f} for * global;
function f(uint) pure{}
// ----
// SyntaxError 8118: (0-23): The type has to be specified explicitly at file level (cannot use '*').
// SyntaxError 2854: (0-23): Can only globally bind functions to specific types.
@@ -0,0 +1,4 @@
using {f} for uint global;
function f(uint) pure{}
// ----
// TypeError 8841: (0-26): Can only use "global" with user-defined types.
@@ -0,0 +1,7 @@
using {f} for L.S global;
function f(L.S memory) pure{}
library L {
struct S { uint x; }
}
// ----
// TypeError 4117: (0-25): Can only use "global" with types defined in the same source unit at file level.
@@ -0,0 +1,14 @@
==== Source: A ====
struct S { uint x; }
==== Source: B ====
using {f} for S global;
using {f} for A.S global;
function f(S memory) pure{}
import {S} from "A";
import "A" as A;
// ----
// TypeError 4117: (B:1-24): Can only use "global" with types defined in the same source unit at file level.
// TypeError 4117: (B:25-50): Can only use "global" with types defined in the same source unit at file level.
@@ -0,0 +1,7 @@
contract C {
using {f} for uint global;
}
function f(uint) pure{}
// ----
// SyntaxError 3367: (17-43): "global" can only be used at file level.
// TypeError 8841: (17-43): Can only use "global" with user-defined types.
@@ -0,0 +1,21 @@
==== Source: A ====
using {f} for S global;
struct S { uint x; }
function gen() pure returns (S memory) {}
function f(S memory _x) pure returns (uint) { return _x.x; }
function f1(S memory _x) pure returns (uint) { return _x.x + 1; }
==== Source: B ====
contract C {
// Here, f points to f1, so we end up with two different functions
// bound as S.f
using {f} for S;
function test() pure public
{
uint p = g().f();
}
}
import {gen as g, f1 as f, S} from "A";
import "A" as A;
// ----
// TypeError 6675: (B:181-186): Member "f" not unique after argument-dependent lookup in struct S memory.
@@ -0,0 +1,17 @@
==== Source: A ====
using {f} for S global;
using {g} for S;
struct S { uint x; }
function gen() pure returns (S memory) {}
function f(S memory _x) pure { _x.g(); }
function g(S memory _x) pure { }
==== Source: B ====
import "A";
function test() pure
{
gen().f();
gen().g();
}
// ----
// TypeError 9582: (B:54-61): Member "g" not found or not visible after argument-dependent lookup in struct S memory.
@@ -0,0 +1,15 @@
==== Source: A ====
using {f} for S global;
// this should not conflict
using {f} for S;
struct S { uint x; }
function gen() pure returns (S memory) {}
function f(S memory _x) pure returns (uint) { return _x.x; }
==== Source: B ====
function test() pure
{
uint p = g().f();
p++;
}
import {gen as g} from "A";
// ----