Skip to content

Commit 9b310bc

Browse files
committed
Merge pull request scala#2583 from som-snytt/issue/7473-pointless-for-crash
SI-7473 Bad for expr crashes postfix
2 parents 1900907 + 5b54681 commit 9b310bc

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/compiler/scala/tools/nsc/ast/parser/Parsers.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,9 @@ self =>
14741474
* }}}
14751475
*/
14761476
def postfixExpr(): Tree = {
1477-
val base = opstack
1478-
var top = prefixExpr()
1477+
val start = in.offset
1478+
val base = opstack
1479+
var top = prefixExpr()
14791480

14801481
while (isIdent) {
14811482
top = reduceStack(isExpr = true, base, top, precedence(in.name), leftAssoc = treeInfo.isLeftAssoc(in.name))
@@ -1493,9 +1494,7 @@ self =>
14931494
val topinfo = opstack.head
14941495
opstack = opstack.tail
14951496
val od = stripParens(reduceStack(isExpr = true, base, topinfo.operand, 0, leftAssoc = true))
1496-
return atPos(od.pos.startOrPoint, topinfo.offset) {
1497-
new PostfixSelect(od, topinfo.operator.encode)
1498-
}
1497+
return makePostfixSelect(start, topinfo.offset, od, topinfo.operator)
14991498
}
15001499
}
15011500
reduceStack(isExpr = true, base, top, 0, leftAssoc = true)

src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ abstract class TreeBuilder {
245245
Assign(lhs, rhs)
246246
}
247247

248+
/** Tree for `od op`, start is start0 if od.pos is borked. */
249+
def makePostfixSelect(start0: Int, end: Int, od: Tree, op: Name): Tree = {
250+
val start = if (od.pos.isDefined) od.pos.startOrPoint else start0
251+
atPos(r2p(start, end, end)) { new PostfixSelect(od, op.encode) }
252+
}
253+
248254
/** A type tree corresponding to (possibly unary) intersection type */
249255
def makeIntersectionTypeTree(tps: List[Tree]): Tree =
250256
if (tps.tail.isEmpty) tps.head

test/files/neg/t7473.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
t7473.scala:6: error: '<-' expected but '=' found.
2+
(for (x = Option(i); if x == j) yield 42) toList
3+
^
4+
t7473.scala:6: error: illegal start of simple expression
5+
(for (x = Option(i); if x == j) yield 42) toList
6+
^
7+
two errors found

test/files/neg/t7473.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
object Foo {
3+
val i,j = 3
4+
//for (x = Option(i); if x == j) yield 42 //t7473.scala:4: error: '<-' expected but '=' found.
5+
// evil postfix!
6+
(for (x = Option(i); if x == j) yield 42) toList
7+
}

0 commit comments

Comments
 (0)