Allow variable to overwrite constant when shadowing.

This commit is contained in:
Stephen Chung 2022-02-18 19:14:42 +08:00
parent 78b5c9fd4e
commit 67a6638818
2 changed files with 15 additions and 5 deletions

View File

@ -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)
}

View File

@ -32,14 +32,27 @@ fn test_var_scope() -> Result<(), Box<EvalAltResult>> {
assert_eq!(scope.get_value::<INT>("x").unwrap(), 123);
assert_eq!(scope.get_value::<INT>("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::<INT>("x").unwrap(), 42);
assert_eq!(scope.get_value::<INT>("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::<INT>("x").unwrap(), 123);
assert_eq!(scope.get_value::<INT>("y").unwrap(), 999);
assert!(scope.is_constant("x").unwrap());
assert!(!scope.is_constant("y").unwrap());
scope.clear();
engine.run_with_scope(