Contract level checker: Disallow free function redefinition and alias

shadowing another free function

Co-authored-by: chriseth <chris@ethereum.org>
This commit is contained in:
Bhargava Shastry
2020-09-25 17:09:58 +02:00
co-authored by chriseth
parent 1cc0d642e8
commit e2a2276272
45 changed files with 564 additions and 17 deletions
@@ -0,0 +1,10 @@
function f() pure returns (uint) { return 1337; }
contract C {
function f() public pure returns (uint) {
return f();
}
}
// ====
// compileViaYul: also
// ----
// f() -> FAILURE
@@ -0,0 +1,15 @@
==== Source: s1.sol ====
import {f as g} from "s2.sol";
function f() pure returns (uint) { return 1; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
function f() pure returns (uint) { return 2; }
contract C {
function foo() public pure returns (uint) {
return f() - g();
}
}
// ====
// compileViaYul: also
// ----
// foo() -> 1
@@ -0,0 +1,14 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
contract C {
function foo() public pure returns (uint) {
return h() - f() - g();
}
}
// ----
// foo() -> -4
@@ -0,0 +1,16 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
==== Source: s3.sol ====
import "s1.sol";
contract C {
function foo() public pure returns (uint) {
return f() - g() - h();
}
}
// ----
// foo() -> -4
@@ -0,0 +1,16 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
==== Source: s3.sol ====
import "s2.sol";
contract C {
function foo() public pure returns (uint) {
return f() - g() - h();
}
}
// ----
// foo() -> -4
@@ -0,0 +1,14 @@
==== Source: s1.sol ====
function f(uint24) pure returns (uint) { return 24; }
function g(bool) pure returns (bool) { return true; }
==== Source: s2.sol ====
import {f as g, g as g} from "s1.sol";
contract C {
function foo() public pure returns (uint, bool) {
return (g(2), g(false));
}
}
// ====
// compileViaYul: also
// ----
// foo() -> 24, true
@@ -0,0 +1,18 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {
function h() public pure returns (uint) {
return g();
}
}
// ====
// compileViaYul: also
// ----
// h() -> 1337
@@ -0,0 +1,18 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure virtual returns (uint) {
return f() + 1;
}
}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {
function g() public pure override returns (uint) {
return f();
}
}
// ====
// compileViaYul: also
// ----
// g() -> 1337
@@ -0,0 +1,18 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure virtual returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {
function g() public pure override returns (uint) {
return super.g();
}
}
// ====
// compileViaYul: also
// ----
// g() -> 1337
@@ -0,0 +1,25 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure virtual returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {
function g() public pure virtual override returns (uint) {
return super.g() + 1;
}
}
==== Source: s3.sol ====
import "s2.sol";
contract E is D {
function g() public pure override returns (uint) {
return super.g() + 1;
}
}
// ====
// compileViaYul: also
// ----
// g() -> 1339
@@ -0,0 +1,27 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {
function h() public pure returns (uint) {
return g();
}
}
==== Source: s3.sol ====
import "s2.sol";
import {f as f} from "s2.sol";
contract E is D {
function i() public pure returns (uint) {
return f();
}
}
// ====
// compileViaYul: also
// ----
// i() -> 1337
@@ -0,0 +1,19 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function g() public pure virtual returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol" as M;
function f() pure returns (uint) { return 6; }
contract D is M.C {
function g() public pure override returns (uint) {
return super.g() + f() * 10000;
}
}
// ====
// compileViaYul: also
// ----
// g() -> 61337
@@ -0,0 +1,14 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
function f() pure returns (uint) { return 6; }
contract D {
function h() public pure returns (uint) {
return g() + f() * 10000;
}
}
// ====
// compileViaYul: also
// ----
// h() -> 61337
@@ -0,0 +1,15 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
==== Source: s3.sol ====
import {g as h} from "s2.sol";
contract C {
function foo() public pure returns (uint) {
return h();
}
}
// ====
// compileViaYul: also
// ----
// foo() -> 1337
@@ -0,0 +1,4 @@
function f(uint24) {}
function f(uint16) {}
function f(int24) {}
function f(bool) {}
@@ -0,0 +1,11 @@
function g() pure returns (uint) { return 1; }
function g() pure returns (string memory) { return "1"; }
contract C {
function foo() public pure returns (uint) {
string memory s = g();
return 100/g();
}
}
// ----
// DeclarationError 1686: (0-46): Function with same name and parameter types defined twice.
// TypeError 9574: (168-189): Type uint256 is not implicitly convertible to expected type string memory.
@@ -0,0 +1,4 @@
function f() pure returns (uint) { return 1337; }
function f() pure returns (uint) { return 42; }
// ----
// DeclarationError 1686: (0-49): Function with same name and parameter types defined twice.
@@ -0,0 +1,5 @@
function f() pure returns (uint) { return 1337; }
function f() pure returns (uint) { return 42; }
function f() pure returns (uint) { return 1; }
// ----
// DeclarationError 1686: (0-49): Function with same name and parameter types defined twice.
@@ -0,0 +1,8 @@
function f() pure returns (uint) { return 1337; }
contract C {
function f() public pure returns (uint) {
return f();
}
}
// ----
// Warning 2519: (65-126): This declaration shadows an existing declaration.
@@ -0,0 +1,9 @@
function f() pure returns (uint) { return 1337; }
function f() view returns (uint) { return 42; }
contract C {
function g() public pure virtual returns (uint) {
return f();
}
}
// ----
// DeclarationError 1686: (0-49): Function with same name and parameter types defined twice.
@@ -0,0 +1,19 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
function g() pure returns (uint) { return 42; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
==== Source: s3.sol ====
// imports f()->1337 as g()
import "s2.sol";
// imports f()->1337 as f() and
// g()->42 as g
import {f as f, g as g} from "s1.sol";
contract C {
function foo() public pure returns (uint) {
// calls f()->1337 / f()->1337
return f() / g();
}
}
// ----
// DeclarationError 1686: (s1.sol:0-49): Function with same name and parameter types defined twice.
@@ -0,0 +1,19 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
function g() pure returns (uint) { return 42; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
==== Source: s3.sol ====
// imports f()->1337 as g()
import "s2.sol";
// imports f()->1337 as f() and
// g()->42 as g
import "s1.sol";
contract C {
function foo() public pure returns (uint) {
// calls f()->1337 / f()->1337
return f() / g();
}
}
// ----
// DeclarationError 1686: (s1.sol:0-49): Function with same name and parameter types defined twice.
@@ -0,0 +1,6 @@
==== Source: s1.sol ====
import {f as g} from "s2.sol";
function f() pure returns (uint) { return 1; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
function f() pure returns (uint) { return 2; }
@@ -0,0 +1,7 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
@@ -0,0 +1,14 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
==== Source: s3.sol ====
import "s1.sol";
import "s2.sol";
// ----
// DeclarationError 1686: (s1.sol:39-93): Function with same name and parameter types defined twice.
// DeclarationError 1686: (s2.sol:31-77): Function with same name and parameter types defined twice.
// DeclarationError 1686: (s2.sol:78-124): Function with same name and parameter types defined twice.
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
==== Source: s3.sol ====
import "s1.sol";
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
import {f as g, g as h} from "s2.sol";
function f() pure returns (uint) { return h() - g(); }
==== Source: s2.sol ====
import {f as h} from "s1.sol";
function f() pure returns (uint) { return 2; }
function g() pure returns (uint) { return 4; }
==== Source: s3.sol ====
import "s2.sol";
@@ -0,0 +1,10 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as f} from "s1.sol";
import {f as f} from "s1.sol";
contract C {
function g() public pure returns (uint) {
return f();
}
}
@@ -0,0 +1,10 @@
==== Source: s1.sol ====
function f(uint) pure returns (uint) { return 24; }
function g() pure returns (bool) { return true; }
==== Source: s2.sol ====
import {f as g, g as g} from "s1.sol";
contract C {
function foo() public pure returns (uint, bool) {
return (g(2), g());
}
}
@@ -0,0 +1,19 @@
==== Source: s1.sol ====
function f() pure returns (uint16) { return 1337; }
function g() pure returns (uint8) { return 42; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
==== Source: s3.sol ====
// imports f(uint16)->1337 as g(uint16)
import "s2.sol";
// imports f(uint16)->1337 as f(uint16) and
// g(uint8)->42 as g(uint8)
import {f as f, g as g} from "s1.sol";
contract C {
function foo() public pure returns (uint) {
// calls f()->1337 / f()->1337
return f() / g();
}
}
// ----
// DeclarationError 1686: (s1.sol:0-51): Function with same name and parameter types defined twice.
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {}
==== Source: s2.sol ====
import "s1.sol";
function f() pure returns (uint) { return 42; }
contract D is C {}
// ----
// DeclarationError 1686: (s2.sol:17-64): Function with same name and parameter types defined twice.
@@ -0,0 +1,12 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {}
==== Source: s2.sol ====
import "s1.sol";
contract D is C {}
==== Source: s3.sol ====
import "s2.sol";
function f() pure returns (uint) { return 42; }
contract E is D {}
// ----
// DeclarationError 1686: (s3.sol:17-64): Function with same name and parameter types defined twice.
@@ -0,0 +1,19 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
contract C {
function f() public pure virtual returns (uint) {
return f();
}
}
==== Source: s2.sol ====
import "s1.sol";
function f() pure returns (uint) { return 42; }
contract D is C {
function f() public pure override returns (uint) {
return f();
}
}
// ----
// Warning 2519: (s1.sol:65-134): This declaration shadows an existing declaration.
// Warning 2519: (s2.sol:85-155): This declaration shadows an existing declaration.
// DeclarationError 1686: (s2.sol:17-64): Function with same name and parameter types defined twice.
@@ -0,0 +1,10 @@
==== Source: s1.sol ====
contract C {
function f() public pure returns (uint) {
return 1337;
}
}
==== Source: s2.sol ====
import {C.f as g} from "s1.sol";
// ----
// ParserError 2314: (s2.sol:9-10): Expected '}' but got '.'
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import "s1.sol";
contract C {
function g() public pure returns (uint) {
return f();
}
}
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as f, f as f, f as f} from "s1.sol";
contract C {
function g() public pure returns (uint) {
return f();
}
}
@@ -0,0 +1,11 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as g} from "s1.sol";
==== Source: s3.sol ====
import {g as h} from "s2.sol";
contract C {
function foo() public pure returns (uint) {
return h();
}
}
@@ -0,0 +1,9 @@
==== Source: s1.sol ====
function f() pure returns (uint) { return 1337; }
==== Source: s2.sol ====
import {f as f, f as f} from "s1.sol";
contract C {
function g() public pure returns (uint) {
return f();
}
}