From 299d6ef308ac5642b1dfc07247ad69b1a547fd6e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 Apr 2022 21:45:11 +0800 Subject: [PATCH] Type checking in switch case condition. --- CHANGELOG.md | 1 + src/ast/stmt.rs | 4 +++- src/parser.rs | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81b7cd2b..e5edd37d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index 3b73a91a..788ff368 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -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. diff --git a/src/parser.rs b/src/parser.rs index 33c315f8..6abb4586 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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) };