Provide unique name from closures with same name (#488)

In YUL it's possible to declare closure with a same name, but in
different scope, which previously resulted to func name conflict during
LLVM IR construction.
To solve this, track YUL scope when building LLVM IR and a special
counter that increments every time function with the same name exists in
LLVM module.
There's still assert if somehow within the same scope there 2 functions
with the same name, which should be forbidden by Yul

Fixes: #474

---------

Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
This commit is contained in:
kvpanchandLJ authored and GitHub committed 2026-03-10 10:55:42 -04:00
1 parent dc155efc73
commit 61ac247135
8 files changed
+218 -19

No files matched your search

+29 -1
View File
@@ -2,7 +2,9 @@
use crate::cli_utils::{
assert_command_failure, assert_command_success, assert_equal_exit_codes, execute_resolc,
execute_solc, RESOLC_YUL_FLAG, SOLC_YUL_FLAG, YUL_CONTRACT_PATH, YUL_INVALID_HEX_NIBBLES_PATH,
execute_solc, RESOLC_YUL_FLAG, SOLC_YUL_FLAG, YUL_CONTRACT_PATH,
YUL_DUPLICATE_FUNCTIONS_DEEP_NESTING_PATH, YUL_DUPLICATE_FUNCTIONS_SWITCH_PATH,
YUL_INVALID_HEX_NIBBLES_PATH,
};
#[test]
@@ -35,3 +37,29 @@ fn bails_with_invalid_input_file() {
let solc_result = execute_solc(&[YUL_INVALID_HEX_NIBBLES_PATH, SOLC_YUL_FLAG]);
assert_equal_exit_codes(&solc_result, &resolc_result);
}
#[test]
fn duplicate_functions_in_switch_cases() {
let resolc_result = execute_resolc(&[
YUL_DUPLICATE_FUNCTIONS_SWITCH_PATH,
RESOLC_YUL_FLAG,
"--bin",
]);
assert_command_success(
&resolc_result,
"Duplicate function names in different switch cases",
);
}
#[test]
fn duplicate_functions_deep_nesting() {
let resolc_result = execute_resolc(&[
YUL_DUPLICATE_FUNCTIONS_DEEP_NESTING_PATH,
RESOLC_YUL_FLAG,
"--bin",
]);
assert_command_success(
&resolc_result,
"Duplicate function names in deeply nested switch cases",
);
}
@@ -0,0 +1,58 @@
object "Test" {
code {
{
let size := datasize("Test_deployed")
codecopy(0, dataoffset("Test_deployed"), size)
return(0, size)
}
}
object "Test_deployed" {
code {
{
switch calldataload(0)
case 0 {
function f() -> ret {
ret := 1
}
switch calldataload(32)
case 0 {
function g() -> ret {
ret := 10
}
mstore(0, add(f(), g()))
return(0, 32)
}
case 1 {
function g() -> ret {
ret := 20
}
mstore(0, add(f(), g()))
return(0, 32)
}
}
case 1 {
function f() -> ret {
ret := 2
}
switch calldataload(32)
case 0 {
function g() -> ret {
ret := 30
}
mstore(0, add(f(), g()))
return(0, 32)
}
case 1 {
function g() -> ret {
ret := 40
}
mstore(0, add(f(), g()))
return(0, 32)
}
}
}
}
}
}
@@ -0,0 +1,30 @@
object "Test" {
code {
{
let size := datasize("Test_deployed")
codecopy(0, dataoffset("Test_deployed"), size)
return(0, size)
}
}
object "Test_deployed" {
code {
{
switch calldataload(0)
case 0 {
function f() -> ret {
ret := 1
}
mstore(0, f())
return(0, 32)
}
case 1 {
function f() -> ret {
ret := 2
}
mstore(0, f())
return(0, 32)
}
}
}
}
}
+1
View File
@@ -9,3 +9,4 @@ mod remappings;
mod runtime_code;
mod standard_json;
mod unsupported_opcodes;
mod yul_function_scoping;
@@ -0,0 +1,43 @@
//! Tests for Yul function scoping and duplicate function name handling.
use crate::test_utils::build_yul;
/// Reproducer from GH-474: duplicate `f` across switch cases.
#[test]
fn duplicate_function_names_in_switch_cases() {
let code = r#"
object "Test" {
code {
{
let size := datasize("Test_deployed")
codecopy(0, dataoffset("Test_deployed"), size)
return(0, size)
}
}
object "Test_deployed" {
code {
{
switch calldataload(0)
case 0 {
function f() -> ret {
ret := 1
}
mstore(0, f())
return(0, 32)
}
case 1 {
function f() -> ret {
ret := 2
}
mstore(0, f())
return(0, 32)
}
}
}
}
}
"#;
build_yul(&[("test.yul", code)])
.expect("should compile duplicate function names in switch cases");
}