Skip to content

Commit 2fc524a

Browse files
authored
Use a better span for an anonymous class (#24640)
An anonymous class is expanded roughly to { class $anon{ ... }; new $anon } Previously the `new` part had a zero extent span at the end of the class. When used with significant indentation, this caused some puzzlement in error messages since one thought the last class definition was to blame. The span is now the whole anonymous class.
2 parents 320e5ea + b4e1a96 commit 2fc524a

File tree

9 files changed

+78
-82
lines changed

9 files changed

+78
-82
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12511251
typedAhead(parent, tree => inferTypeParams(typedType(tree), pt))
12521252
val anon = tpnme.ANON_CLASS
12531253
val clsDef = TypeDef(anon, templ1).withFlags(Final | Synthetic)
1254-
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(anon), Nil)), pt)
1254+
typed(
1255+
cpy.Block(tree)(
1256+
clsDef :: Nil,
1257+
New(Ident(anon), Nil).withSpan(tree.span)),
1258+
pt)
12551259
case _ =>
12561260
var tpt1 = typedType(tree.tpt)
12571261
val tsym = tpt1.tpe.underlyingClassRef(refinementOK = false).typeSymbol

tests/neg-custom-args/captures/capt1.check

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
| Note that capability x is not included in capture set {}.
3535
|
3636
| longer explanation available when compiling with `-explain`
37-
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:28:40 ----------------------------------------
38-
28 | def m() = if x == null then y else y // error
39-
| ^
40-
| Found: A^{x}
41-
| Required: A
37+
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:27:2 -----------------------------------------
38+
27 | new A: // error
39+
| ^
40+
| Found: A^{x}
41+
| Required: A
4242
|
43-
| Note that capability x is not included in capture set {}.
43+
| Note that capability x is not included in capture set {}.
44+
28 | def m() = if x == null then y else y
4445
|
4546
| longer explanation available when compiling with `-explain`
4647
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/capt1.scala:36:24 ----------------------------------------

tests/neg-custom-args/captures/capt1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def h3(x: Cap): A =
2424
F(22) // error
2525

2626
def h4(x: Cap, y: Int): A =
27-
new A:
28-
def m() = if x == null then y else y // error
27+
new A: // error
28+
def m() = if x == null then y else y
2929

3030
def f1(c: Cap): () ->{c} c.type = () => c // ok
3131

tests/neg-custom-args/captures/method-uses.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def test3(xs: List[() => Unit]): () ->{xs*} Unit = () =>
2323
def test4(xs: List[() => Unit]) = () => xs.head // error, ok under deferredReaches
2424

2525
def test5(xs: List[() => Unit]) = new:
26-
println(xs.head) // error, ok under deferredReaches // error
26+
println(xs.head) // error, ok under deferredReaches
2727

