Fix #5013: return statement as an expression (#5014)

* fix #5013

* disallow statement in the expression
This commit is contained in:
zdenko 2018-03-18 07:08:43 +01:00 committed by Geoffrey Booth
parent ce66a499de
commit 001f97ac39
4 changed files with 36 additions and 45 deletions

View File

@ -1267,15 +1267,6 @@
if (!props && base instanceof Value) {
return base;
}
if (base instanceof Parens && base.contains(function(n) {
return n instanceof StatementLiteral;
})) {
// When `Parens` block includes a `StatementLiteral` (e.g. `(b; break) for a in arr`),
// it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
// pure statement (`break`) can't be used in an expression.
// For this reasons, we return `Block` instead of `Parens`.
return base.unwrap();
}
this.base = base;
this.properties = props || [];
if (tag) {

View File

@ -863,11 +863,6 @@ exports.Value = class Value extends Base
constructor: (base, props, tag, isDefaultValue = no) ->
super()
return base if not props and base instanceof Value
# When `Parens` block includes a `StatementLiteral` (e.g. `(b; break) for a in arr`),
# it won't compile since `Parens` (`(b; break)`) is compiled as `Value` and
# pure statement (`break`) can't be used in an expression.
# For this reasons, we return `Block` instead of `Parens`.
return base.unwrap() if base instanceof Parens and base.contains (n) -> n instanceof StatementLiteral
@base = base
@properties = props or []
@[tag] = yes if tag

View File

@ -1112,37 +1112,6 @@ test "#3921: `switch`", ->
else "none"
eq "five", c
# Issue #3441: Parentheses wrapping expression throw invalid error in `then` clause
test "#3441: `StatementLiteral` in parentheses", ->
i = 0
r1 = ((i++; break) while i < 10)
arrayEq r1, []
i = 0
r2 = ((i++; continue) while i < 10)
arrayEq r2, []
i = 0
r4 = while i < 10 then (i++; break)
arrayEq r4, []
i = 0
r5 = while i < 10 then (i++; continue)
arrayEq r5, []
arr = [0..9]
r6 = ((a; break) for a in arr)
arrayEq r6, []
r7 = ((a; continue) for a in arr)
arrayEq r7, []
r8 = for a in arr then (a; break)
arrayEq r8, []
r9 = for a in arr then (a; continue)
arrayEq r9, []
# Issue #3909: backslash to break line in `for` loops throw syntax error
test "#3909: backslash `for own ... of`", ->

View File

@ -962,6 +962,42 @@ test "#4097: `yield return` as an expression", ->
^^^^^^^^^^^^
'''
test "#5013: `await return` as an expression", ->
assertErrorFormat '''
-> (await return)
''', '''
[stdin]:1:5: error: cannot use a pure statement in an expression
-> (await return)
^^^^^^^^^^^^
'''
test "#5013: `return` as an expression", ->
assertErrorFormat '''
-> (return)
''', '''
[stdin]:1:5: error: cannot use a pure statement in an expression
-> (return)
^^^^^^
'''
test "#5013: `break` as an expression", ->
assertErrorFormat '''
(b = 1; break) for b in a
''', '''
[stdin]:1:9: error: cannot use a pure statement in an expression
(b = 1; break) for b in a
^^^^^
'''
test "#5013: `continue` as an expression", ->
assertErrorFormat '''
(b = 1; continue) for b in a
''', '''
[stdin]:1:9: error: cannot use a pure statement in an expression
(b = 1; continue) for b in a
^^^^^^^^
'''
test "`&&=` and `||=` with a space in-between", ->
assertErrorFormat '''
a = 0