Skip to content

Commit 4968ee6

Browse files
som-snyttjan-pieterSethTisueiusildraajafri2001
committed
Use other tree for actual symbol of Assign
Co-authored-by: Jan-Pieter van den Heuvel <jan-pieter@users.noreply.github.com> Co-authored-by: Seth Tisue <seth@tisue.net> Co-authored-by: Lucas Nouguier <iusildra@users.noreply.github.com> Co-authored-by: Prince <ajafri2001@users.noreply.github.com>
1 parent b325681 commit 4968ee6

File tree

5 files changed

+123
-6
lines changed

5 files changed

+123
-6
lines changed

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import core.*
88
import Texts.*, Types.*, Flags.*, Symbols.*, Contexts.*
99
import Decorators.*
1010
import reporting.Message
11-
import util.{DiffUtil, SimpleIdentitySet}
11+
import util.{Chars, DiffUtil, SimpleIdentitySet}
1212
import Highlighting.*
1313

1414
object Formatting {
@@ -184,7 +184,8 @@ object Formatting {
184184
}
185185

186186
def assemble(args: Seq[Shown])(using Context): String = {
187-
def isLineBreak(c: Char) = c == '\n' || c == '\f' // compatible with StringLike#isLineBreak
187+
// compatible with CharArrayReader (not StringOps)
188+
inline def isLineBreak(c: Char) = c == Chars.LF || c == Chars.FF
188189
def stripTrailingPart(s: String) = {
189190
val (pre, post) = s.span(c => !isLineBreak(c))
190191
pre ++ post.stripMargin

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ trait Dynamic {
136136
typedDynamicAssign(qual, name, sel.span, Nil)
137137
case TypeApply(sel @ Select(qual, name), targs) if !isDynamicMethod(name) =>
138138
typedDynamicAssign(qual, name, sel.span, targs)
139-
case _ =>
140-
errorTree(tree, ReassignmentToVal(tree.lhs.symbol.name, pt))
139+
case lhs =>
140+
val name = lhs match
141+
case nt: NameTree => nt.name
142+
case _ => lhs.symbol.name
143+
errorTree(tree, ReassignmentToVal(name, pt))
141144
}
142145
}
143146

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
14291429
def lhs1 = adapt(lhsCore, LhsProto, locked)
14301430

14311431
def reassignmentToVal =
1432-
report.error(ReassignmentToVal(lhsCore.symbol.name, pt), tree.srcPos)
1432+
val name = lhs match
1433+
case nt: NameTree => nt.name
1434+
case _ => lhs1.symbol.orElse(lhsCore.symbol).name
1435+
report.error(ReassignmentToVal(name, pt), tree.srcPos)
14331436
cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType)
14341437

14351438
def canAssign(sym: Symbol) =
@@ -1518,7 +1521,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15181521
typedDynamicAssign(tree, pt)
15191522
case tpe =>
15201523
reassignmentToVal
1521-
}
1524+
}
15221525
}
15231526

15241527
def typedBlockStats(stats: List[untpd.Tree])(using Context): (List[tpd.Tree], Context) =

tests/neg/i22671.check

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i22671.scala:41:22 ------------------------------------------------------------
2+
41 | names_times(fields(0)) += fields(1).toLong // error
3+
| ^^^^^^^^^
4+
| Found: Char
5+
| Required: String
6+
|
7+
| longer explanation available when compiling with `-explain`
8+
-- [E008] Not Found Error: tests/neg/i22671.scala:45:6 -----------------------------------------------------------------
9+
45 | x() += "42" // error
10+
| ^^^^^^
11+
| value += is not a member of Int - did you mean Int.!=? or perhaps Int.<=?
12+
-- [E052] Type Error: tests/neg/i22671.scala:49:6 ----------------------------------------------------------------------
13+
49 | c = 42 // error
14+
| ^^^^^^
15+
| Reassignment to val c
16+
|
17+
| longer explanation available when compiling with `-explain`
18+
-- [E052] Type Error: tests/neg/i22671.scala:9:6 -----------------------------------------------------------------------
19+
9 | X.w = 27 // error
20+
| ^^^^^^^^
21+
| Reassignment to val w
22+
|
23+
| longer explanation available when compiling with `-explain`
24+
-- [E052] Type Error: tests/neg/i22671.scala:12:6 ----------------------------------------------------------------------
25+
12 | X.x = 27 // error
26+
| ^^^^^^^^
27+
| Reassignment to val x
28+
|
29+
| longer explanation available when compiling with `-explain`
30+
-- [E052] Type Error: tests/neg/i22671.scala:16:4 ----------------------------------------------------------------------
31+
16 | x = 27 // error
32+
| ^^^^^^
33+
| Reassignment to val x
34+
|
35+
| longer explanation available when compiling with `-explain`
36+
-- [E052] Type Error: tests/neg/i22671.scala:20:4 ----------------------------------------------------------------------
37+
20 | y = 27 // error
38+
| ^^^^^^
39+
| Reassignment to val y
40+
|
41+
| longer explanation available when compiling with `-explain`
42+
-- [E052] Type Error: tests/neg/i22671.scala:24:4 ----------------------------------------------------------------------
43+
24 | y = 27 // error
44+
| ^^^^^^
45+
| Reassignment to val y
46+
|
47+
| longer explanation available when compiling with `-explain`
48+
-- [E052] Type Error: tests/neg/i22671.scala:28:4 ----------------------------------------------------------------------
49+
28 | x = 27 // error
50+
| ^^^^^^
51+
| Reassignment to val x
52+
|
53+
| longer explanation available when compiling with `-explain`
54+
-- [E008] Not Found Error: tests/neg/i22671.scala:31:6 -----------------------------------------------------------------
55+
31 | X.x += 27 // error
56+
| ^^^^^^
57+
| value += is not a member of Int - did you mean Int.!=? or perhaps Int.<=?
58+
-- [E008] Not Found Error: tests/neg/i22671.scala:32:4 -----------------------------------------------------------------
59+
32 | 1 += 1 // error
60+
| ^^^^
61+
| value += is not a member of Int - did you mean (1 : Int).!=? or perhaps (1 : Int).<=?

tests/neg/i22671.scala

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
object X:
2+
val w: Int = 42
3+
def w(y: Int): Int = x + y
4+
def x: Int = 42
5+
def x(y: Int): Int = x + y
6+
val z = 26
7+
8+
def w =
9+
X.w = 27 // error
10+
11+
def f =
12+
X.x = 27 // error
13+
14+
def g =
15+
import X.x
16+
x = 27 // error
17+
18+
def h =
19+
import X.x as y
20+
y = 27 // error
21+
22+
def i =
23+
import X.z as y
24+
y = 27 // error
25+
26+
def j =
27+
val x = 42
28+
x = 27 // error
29+
30+
def k =
31+
X.x += 27 // error
32+
1 += 1 // error
33+
34+
35+
object t8763:
36+
import collection.mutable
37+
def bar(): Unit =
38+
val names_times = mutable.Map.empty[String, mutable.Set[Long]]
39+
val line = ""
40+
val Array(fields) = line.split("\t")
41+
names_times(fields(0)) += fields(1).toLong // error
42+
43+
object t9834:
44+
object x { def apply() = 42 ; def update(i: Int) = () }
45+
x() += "42" // error
46+
47+
class C(c: Int):
48+
def test(): Unit =
49+
c = 42 // error

0 commit comments

Comments
 (0)