Skip to content

Commit

Permalink
Merge pull request #633 from schungx/v1.9.1
Browse files Browse the repository at this point in the history
v1.9.1
  • Loading branch information
schungx committed Aug 29, 2022
2 parents 00f84ef + 498d6f5 commit 186b6d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Rhai Release Notes
==================

Version 1.9.1
=============

This is a bug-fix version that fixes a bug.

Accessing properties in _Strict Variables Mode_ no longer generates a _variable not found_ error.


Version 1.9.0
=============

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "codegen"]

[package]
name = "rhai"
version = "1.9.0"
version = "1.9.1"
rust-version = "1.60.0"
edition = "2018"
resolver = "2"
Expand Down
30 changes: 13 additions & 17 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ impl Engine {
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FnLib,
is_property: bool,
settings: ParseSettings,
) -> ParseResult<Expr> {
#[cfg(not(feature = "unchecked"))]
Expand Down Expand Up @@ -1435,11 +1436,11 @@ impl Engine {
|crate::ast::Ident { name, pos }| {
let (index, is_func) = state.access_var(name, lib, *pos);

if settings.options.contains(LangOptions::STRICT_VAR)
&& !settings.in_closure
if !is_func
&& index.is_none()
&& !settings.in_closure
&& settings.options.contains(LangOptions::STRICT_VAR)
&& !state.scope.contains(name)
&& !is_func
{
// If the parent scope is not inside another capturing closure
// then we can conclude that the captured variable doesn't exist.
Expand Down Expand Up @@ -1572,20 +1573,18 @@ impl Engine {
// Once the identifier consumed we must enable next variables capturing
state.allow_capture = true;
}
Expr::Variable(
(None, ns, 0, state.get_interned_string(s)).into(),
None,
settings.pos,
)
let name = state.get_interned_string(s);
Expr::Variable((None, ns, 0, name).into(), None, settings.pos)
}
// Normal variable access
_ => {
let (index, is_func) = state.access_var(&s, lib, settings.pos);

if settings.options.contains(LangOptions::STRICT_VAR)
if !is_property
&& !is_func
&& index.is_none()
&& settings.options.contains(LangOptions::STRICT_VAR)
&& !state.scope.contains(&s)
&& !is_func
{
return Err(
PERR::VariableUndefined(s.to_string()).into_err(settings.pos)
Expand All @@ -1599,11 +1598,8 @@ impl Engine {
None
}
});
Expr::Variable(
(index, ns, 0, state.get_interned_string(s)).into(),
short_index,
settings.pos,
)
let name = state.get_interned_string(s);
Expr::Variable((index, ns, 0, name).into(), short_index, settings.pos)
}
}
}
Expand Down Expand Up @@ -1790,7 +1786,7 @@ impl Engine {
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)),
}

let rhs = self.parse_primary(input, state, lib, settings.level_up())?;
let rhs = self.parse_primary(input, state, lib, true, settings.level_up())?;
let op_flags = match op {
Token::Period => ASTFlags::NONE,
Token::Elvis => ASTFlags::NEGATED,
Expand Down Expand Up @@ -1960,7 +1956,7 @@ impl Engine {
// <EOF>
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
// All other tokens
_ => self.parse_primary(input, state, lib, settings.level_up()),
_ => self.parse_primary(input, state, lib, false, settings.level_up()),
}
}

Expand Down
11 changes: 7 additions & 4 deletions tests/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ fn test_options_allow() -> Result<(), Box<EvalAltResult>> {
fn test_options_strict_var() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();

let mut scope = Scope::new();
scope.push("x", 42 as INT);
scope.push_constant("y", 0 as INT);

engine.compile("let x = if y { z } else { w };")?;

#[cfg(not(feature = "no_function"))]
Expand All @@ -78,9 +74,16 @@ fn test_options_strict_var() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_function"))]
engine.compile("let f = |y| x * y;")?;

let mut scope = Scope::new();
scope.push("x", 42 as INT);
scope.push_constant("y", 0 as INT);

engine.set_strict_variables(true);

assert!(engine.compile("let x = if y { z } else { w };").is_err());

engine.compile_with_scope(&mut scope, "if x.abs() { y } else { x + y.len };")?;

engine.compile("let y = 42; let x = y;")?;

assert_eq!(
Expand Down

0 comments on commit 186b6d0

Please sign in to comment.