diff --git a/crates/common/src/preprocessor/deps.rs b/crates/common/src/preprocessor/deps.rs index a5c25cbe9ab13..01312719d2169 100644 --- a/crates/common/src/preprocessor/deps.rs +++ b/crates/common/src/preprocessor/deps.rs @@ -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}\"")); diff --git a/crates/forge/tests/cli/test_optimizer.rs b/crates/forge/tests/cli/test_optimizer.rs index 9dc6fc7706fd1..2afed7508249f 100644 --- a/crates/forge/tests/cli/test_optimizer.rs +++ b/crates/forge/tests/cli/test_optimizer.rs @@ -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: [..]) +... +"#]]); +});