2828
def test6(xs: List[() => Unit]) =
29-
val x= new { println(xs.head) } // error // error
29+
val x= new { println(xs.head) } // error
3030
x
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
-- Error: tests/neg-custom-args/captures/mut-iterator4.scala:9:26 ------------------------------------------------------
2-
9 | update def next() = f(Iterator.this.next()) // error // error
2+
9 | update def next() = f(Iterator.this.next()) // error
33
| ^^^^^^^^^^^^^
44
| Read-only method map accesses exclusive capability (Iterator.this : Iterator[T]^);
55
| method map should be declared an update method to allow this.
66
|
77
| where: ^ refers to the universal root capability
8-
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/mut-iterator4.scala:9:47 ---------------------------------
9-
9 | update def next() = f(Iterator.this.next()) // error // error
10-
| ^
11-
|Found: Iterator[U^'s1]^{Iterator.this.rd, f, Iterator.this, cap}
12-
|Required: Iterator[U]^{Iterator.this, f}
13-
|
14-
|Note that capability cap is not included in capture set {Iterator.this, f}.
15-
|
16-
|where: cap is a fresh root capability created in method map when constructing instance Object with (Iterator[U]^{cap².rd}) {...}
17-
|
18-
| longer explanation available when compiling with `-explain`
19-
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/mut-iterator4.scala:23:34 --------------------------------
20-
23 | update def next() = f(it.next()) // error
21-
| ^
22-
|Found: Iterator[U^'s2]^{it.rd, f, it, cap}
8+
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/mut-iterator4.scala:21:81 --------------------------------
9+
21 |def mappedIterator[T, U](it: Iterator[T]^, f: T => U): Iterator[U]^{it, f} = new Iterator: // error
10+
| ^
11+
|Found: Iterator[U^'s1]^{it.rd, f, it, cap}
2312
|Required: Iterator[U]^{it, f}
2413
|
2514
|Note that capability cap is not included in capture set {it, f}.
2615
|
2716
|where: cap is a fresh root capability created in method mappedIterator when constructing instance Object with (Iterator[U]^{cap².rd}) {...}
17+
22 | def hasNext = it.hasNext
18+
23 | update def next() = f(it.next())
2819
|
2920
| longer explanation available when compiling with `-explain`

tests/neg-custom-args/captures/mut-iterator4.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait Iterator[T] extends Stateful:
66

77
def map[U](f: T => U): Iterator[U]^{Iterator.this, f} = new Iterator:
88
def hasNext = Iterator.this.hasNext
9-
update def next() = f(Iterator.this.next()) // error // error
9+
update def next() = f(Iterator.this.next()) // error
1010

1111
end Iterator
1212

@@ -18,9 +18,9 @@ def listIterator[T](xs: List[T]): Iterator[T]^ = new Iterator[T]:
1818
current = xs1
1919
x
2020

21-
def mappedIterator[T, U](it: Iterator[T]^, f: T => U): Iterator[U]^{it, f} = new Iterator:
21+
def mappedIterator[T, U](it: Iterator[T]^, f: T => U): Iterator[U]^{it, f} = new Iterator: // error
2222
def hasNext = it.hasNext
23-
update def next() = f(it.next()) // error
23+
update def next() = f(it.next())
2424

2525
class IO extends SharedCapability:
2626
def write(x: Any): Unit = ()

tests/neg/6570-1.check

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
-- [E007] Type Mismatch Error: tests/neg/6570-1.scala:23:27 ------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/6570-1.scala:23:14 ------------------------------------------------------------
22
23 | def thing = new Trait1 {} // error
3-
| ^
4-
| Found: Object with Trait1 {...}
5-
| Required: N[Box[Int & String]]
3+
| ^^^^^^^^^^^^^
4+
| Found: Object with Trait1 {...}
5+
| Required: N[Box[Int & String]]
66
|
7-
| Note: a match type could not be fully reduced:
7+
| Note: a match type could not be fully reduced:
88
|
9-
| trying to reduce N[Box[Int & String]]
10-
| failed since selector Box[Int & String]
11-
| is uninhabited (there are no values of that type).
9+
| trying to reduce N[Box[Int & String]]
10+
| failed since selector Box[Int & String]
11+
| is uninhabited (there are no values of that type).
1212
|
1313
| longer explanation available when compiling with `-explain`
1414
-- [E007] Type Mismatch Error: tests/neg/6570-1.scala:36:54 ------------------------------------------------------------

tests/neg/6570.check

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
| x >: Int
1818
|
1919
| longer explanation available when compiling with `-explain`
20-
-- [E007] Type Mismatch Error: tests/neg/6570.scala:29:29 --------------------------------------------------------------
20+
-- [E007] Type Mismatch Error: tests/neg/6570.scala:29:16 --------------------------------------------------------------
2121
29 | def thing = new Trait1 {} // error
22-
| ^
23-
| Found: Object with Base.Trait1 {...}
24-
| Required: Base.N[String & Int]
22+
| ^^^^^^^^^^^^^
23+
| Found: Object with Base.Trait1 {...}
24+
| Required: Base.N[String & Int]
2525
|
26-
| Note: a match type could not be fully reduced:
26+
| Note: a match type could not be fully reduced:
2727
|
28-
| trying to reduce Base.N[String & Int]
29-
| failed since selector String & Int
30-
| is uninhabited (there are no values of that type).
28+
| trying to reduce Base.N[String & Int]
29+
| failed since selector String & Int
30+
| is uninhabited (there are no values of that type).
3131
|
3232
| longer explanation available when compiling with `-explain`
3333
-- [E007] Type Mismatch Error: tests/neg/6570.scala:47:32 --------------------------------------------------------------
@@ -46,43 +46,43 @@
4646
| a >: Int
4747
|
4848
| longer explanation available when compiling with `-explain`
49-
-- [E007] Type Mismatch Error: tests/neg/6570.scala:51:29 --------------------------------------------------------------
49+
-- [E007] Type Mismatch Error: tests/neg/6570.scala:51:16 --------------------------------------------------------------
5050
51 | def thing = new Trait1 {} // error
51-
| ^
52-
| Found: Object with Base.Trait1 {...}
53-
| Required: Base.N[String & Int]
51+
| ^^^^^^^^^^^^^
52+
| Found: Object with Base.Trait1 {...}
53+
| Required: Base.N[String & Int]
5454
|
55-
| Note: a match type could not be fully reduced:
55+
| Note: a match type could not be fully reduced:
5656
|
57-
| trying to reduce Base.N[String & Int]
58-
| failed since selector String & Int
59-
| is uninhabited (there are no values of that type).
57+
| trying to reduce Base.N[String & Int]
58+
| failed since selector String & Int
59+
| is uninhabited (there are no values of that type).
6060
|
6161
| longer explanation available when compiling with `-explain`
62-
-- [E007] Type Mismatch Error: tests/neg/6570.scala:69:29 --------------------------------------------------------------
62+
-- [E007] Type Mismatch Error: tests/neg/6570.scala:69:16 --------------------------------------------------------------
6363
69 | def thing = new Trait1 {} // error
64-
| ^
65-
| Found: Object with Base.Trait1 {...}
66-
| Required: Base.N[String & Int]
64+
| ^^^^^^^^^^^^^
65+
| Found: Object with Base.Trait1 {...}
66+
| Required: Base.N[String & Int]
6767
|
68-
| Note: a match type could not be fully reduced:
68+
| Note: a match type could not be fully reduced:
6969
|
70-
| trying to reduce Base.N[String & Int]
71-
| failed since selector String & Int
72-
| is uninhabited (there are no values of that type).
70+
| trying to reduce Base.N[String & Int]
71+
| failed since selector String & Int
72+
| is uninhabited (there are no values of that type).
7373
|
7474
| longer explanation available when compiling with `-explain`
75-
-- [E007] Type Mismatch Error: tests/neg/6570.scala:86:29 --------------------------------------------------------------
75+
-- [E007] Type Mismatch Error: tests/neg/6570.scala:86:16 --------------------------------------------------------------
7676
86 | def thing = new Trait1 {} // error
77-
| ^
78-
| Found: Object with Base.Trait1 {...}
79-
| Required: Base.N[String & Int]
77+
| ^^^^^^^^^^^^^
78+
| Found: Object with Base.Trait1 {...}
79+
| Required: Base.N[String & Int]
8080
|
81-
| Note: a match type could not be fully reduced:
81+
| Note: a match type could not be fully reduced:
8282
|
83-
| trying to reduce Base.N[String & Int]
84-
| failed since selector String & Int
85-
| is uninhabited (there are no values of that type).
83+
| trying to reduce Base.N[String & Int]
84+
| failed since selector String & Int
85+
| is uninhabited (there are no values of that type).
8686
|
8787
| longer explanation available when compiling with `-explain`
8888
-- [E007] Type Mismatch Error: tests/neg/6570.scala:103:32 -------------------------------------------------------------
@@ -101,16 +101,16 @@
101101
| t >: Int
102102
|
103103
| longer explanation available when compiling with `-explain`
104-
-- [E007] Type Mismatch Error: tests/neg/6570.scala:107:29 -------------------------------------------------------------
104+
-- [E007] Type Mismatch Error: tests/neg/6570.scala:107:16 -------------------------------------------------------------
105105
107 | def thing = new Trait1 {} // error
106-
| ^
107-
| Found: Object with Base.Trait1 {...}
108-
| Required: Base.N[String & Int]
106+
| ^^^^^^^^^^^^^
107+
| Found: Object with Base.Trait1 {...}
108+
| Required: Base.N[String & Int]
109109
|
110-
| Note: a match type could not be fully reduced:
110+
| Note: a match type could not be fully reduced:
111111
|
112-
| trying to reduce Base.N[String & Int]
113-
| failed since selector String & Int
114-
| is uninhabited (there are no values of that type).
112+
| trying to reduce Base.N[String & Int]
113+
| failed since selector String & Int
114+
| is uninhabited (there are no values of that type).
115115
|
116116
| longer explanation available when compiling with `-explain`

tests/neg/i13703.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E007] Type Mismatch Error: tests/neg/i13703.scala:3:78 -------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/i13703.scala:3:52 -------------------------------------------------------------
22
3 |val f2: Foo { val i: Int; def i_=(x: Int): Unit } = new Foo { var i: Int = 0 } // error
3-
| ^
4-
| Found: Object with Foo {...}
5-
| Required: Foo{val i: Int; def i_=(x: Int): Unit}
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| Found: Object with Foo {...}
5+
| Required: Foo{val i: Int; def i_=(x: Int): Unit}
66
|
77
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)