-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Validate if
statements
#1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
830f269
3a63c91
13d7f71
b28428c
c2c9d7b
38f4c83
227c688
95e197d
88a6d4c
55499f2
161c6de
a6df030
041c805
7d05787
8fde423
1d45a86
de73110
b00778b
89ef326
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1402,7 +1402,7 @@ fn for_loop_conditions_are_numerical() { | |
|
||
FOR i := 100000 TO x BY y DO | ||
END_FOR | ||
|
||
END_PROGRAM | ||
", | ||
); | ||
|
@@ -1436,7 +1436,7 @@ fn for_loop_conditions_are_real_and_trigger_error() { | |
|
||
FOR i := 10.0 TO x BY y DO | ||
END_FOR | ||
|
||
END_PROGRAM | ||
", | ||
); | ||
|
@@ -1468,3 +1468,57 @@ fn for_loop_conditions_are_real_and_trigger_error() { | |
|
||
"###); | ||
} | ||
|
||
#[test] | ||
fn if_statement_triggers_error_if_condition_is_not_boolean() { | ||
let diagnostic = parse_and_validate_buffered( | ||
" | ||
FUNCTION main | ||
VAR | ||
x : BOOL; | ||
y : DINT; | ||
z : STRING; | ||
END_VAR | ||
|
||
IF x THEN | ||
ELSIF y THEN | ||
ELSIF z THEN | ||
ELSIF 0 THEN | ||
ELSIF 1 THEN | ||
|
||
// These should be Ok | ||
ELSIF (0 < 1) THEN | ||
ELSIF (y < 0) THEN | ||
ELSIF ((1 = 2) = 3) THEN | ||
END_IF | ||
END_FUNCTION | ||
", | ||
); | ||
|
||
assert_snapshot!(diagnostic, @r###" | ||
error: Expected a boolean, got `DINT` | ||
┌─ <internal>:10:21 | ||
│ | ||
10 │ ELSIF y THEN | ||
│ ^ Expected a boolean, got `DINT` | ||
|
||
error: Expected a boolean, got `STRING` | ||
┌─ <internal>:11:21 | ||
│ | ||
11 │ ELSIF z THEN | ||
│ ^ Expected a boolean, got `STRING` | ||
|
||
error: Expected a boolean, got `DINT` | ||
┌─ <internal>:12:21 | ||
│ | ||
12 │ ELSIF 0 THEN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think IF 1 and IF 0 are fine.. not because i like them but because 0 and 1 are common I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially also wanted to support them but I thought if someone is using 0 or 1 they could also just use FALSE or TRUE instead. But also I just replicated what Rust does, though comparing with C makes more sense generally speaking. Anyhow, do we proceed with supporting 0 and 1s here? |
||
│ ^ Expected a boolean, got `DINT` | ||
|
||
error: Expected a boolean, got `DINT` | ||
┌─ <internal>:13:21 | ||
│ | ||
13 │ ELSIF 1 THEN | ||
│ ^ Expected a boolean, got `DINT` | ||
|
||
"###); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that this is wrong, but i'm interested to see how often people also use integers here the C way..
I would just accept this for now and and we discuss later if it might need to become a warning. In which case it will need its own error code btw.
Also note to self, when merging the diagnostics make sure this becomes E094