Skip to content

Commit a9a08b4

Browse files
committed
Note if name used differs from symbol in assign
It must have been imported; say so for clarity. Use current syntax in explanatory examples; don't use very fake code but prefer /* code */. Some LOC are made shorter for reading.
1 parent f55f055 commit a9a08b4

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,11 @@ class ReassignmentToVal(sym: Symbol, usage: Name, rhs: untpd.Tree)(using Context
15551555
val isSetter = usage.isSetterName && sym.info.firstParamTypes.nonEmpty
15561556
def msg(using Context) =
15571557
if isSetter then i"Bad assignment to setter should use $usage($rhs)"
1558-
else if sym.exists then i"Assignment to $sym"
1558+
else if sym.exists then
1559+
if sym.name != usage && usage != nme.NO_NAME then
1560+
i"Assignment to $sym imported as $usage"
1561+
else
1562+
i"Assignment to $sym"
15591563
else i"Bad assignment to $usage"
15601564
def explain(using Context) =
15611565
val name =
@@ -1577,7 +1581,7 @@ class ReassignmentToVal(sym: Symbol, usage: Name, rhs: untpd.Tree)(using Context
15771581
| ${hl("var")} $name ${hl("=")} ???
15781582
|However, it's more common to initialize a variable just once
15791583
|with a complex expression or even a block with many statements:
1580-
| ${hl("val")} $name ${hl("= if (condition) 1 else -1")}$addendum
1584+
| ${hl("val")} $name ${hl("= if condition then /* code */ else /* other code */")}$addendum
15811585
|"""
15821586

15831587
class TypeDoesNotTakeParameters(tpe: Type, params: List[untpd.Tree])(using Context)

compiler/src/dotty/tools/dotc/typer/Dynamic.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,17 @@ trait Dynamic {
131131
def typedDynamicAssign(tree: untpd.Assign, pt: Type)(using Context): Tree = {
132132
def typedDynamicAssign(qual: untpd.Tree, name: Name, selSpan: Span, targs: List[untpd.Tree]): Tree =
133133
typedApply(untpd.Apply(coreDynamic(qual, nme.updateDynamic, name, selSpan, targs), tree.rhs), pt)
134+
def reassignmentToVal(sym: Symbol, name: Name) =
135+
errorTree(tree, ReassignmentToVal(sym, name, untpd.EmptyTree))
134136
tree.lhs match {
135137
case sel @ Select(qual, name) if !isDynamicMethod(name) =>
136138
typedDynamicAssign(qual, name, sel.span, Nil)
137139
case TypeApply(sel @ Select(qual, name), targs) if !isDynamicMethod(name) =>
138140
typedDynamicAssign(qual, name, sel.span, targs)
141+
case lhs: NameTree =>
142+
reassignmentToVal(lhs.symbol, lhs.name)
139143
case lhs =>
140-
val name = lhs match { case nt: NameTree => nt.name case _ => nme.NO_NAME }
141-
errorTree(tree, ReassignmentToVal(lhs.symbol, name, untpd.EmptyTree))
144+
reassignmentToVal(lhs.symbol, nme.NO_NAME)
142145
}
143146
}
144147

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,8 +1430,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
14301430
def lhs1 = adapt(lhsCore, LhsProto, locked)
14311431

14321432
def reassignmentToVal =
1433-
val name = lhs match { case nt: NameTree => nt.name case _ => nme.NO_NAME }
1434-
report.error(ReassignmentToVal(lhs1.symbol `orElse` lhsCore.symbol, name, tree.rhs), tree.srcPos)
1433+
def reassignmentError() =
1434+
val name =
1435+
lhs match
1436+
case lhs: NameTree => lhs.name
1437+
case _ => nme.NO_NAME
1438+
val msg = ReassignmentToVal(lhs1.symbol `orElse` lhsCore.symbol, name, tree.rhs)
1439+
report.error(msg, tree.srcPos)
1440+
reassignmentError()
14351441
cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType)
14361442

14371443
def canAssign(sym: Symbol) =

tests/neg/i22671.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
-- [E052] Type Error: tests/neg/i22671.scala:20:4 ----------------------------------------------------------------------
3737
20 | y = 27 // error
3838
| ^^^^^^
39-
| Assignment to method x
39+
| Assignment to method x imported as y
4040
|
4141
| longer explanation available when compiling with `-explain`
4242
-- [E052] Type Error: tests/neg/i22671.scala:24:4 ----------------------------------------------------------------------
4343
24 | y = 27 // error
4444
| ^^^^^^
45-
| Assignment to value z
45+
| Assignment to value z imported as y
4646
|
4747
| longer explanation available when compiling with `-explain`
4848
-- [E052] Type Error: tests/neg/i22671.scala:28:4 ----------------------------------------------------------------------

tests/neg/i22671.explain.check

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| var w = ???
1111
| However, it's more common to initialize a variable just once
1212
| with a complex expression or even a block with many statements:
13-
| val w = if (condition) 1 else -1
13+
| val w = if condition then /* code */ else /* other code */
1414
| Assignment syntax can be used if there is a corresponding setter of the form:
1515
| def w_=(x: Int): Unit = ???
1616
--------------------------------------------------------------------------------------------------------------------
@@ -26,14 +26,14 @@
2626
| var x = ???
2727
| However, it's more common to initialize a variable just once
2828
| with a complex expression or even a block with many statements:
29-
| val x = if (condition) 1 else -1
29+
| val x = if condition then /* code */ else /* other code */
3030
| Assignment syntax can be used if there is a corresponding setter of the form:
3131
| def x_=(x: Int): Unit = ???
3232
--------------------------------------------------------------------------------------------------------------------
3333
-- [E052] Type Error: tests/neg/i22671.explain.scala:21:4 --------------------------------------------------------------
3434
21 | y = 27 // error overload renamed
3535
| ^^^^^^
36-
| Assignment to method x
36+
| Assignment to method x imported as y
3737
|--------------------------------------------------------------------------------------------------------------------
3838
| Explanation (enabled by `-explain`)
3939
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -42,14 +42,14 @@
4242
| var x = ???
4343
| However, it's more common to initialize a variable just once
4444
| with a complex expression or even a block with many statements:
45-
| val x = if (condition) 1 else -1
45+
| val x = if condition then /* code */ else /* other code */
4646
| Assignment syntax can be used if there is a corresponding setter of the form:
4747
| def x_=(x: Int): Unit = ???
4848
--------------------------------------------------------------------------------------------------------------------
4949
-- [E052] Type Error: tests/neg/i22671.explain.scala:25:4 --------------------------------------------------------------
5050
25 | y = 27 // error val renamed
5151
| ^^^^^^
52-
| Assignment to value z
52+
| Assignment to value z imported as y
5353
|--------------------------------------------------------------------------------------------------------------------
5454
| Explanation (enabled by `-explain`)
5555
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -58,7 +58,7 @@
5858
| var z = ???
5959
| However, it's more common to initialize a variable just once
6060
| with a complex expression or even a block with many statements:
61-
| val z = if (condition) 1 else -1
61+
| val z = if condition then /* code */ else /* other code */
6262
| Assignment syntax can be used if there is a corresponding setter of the form:
6363
| def z_=(x: Int): Unit = ???
6464
--------------------------------------------------------------------------------------------------------------------
@@ -74,7 +74,7 @@
7474
| var x = ???
7575
| However, it's more common to initialize a variable just once
7676
| with a complex expression or even a block with many statements:
77-
| val x = if (condition) 1 else -1
77+
| val x = if condition then /* code */ else /* other code */
7878
--------------------------------------------------------------------------------------------------------------------
7979
-- [E052] Type Error: tests/neg/i22671.explain.scala:32:6 --------------------------------------------------------------
8080
32 | t.t = t // error
@@ -88,7 +88,7 @@
8888
| var t = ???
8989
| However, it's more common to initialize a variable just once
9090
| with a complex expression or even a block with many statements:
91-
| val t = if (condition) 1 else -1
91+
| val t = if condition then /* code */ else /* other code */
9292
| Assignment syntax can be used if there is a corresponding setter of the form:
9393
| def t_=(x: Int): Unit = ???
9494
--------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)