From 457be797a2c168930d6dc4a7894470b030d9101b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 2 Sep 2021 22:17:45 +0800 Subject: [PATCH] Fixed bug in using error variable within `catch` block. --- CHANGELOG.md | 9 +++++++++ src/parse.rs | 1 + tests/throw.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a8d41a..7084cebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 1.0.3 +============= + +Bug fixes +--------- + +* Fixed bug with `catch` variable used in `catch` block. + + Version 1.0.2 ============= diff --git a/src/parse.rs b/src/parse.rs index a98154cc..e383d597 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -2915,6 +2915,7 @@ fn parse_try_catch( } let name = state.get_identifier(name); + state.stack.push((name.clone(), AccessMode::ReadWrite)); Some(Ident { name, pos }) } else { None diff --git a/tests/throw.rs b/tests/throw.rs index 0b8b717f..a93cfd04 100644 --- a/tests/throw.rs +++ b/tests/throw.rs @@ -29,6 +29,50 @@ fn test_try_catch() -> Result<(), Box> { 123 ); + #[cfg(not(feature = "no_function"))] + assert_eq!( + engine.eval::( + " + fn foo(x) { try { throw 42; } catch (x) { return x; } } + foo(0) + " + )?, + 42 + ); + + assert_eq!( + engine.eval::( + " + let err = 123; + let x = 0; + try { throw 42; } catch(err) { return err; } + " + )?, + 42 + ); + + assert_eq!( + engine.eval::( + " + let foo = 123; + let x = 0; + try { throw 42; } catch(err) { return foo; } + " + )?, + 123 + ); + + assert_eq!( + engine.eval::( + " + let foo = 123; + let x = 0; + try { throw 42; } catch(err) { return err; } + " + )?, + 42 + ); + #[cfg(not(feature = "unchecked"))] assert!(matches!( *engine