Skip to content

Commit 1c5364c

Browse files
committed
RHS of var def is an expr
Leading underscore short-circuited `var f: Int => Int = _ + 1`. Also remove an extraneous `toList`. The test for default var value should narrowly accept only the correct syntax. Ports scala/bug#11437
1 parent d37a543 commit 1c5364c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,14 +3146,20 @@ object Parsers {
31463146
}
31473147
else emptyType
31483148
val rhs =
3149-
if (tpt.isEmpty || in.token == EQUALS)
3149+
if tpt.isEmpty || in.token == EQUALS then
31503150
endMarkerScope(first) {
31513151
accept(EQUALS)
3152-
if (in.token == USCORE && !tpt.isEmpty && mods.is(Mutable) &&
3153-
(lhs.toList forall (_.isInstanceOf[Ident])))
3154-
wildcardIdent()
3152+
val rhs0 = subExpr()
3153+
val defaultOK = !tpt.isEmpty && mods.is(Mutable) && lhs.forall(_.isInstanceOf[Ident])
3154+
def isDefaultSyntax(t: Tree) = t match {
3155+
case Ident(name) => placeholderParams.nonEmpty && name == placeholderParams.head.name
3156+
case _ => false
3157+
}
3158+
if defaultOK && isDefaultSyntax(rhs0) then
3159+
placeholderParams = placeholderParams.tail
3160+
atSpan(rhs0.span) { Ident(nme.WILDCARD) }
31553161
else
3156-
subExpr()
3162+
rhs0
31573163
}
31583164
else EmptyTree
31593165
lhs match {

tests/neg/t11437.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
class Regress {
3+
var v: Int = _
4+
def f = 42
5+
var w: Int = (_) // error: not default value syntax
6+
}

tests/pos/t11437.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
trait T {
3+
val adder0: Int => Int = _ + 3 // Works fine
4+
var adder1: Int => Int = (_ + 3) // Works fine
5+
var adder2: Int => Int = _ + 3 // was: Error
6+
}
7+
class Regress {
8+
var v: Int = _
9+
def f = 42
10+
//var w: Int = (_) //Unbound placeholder parameter; incorrect use of _
11+
}

0 commit comments

Comments
 (0)