Skip to content

Commit 35e367d

Browse files
committed
Add tests and allow user-defined functions with blobhash identifier for EVM versions pre cancun
1 parent 1643a6d commit 35e367d

File tree

9 files changed

+104
-1
lines changed

9 files changed

+104
-1
lines changed

libyul/backends/evm/EVMDialect.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,22 @@ std::set<YulString> createReservedIdentifiers(langutil::EVMVersion _evmVersion)
128128
return _instrName == "prevrandao" && _evmVersion < langutil::EVMVersion::paris();
129129
};
130130

131+
// TODO remove this in 0.9.0. We allow creating functions or identifiers in Yul with the name
132+
// blobhash for VMs before cancun.
133+
auto blobHashException = [&](evmasm::Instruction _instr) -> bool
134+
{
135+
return _instr == evmasm::Instruction::BLOBHASH && _evmVersion < langutil::EVMVersion::cancun();
136+
};
137+
131138
std::set<YulString> reserved;
132139
for (auto const& instr: evmasm::c_instructions)
133140
{
134141
std::string name = toLower(instr.first);
135-
if (!baseFeeException(instr.second) && !prevRandaoException(name))
142+
if (
143+
!baseFeeException(instr.second) &&
144+
!prevRandaoException(name) &&
145+
!blobHashException(instr.second)
146+
)
136147
reserved.emplace(name);
137148
}
138149
reserved += std::vector<YulString>{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
contract C {
2+
function f() public pure returns (uint ret) {
3+
assembly {
4+
let blobhash := 1
5+
ret := blobhash
6+
}
7+
}
8+
function g() public pure returns (uint ret) {
9+
assembly {
10+
function blobhash() -> r {
11+
r := 1000
12+
}
13+
ret := blobhash()
14+
}
15+
}
16+
}
17+
// ====
18+
// EVMVersion: <=shanghai
19+
// compileViaYul: also
20+
// ----
21+
// f() -> 1
22+
// g() -> 1000
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
contract C {
2+
function f() public view returns (bytes32 ret) {
3+
assembly {
4+
ret := blobhash(1)
5+
}
6+
}
7+
}
8+
// ====
9+
// EVMVersion: >=cancun
10+
// ----
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contract C {
2+
function f() pure external returns (bytes32 ret) {
3+
assembly {
4+
ret := blobhash()
5+
}
6+
}
7+
}
8+
// ====
9+
// EVMVersion: =shanghai
10+
// ----
11+
// DeclarationError 4619: (106-114): Function "blobhash" not found.
12+
// DeclarationError 8678: (99-116): Variable count for assignment to "ret" does not match number of values (1 vs. 0)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
contract C {
2+
function f() public pure returns (uint ret) {
3+
assembly {
4+
function blobhash() -> r {
5+
r := 1000
6+
}
7+
ret := blobhash()
8+
}
9+
}
10+
}
11+
// ====
12+
// EVMVersion: >=cancun
13+
// ----
14+
// ParserError 5568: (103-111): Cannot use builtin function name "blobhash" as identifier name.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contract C {
2+
function f() public view returns (bytes32 ret) {
3+
assembly {
4+
ret := blobhash(1)
5+
}
6+
}
7+
}
8+
// ====
9+
// EVMVersion: <=shanghai
10+
// ----
11+
// DeclarationError 4619: (104-112): Function "blobhash" not found.
12+
// DeclarationError 8678: (97-115): Variable count for assignment to "ret" does not match number of values (1 vs. 0)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f() public pure {
3+
assembly { pop(blobhash(0)) }
4+
}
5+
}
6+
// ====
7+
// EVMVersion: >=cancun
8+
// ----
9+
// TypeError 2527: (67-78): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
function blobhash() {}
3+
}
4+
// ====
5+
// EVMVersion: >=cancun
6+
// ----
7+
// ParserError 5568: (15-23): Cannot use builtin function name "blobhash" as identifier name.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
function blobhash() {}
3+
}
4+
// ====
5+
// EVMVersion: <=shanghai
6+
// ----

0 commit comments

Comments
 (0)