From 67a663881861370e8d60f1eb975caa7a86741564 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 18 Feb 2022 19:14:42 +0800 Subject: [PATCH] Allow variable to overwrite constant when shadowing. --- src/parser.rs | 5 +---- tests/var_scope.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 9365a817..6c39bf57 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2663,13 +2663,10 @@ fn parse_let( AST_OPTION_NONE }; - let existing = state.stack.get_index(&name).and_then(|(n, a)| { + let existing = state.stack.get_index(&name).and_then(|(n, ..)| { if n < state.block_stack_len { // Defined in parent block None - } else if a == AccessMode::ReadOnly && access != AccessMode::ReadOnly { - // Overwrite constant - None } else { Some(n) } diff --git a/tests/var_scope.rs b/tests/var_scope.rs index 813dfe5a..d7998379 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -32,14 +32,27 @@ fn test_var_scope() -> Result<(), Box> { assert_eq!(scope.get_value::("x").unwrap(), 123); assert_eq!(scope.get_value::("y").unwrap(), 999); + scope.clear(); + engine.run_with_scope( + &mut scope, + "const x = 3; let y = 0; let x = 42; let y = 999;", + )?; + assert_eq!(scope.len(), 2); + assert_eq!(scope.get_value::("x").unwrap(), 42); + assert_eq!(scope.get_value::("y").unwrap(), 999); + assert!(!scope.is_constant("x").unwrap()); + assert!(!scope.is_constant("y").unwrap()); + scope.clear(); engine.run_with_scope( &mut scope, "const x = 3; let y = 0; let x = 42; let y = 999; const x = 123;", )?; - assert_eq!(scope.len(), 3); + assert_eq!(scope.len(), 2); assert_eq!(scope.get_value::("x").unwrap(), 123); assert_eq!(scope.get_value::("y").unwrap(), 999); + assert!(scope.is_constant("x").unwrap()); + assert!(!scope.is_constant("y").unwrap()); scope.clear(); engine.run_with_scope(