Closed ThiNei2l closed 1 year ago
It is parsing the expression as:
if 1==1 {
"foo"
} else {
"foo"
}
+"bar"
Obviously that is syntactically correct (and valid as an if
parses first as a statement...)
However, parsing it as an expression will cause similar problems:
fn foo() {
if hello() {
do_work();
} else {
goof_off();
}
-1 // would need (-1) to parse correctly
}
This case is similar to https://github.com/rhaiscript/rhai/issues/628
I'm open to ideas on whether it is better to parse first as a statement or as an expression...
Rust simply won't parse it as an expression unless it is enclosed by parentheses:
// syntax error without ( ... )
(if 1==1 {"foo"} else {"foo"}) + "bar"
FYI, just found this in the Rust compiler source code, which highlights similar problems, but this time with a |
which can be interpreted both as an OR operator or the first half of a closure.
if true {...} else {...}
|x| 5
/// shouldn't be parsed as:
(if true {...} else {...} | x) | 5
Closing this for now. I'm going to leave it as is, but add new material to the Book to alert users regarding this.
Hi there,
I am not really sure what is happening here or if this is a bug, but I thought I would report it. Consider this pretty nonsensical program:
The output is
The first snippet behaves as expected: we concatenate the result of the if-expression and "bar". In the second snippet the same expression ceases to work. If I understand the message correctly rhai is now looking for a unary
+
?Is this a bug or am I missing something?