From f903eda8ab3ef6e3dfbaa0c6b274bad2fb35596f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 17 Oct 2020 18:18:29 +0800 Subject: [PATCH] Catch Fn and eval in method call at parse time. --- RELEASES.md | 4 ++++ src/fn_call.rs | 31 ++++++++++--------------------- src/parser.rs | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 715003ed..9a7f9d08 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -11,6 +11,10 @@ Breaking changes * `EvalAltResult::ErrorReadingScriptFile` is removed in favor of the new `EvalAltResult::ErrorSystem`. * `EvalAltResult::ErrorLoopBreak` is renamed to `EvalAltResult::LoopBreak`. +Enhancements + +* Calling `eval` or `Fn` in method-call style, which is an error, is now caught during parsing. + Version 0.19.2 ============== diff --git a/src/fn_call.rs b/src/fn_call.rs index 5785f1d2..43cbb9bb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -524,27 +524,16 @@ impl Engine { )) } - // Fn - KEYWORD_FN_PTR - if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) => - { - EvalAltResult::ErrorRuntime( - "'Fn' should not be called in method style. Try Fn(...);".into(), - Position::none(), - ) - .into() - } - - // eval - reaching this point it must be a method-style call - KEYWORD_EVAL - if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) => - { - EvalAltResult::ErrorRuntime( - "'eval' should not be called in method style. Try eval(...);".into(), - Position::none(), - ) - .into() - } + // Fn/eval - reaching this point it must be a method-style call, mostly like redirected + // by a function pointer so it isn't caught at parse time. + KEYWORD_FN_PTR | KEYWORD_EVAL if args.len() == 1 => EvalAltResult::ErrorRuntime( + format!( + "'{}' should not be called in method style. Try {}(...);", + fn_name, fn_name + ), + Position::none(), + ) + .into(), // Script-like function found #[cfg(not(feature = "no_function"))] diff --git a/src/parser.rs b/src/parser.rs index 55d97082..a26ec543 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,7 +1,9 @@ //! Main module defining the lexer and parser. use crate::any::{Dynamic, Union}; -use crate::engine::{Engine, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT}; +use crate::engine::{ + Engine, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT, +}; use crate::error::{LexError, ParseError, ParseErrorType}; use crate::fn_native::{FnPtr, Shared}; use crate::module::{Module, ModuleRef}; @@ -2318,6 +2320,17 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result + { + return Err(PERR::BadInput(format!( + "'{}' should not be called in method style. Try {}(...);", + (x.0).0, + (x.0).0 + )) + .into_err((x.0).3)); + } // lhs.func!(...) (_, Expr::FnCall(x)) if (x.0).2 => { return Err(PERR::MalformedCapture(