Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions crates/common/src/preprocessor/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,11 @@ pub(crate) fn remove_bytecode_dependencies(
call_args_offset,
value,
salt,
try_stmt,
try_stmt: _,
} => {
let (mut update, closing_seq) = if *try_stmt {
(String::new(), "})")
} else {
(format!("{name}(payable("), "})))")
};
// Always wrap in `{name}(payable(...))` so the expression remains of the
// contract type in both try and non-try contexts.
let (mut update, closing_seq) = (format!("{name}(payable("), "})))");
update.push_str(&format!("{vm}.deployCode({{"));
update.push_str(&format!("_artifact: \"{artifact}\""));

Expand Down
48 changes: 48 additions & 0 deletions crates/forge/tests/cli/test_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,3 +1535,51 @@ Compiling 1 files with [..]

"#]]);
});

// Preprocess test contracts with try constructor statements that bind return type.
forgetest_init!(preprocess_contract_with_try_ctor_stmt_and_returns, |prj, cmd| {
prj.wipe_contracts();
prj.update_config(|config| {
config.dynamic_test_linking = true;
});

prj.add_source(
"CounterB.sol",
r#"
contract CounterB {
uint256 number;
constructor(uint256 a) payable {
require(a > 0, \"ctor failure\");
number = a;
}
}
"#,
);

prj.add_test(
"CounterReturns.t.sol",
r#"
import {Test} from \"forge-std/Test.sol\";
import {CounterB} from \"../src/CounterB.sol\";

contract CounterReturnsTest is Test {
function test_try_counterB_creation_returns() public {
try new CounterB(1) returns (CounterB c) {
// ensure the bound variable is of type CounterB
c;
} catch {
revert();
}
}
}
"#,
);

cmd.args(["test"]).with_no_redact().assert_success().stdout_eq(str![[r#"
...
Compiling 12 files with [..]
...
[PASS] test_try_counterB_creation_returns() (gas: [..])
...
"#]]);
});
Loading