Skip to content

Commit 24c88b2

Browse files
Backport "fix: allow postfix setters under language.postfixOps" to 3.7.4 (#24032)
Backports #23775 to the 3.7.4. PR submitted by the release tooling. [skip ci]
2 parents 9eaa790 + fafb5c1 commit 24c88b2

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,7 @@ object Parsers {
23702370
* | ForExpr
23712371
* | [SimpleExpr `.'] id `=' Expr
23722372
* | PrefixOperator SimpleExpr `=' Expr
2373+
* | InfixExpr id [nl] `=' Expr -- only if language.postfixOps is enabled
23732374
* | SimpleExpr1 ArgumentExprs `=' Expr
23742375
* | PostfixExpr [Ascription]
23752376
* | ‘inline’ InfixExpr MatchClause
@@ -2525,7 +2526,7 @@ object Parsers {
25252526
def expr1Rest(t: Tree, location: Location): Tree =
25262527
if in.token == EQUALS then
25272528
t match
2528-
case Ident(_) | Select(_, _) | Apply(_, _) | PrefixOp(_, _) =>
2529+
case Ident(_) | Select(_, _) | Apply(_, _) | PrefixOp(_, _) | PostfixOp(_, _) =>
25292530
atSpan(startOffset(t), in.skipToken()) {
25302531
val loc = if location.inArgs then location else Location.ElseWhere
25312532
Assign(t, subPart(() => expr(loc)))

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
261261
| ForExpr
262262
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
263263
| PrefixOperator SimpleExpr ‘=’ Expr Assign(expr, expr)
264+
| InfixExpr id [nl] `=' Expr Assign(expr, expr) -- only if language.postfixOps is enabled
264265
| SimpleExpr ArgumentExprs ‘=’ Expr Assign(expr, expr)
265266
| PostfixExpr [Ascription]
266267
| ‘inline’ InfixExpr MatchClause

docs/_docs/reference/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
246246
| ForExpr
247247
| [SimpleExpr ‘.’] id ‘=’ Expr
248248
| PrefixOperator SimpleExpr ‘=’ Expr
249+
| InfixExpr id [nl] `=' Expr -- only if language.postfixOps is enabled
249250
| SimpleExpr ArgumentExprs ‘=’ Expr
250251
| PostfixExpr [Ascription]
251252
| ‘inline’ InfixExpr MatchClause

tests/pos/vec-access-syntax.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.language.postfixOps
2+
3+
class Vector(values: Int*) {
4+
val data = values.toArray
5+
class Getter(i: Int) {
6+
def `>_=`(x: Int) =
7+
data(i) = x
8+
def > : Int =
9+
data(i)
10+
}
11+
def < (i:Int) = new Getter(i)
12+
override def toString = data.mkString("<", ", ", ">")
13+
}
14+
15+
object Test {
16+
def main(args: Array[String]): Unit = {
17+
val v = new Vector(1, 2, 3)
18+
println(v) // prints <1, 2, 3>
19+
v<1> = 10 // assign 10 to element at index 1
20+
println(v) // prints <1, 10, 3>
21+
println(v<1>) // prints: value at 1 is 10
22+
}
23+
}

0 commit comments

Comments
 (0)