Forbid floating-point switch cases after range case.

This commit is contained in:
Stephen Chung 2023-02-13 08:58:59 +08:00
parent 10089c5cb0
commit 7da20dd090
3 changed files with 31 additions and 17 deletions

View File

@ -14,7 +14,7 @@ use crate::tokenizer::{
is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream, is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream,
TokenizerControl, TokenizerControl,
}; };
use crate::types::dynamic::AccessMode; use crate::types::dynamic::{AccessMode, Union};
use crate::types::StringsInterner; use crate::types::StringsInterner;
use crate::{ use crate::{
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, ExclusiveRange, FnArgsVec, calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, ExclusiveRange, FnArgsVec,
@ -1243,9 +1243,20 @@ impl Engine {
continue; continue;
} }
if value.is_int() && !ranges.is_empty() { if !ranges.is_empty() {
let forbidden = match value {
Dynamic(Union::Int(..)) => true,
#[cfg(not(feature = "no_float"))]
Dynamic(Union::Float(..)) => true,
#[cfg(feature = "decimal")]
Dynamic(Union::Decimal(..)) => true,
_ => false,
};
if forbidden {
return Err(PERR::WrongSwitchIntegerCase.into_err(expr.start_position())); return Err(PERR::WrongSwitchIntegerCase.into_err(expr.start_position()));
} }
}
let hasher = &mut get_hasher(); let hasher = &mut get_hasher();
value.hash(hasher); value.hash(hasher);

View File

@ -105,7 +105,7 @@ pub enum ParseErrorType {
DuplicatedSwitchCase, DuplicatedSwitchCase,
/// A variable name is duplicated. Wrapped value is the variable name. /// A variable name is duplicated. Wrapped value is the variable name.
DuplicatedVariable(String), DuplicatedVariable(String),
/// An integer case of a `switch` statement is in an appropriate place. /// A numeric case of a `switch` statement is in an appropriate place.
WrongSwitchIntegerCase, WrongSwitchIntegerCase,
/// The default case of a `switch` statement is in an appropriate place. /// The default case of a `switch` statement is in an appropriate place.
WrongSwitchDefaultCase, WrongSwitchDefaultCase,
@ -236,7 +236,7 @@ impl fmt::Display for ParseErrorType {
Self::Reserved(s) if is_valid_identifier(s.as_str()) => write!(f, "'{s}' is a reserved keyword"), Self::Reserved(s) if is_valid_identifier(s.as_str()) => write!(f, "'{s}' is a reserved keyword"),
Self::Reserved(s) => write!(f, "'{s}' is a reserved symbol"), Self::Reserved(s) => write!(f, "'{s}' is a reserved symbol"),
Self::UnexpectedEOF => f.write_str("Script is incomplete"), Self::UnexpectedEOF => f.write_str("Script is incomplete"),
Self::WrongSwitchIntegerCase => f.write_str("Integer switch case cannot follow a range case"), Self::WrongSwitchIntegerCase => f.write_str("Numeric switch case cannot follow a range case"),
Self::WrongSwitchDefaultCase => f.write_str("Default switch case must be the last"), Self::WrongSwitchDefaultCase => f.write_str("Default switch case must be the last"),
Self::WrongSwitchCaseCondition => f.write_str("This switch case cannot have a condition"), Self::WrongSwitchCaseCondition => f.write_str("This switch case cannot have a condition"),
Self::PropertyExpected => f.write_str("Expecting name of a property"), Self::PropertyExpected => f.write_str("Expecting name of a property"),

View File

@ -10,9 +10,7 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
engine.eval::<char>("switch 2 { 1 => (), 2 => 'a', 42 => true }")?, engine.eval::<char>("switch 2 { 1 => (), 2 => 'a', 42 => true }")?,
'a' 'a'
); );
engine engine.run("switch 3 { 1 => (), 2 => 'a', 42 => true }")?;
.run("switch 3 { 1 => (), 2 => 'a', 42 => true }")
.unwrap();
assert_eq!( assert_eq!(
engine.eval::<INT>("switch 3 { 1 => (), 2 => 'a', 42 => true, _ => 123 }")?, engine.eval::<INT>("switch 3 { 1 => (), 2 => 'a', 42 => true, _ => 123 }")?,
123 123
@ -31,15 +29,13 @@ fn test_switch() -> Result<(), Box<EvalAltResult>> {
)?, )?,
'a' 'a'
); );
assert!(engine assert!(
.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', 42 => true }") engine.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', 42 => true }")?
.unwrap()); );
assert!(engine assert!(
.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', _ => true }") engine.eval_with_scope::<bool>(&mut scope, "switch x { 1 => (), 2 => 'a', _ => true }")?
.unwrap()); );
let _: () = engine let _: () = engine.eval_with_scope::<()>(&mut scope, "switch x { 1 => 123, 2 => 'a' }")?;
.eval_with_scope::<()>(&mut scope, "switch x { 1 => 123, 2 => 'a' }")
.unwrap();
assert_eq!( assert_eq!(
engine.eval_with_scope::<INT>( engine.eval_with_scope::<INT>(
@ -276,6 +272,13 @@ fn test_switch_ranges() -> Result<(), Box<EvalAltResult>> {
).expect_err("should error").err_type(), ).expect_err("should error").err_type(),
ParseErrorType::WrongSwitchIntegerCase ParseErrorType::WrongSwitchIntegerCase
)); ));
#[cfg(not(feature = "no_float"))]
assert!(matches!(
engine.compile(
"switch x { 10..20 => (), 20..=42 => 'a', 25..45 => 'z', 42.0 => 'x', 30..100 => true }"
).expect_err("should error").err_type(),
ParseErrorType::WrongSwitchIntegerCase
));
assert_eq!( assert_eq!(
engine.eval_with_scope::<char>( engine.eval_with_scope::<char>(
&mut scope, &mut scope,