Type checking in switch case condition.

This commit is contained in:
Stephen Chung 2022-04-19 21:45:11 +08:00
parent 770b2e04cc
commit 299d6ef308
3 changed files with 10 additions and 2 deletions

View File

@ -13,6 +13,7 @@ Enhancements
------------
* `Module::eval_ast_as_new_raw` is made public as a low-level API.
* Improper `switch` case condition syntax is now caught at parse time.
Version 1.6.1

View File

@ -122,7 +122,9 @@ impl fmt::Debug for OpAssignment<'_> {
}
}
/// A statements block with an optional condition.
/// A statements block with a condition.
///
/// The condition may simply be [`Expr::BoolConstant`] with `true` if there is actually no condition.
#[derive(Debug, Clone, Hash)]
pub struct ConditionalStmtBlock {
/// Condition.

View File

@ -1054,7 +1054,12 @@ impl Engine {
Some(self.parse_expr(input, state, lib, settings.level_up())?);
let condition = if match_token(input, Token::If).0 {
self.parse_expr(input, state, lib, settings.level_up())?
ensure_not_statement_expr(input, "a boolean")?;
let guard = self
.parse_expr(input, state, lib, settings.level_up())?
.ensure_bool_expr()?;
ensure_not_assignment(input)?;
guard
} else {
Expr::BoolConstant(true, Position::NONE)
};