@@ -793,8 +793,12 @@ fn expect_emit(
793793pub ( crate ) fn handle_expect_emit (
794794 state : & mut Cheatcodes ,
795795 log : & alloy_primitives:: Log ,
796- interpreter : & mut Interpreter ,
797- ) {
796+ mut interpreter : Option < & mut Interpreter > ,
797+ ) -> Option < & ' static str > {
798+ // This function returns an optional string indicating a failure reason.
799+ // If the string is `Some`, it indicates that the expectation failed with the provided reason.
800+ let mut should_fail = None ;
801+
798802 // Fill or check the expected emits.
799803 // We expect for emit checks to be filled as they're declared (from oldest to newest),
800804 // so we fill them and push them to the back of the queue.
@@ -806,7 +810,7 @@ pub(crate) fn handle_expect_emit(
806810 // This allows a contract to arbitrarily emit more events than expected (additive behavior),
807811 // as long as all the previous events were matched in the order they were expected to be.
808812 if state. expected_emits . iter ( ) . all ( |( expected, _) | expected. found ) {
809- return ;
813+ return should_fail ;
810814 }
811815
812816 // Check count=0 expectations against this log - fail immediately if violated
@@ -818,14 +822,19 @@ pub(crate) fn handle_expect_emit(
818822 // Check revert address
819823 && ( expected_emit. address . is_none ( ) || expected_emit. address == Some ( log. address ) )
820824 {
821- // This event was emitted but we expected it NOT to be (count=0)
822- // Fail immediately
823- interpreter. bytecode . set_action ( InterpreterAction :: new_return (
824- InstructionResult :: Revert ,
825- Error :: encode ( "log emitted 1 time, expected 0" ) ,
826- interpreter. gas ,
827- ) ) ;
828- return ;
825+ if let Some ( interpreter) = & mut interpreter {
826+ // This event was emitted but we expected it NOT to be (count=0)
827+ // Fail immediately
828+ interpreter. bytecode . set_action ( InterpreterAction :: new_return (
829+ InstructionResult :: Revert ,
830+ Error :: encode ( "log emitted but expected 0 times" ) ,
831+ interpreter. gas ,
832+ ) ) ;
833+ } else {
834+ should_fail = Some ( "log emitted but expected 0 times" ) ;
835+ }
836+
837+ return should_fail;
829838 }
830839 }
831840
@@ -850,7 +859,7 @@ pub(crate) fn handle_expect_emit(
850859 if !should_fill_logs
851860 && state. expected_emits . iter ( ) . all ( |( emit, _) | emit. found || emit. count == 0 )
852861 {
853- return ;
862+ return should_fail ;
854863 }
855864
856865 let ( mut event_to_fill_or_check, mut count_map) = state
@@ -867,14 +876,17 @@ pub(crate) fn handle_expect_emit(
867876 state
868877 . expected_emits
869878 . insert ( index_to_fill_or_check, ( event_to_fill_or_check, count_map) ) ;
870- } else {
879+ } else if let Some ( interpreter ) = & mut interpreter {
871880 interpreter. bytecode . set_action ( InterpreterAction :: new_return (
872881 InstructionResult :: Revert ,
873882 Error :: encode ( "use vm.expectEmitAnonymous to match anonymous events" ) ,
874883 interpreter. gas ,
875884 ) ) ;
885+ } else {
886+ should_fail = Some ( "use vm.expectEmitAnonymous to match anonymous events" ) ;
876887 }
877- return ;
888+
889+ return should_fail;
878890 } ;
879891
880892 // Increment/set `count` for `log.address` and `log.data`
@@ -953,6 +965,8 @@ pub(crate) fn handle_expect_emit(
953965 // appear.
954966 state. expected_emits . push_front ( ( event_to_fill_or_check, count_map) ) ;
955967 }
968+
969+ should_fail
956970}
957971
958972/// Handles expected emits specified by the `expectEmit` cheatcodes.
0 commit comments