-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
Attempts to fix issue #266 #317
base: master
Are you sure you want to change the base?
Changes from 2 commits
1e1e6ab
532bc4f
8be203a
546a877
e17eb7c
c2d812b
9ea9fb3
6a7536a
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 |
---|---|---|
|
@@ -2460,6 +2460,71 @@ class parser { | |
v.visit_exit_with_scope(); | ||
} | ||
|
||
bool is_side_effect_free(expression *ast) { | ||
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. Nit: Maybe it'd be better to invert the meaning of this function and call it
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch (ast->kind()) { | ||
case expression_kind::_class: | ||
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. Blocking: Class expressions might have side effects. For example: function f() { console.log("yo"); }
let C = class { [f()]() {} }; We don't have enough information in the AST to know, so let's be conservative and assume that side effects might happen. |
||
case expression_kind::_invalid: | ||
case expression_kind::_typeof: | ||
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. Blocking: typeof 42; // no side effect
typeof f(); // side effect |
||
case expression_kind::await: | ||
case expression_kind::import: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case expression_kind::literal: | ||
case expression_kind::new_target: | ||
case expression_kind::private_variable: | ||
case expression_kind::rw_unary_prefix: | ||
case expression_kind::rw_unary_suffix: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case expression_kind::spread: | ||
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. Blocking: |
||
case expression_kind::super: | ||
case expression_kind::unary_operator: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case expression_kind::variable: | ||
case expression_kind::yield_many: | ||
case expression_kind::yield_none: | ||
case expression_kind::yield_one: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
|
||
case expression_kind::dot: | ||
case expression_kind::index: | ||
case expression_kind::function: | ||
case expression_kind::named_function: | ||
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. Almost-blocking: |
||
return false; | ||
|
||
case expression_kind::_new: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case expression_kind::_template: | ||
case expression_kind::array: | ||
case expression_kind::binary_operator: | ||
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. Blocking: I think we should write a |
||
case expression_kind::call: | ||
case expression_kind::tagged_template_literal: | ||
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. Blocking: |
||
case expression_kind::trailing_comma: | ||
case expression_kind::arrow_function_with_expression: | ||
case expression_kind::arrow_function_with_statements: | ||
for (int i = 0; i < ast->child_count(); i++) { | ||
if (!this->is_side_effect_free(ast->child(i))) return false; | ||
} | ||
return true; | ||
|
||
case expression_kind::assignment: | ||
case expression_kind::compound_assignment: | ||
case expression_kind::conditional_assignment: | ||
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. Blocking: Assignments always have side effects. |
||
return this->is_side_effect_free(ast->child_0()) && | ||
this->is_side_effect_free(ast->child_1()); | ||
|
||
case expression_kind::conditional: | ||
return this->is_side_effect_free(ast->child_0()) && | ||
this->is_side_effect_free(ast->child_1()) && | ||
this->is_side_effect_free(ast->child_2()); | ||
|
||
case expression_kind::object: { | ||
for (int i = 0; i < ast->object_entry_count(); i++) { | ||
auto entry = ast->object_entry(i); | ||
if (entry.property.has_value()) { | ||
if (!this->is_side_effect_free(*entry.property)) return false; | ||
} | ||
} | ||
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. Blocking: Some object literal keys can have side effects. For example: function f() { console.log("yo"); }
let o = { [f()]: true }; |
||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
template <QLJS_PARSE_VISITOR Visitor> | ||
void parse_and_visit_if(Visitor &v) { | ||
QLJS_ASSERT(this->peek().type == token_type::kw_if); | ||
|
@@ -2515,8 +2580,42 @@ class parser { | |
} | ||
|
||
if (this->peek().type == token_type::kw_else) { | ||
source_code_span else_span = this->peek().span(); | ||
this->skip(); | ||
parse_and_visit_body(); | ||
|
||
switch (this->peek().type) { | ||
default: | ||
parse_and_visit_body(); | ||
break; | ||
|
||
case token_type::left_paren: | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expression *ast = this->parse_expression(precedence{}); | ||
this->visit_expression(ast, v, variable_context::rhs); | ||
|
||
bool is_invalidating_if = false; | ||
|
||
switch (ast->kind()) { | ||
default: | ||
break; | ||
|
||
// Any other kind? | ||
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 not. |
||
case expression_kind::arrow_function_with_expression: | ||
case expression_kind::arrow_function_with_statements: | ||
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. Perhaps we should make |
||
is_invalidating_if = true; | ||
break; | ||
} | ||
|
||
if (this->peek().type == token_type::left_curly) { | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!is_invalidating_if && this->is_side_effect_free(ast)) { | ||
this->error_reporter_->report( | ||
error_else_with_conditional_missing_if{ | ||
.else_token = else_span, | ||
}); | ||
} | ||
parse_and_visit_body(); | ||
} | ||
break; | ||
strager marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
|
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.
Nit: I'd put this function's implementation in parse.cpp.