Skip to content

Commit 81d454f

Browse files
committed
Improve -explain rendering
1 parent e523c8d commit 81d454f

File tree

86 files changed

+567
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+567
-360
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ object ConsoleReporter {
3232
/** Prints the message with the given position indication. */
3333
def doReport(dia: Diagnostic)(using Context): Unit = {
3434
printMessage(messageAndPos(dia))
35-
if Diagnostic.shouldExplain(dia) then
36-
printMessage(explanation(dia.msg))
37-
else if dia.msg.canExplain then
38-
printMessage("\nlonger explanation available when compiling with `-explain`")
3935
}
4036
}
4137
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ trait MessageRendering {
248248
else sb.append(msg.message)
249249
if (dia.isVerbose)
250250
appendFilterHelp(dia, sb)
251+
252+
if Diagnostic.shouldExplain(dia) then
253+
sb.append(EOL).append(newBox())
254+
sb.append(EOL).append(offsetBox).append(" Explanation")
255+
sb.append(EOL).append(newBox(soft = true))
256+
dia.msg.explanation.split(EOL).foreach { line =>
257+
sb.append(EOL).append(offsetBox).append(if line.isEmpty then "" else " ").append(line)
258+
}
259+
sb.append(EOL).append(endBox)
260+
else if dia.msg.canExplain then
261+
sb.append(EOL).append(newBox())
262+
sb.append(EOL).append(offsetBox).append(" longer explanation available when compiling with `-explain`")
263+
sb.append(EOL).append(endBox)
264+
251265
sb.toString
252266
}
253267

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,17 @@ object ErrorReporting {
142142
|conforms to
143143
| $expected
144144
|but the comparison trace ended with `false`:
145-
"""
145+
|"""
146146
val c = ctx.typerState.constraint
147147
val constraintText =
148148
if c.domainLambdas.isEmpty then
149149
"the empty constraint"
150150
else
151151
i"""a constraint with:
152152
|$c"""
153-
i"""
154-
|${TypeComparer.explained(_.isSubType(found, expected), header)}
155-
|
156-
|The tests were made under $constraintText"""
153+
i"""${TypeComparer.explained(_.isSubType(found, expected), header)}
154+
|
155+
|The tests were made under $constraintText"""
157156

158157
/** Format `raw` implicitNotFound or implicitAmbiguous argument, replacing
159158
* all occurrences of `${X}` where `X` is in `paramNames` with the

compiler/test-resources/repl/errmsgs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,57 @@ scala> val x: List[String] = List(1)
66
| ^
77
| Found: (1 : Int)
88
| Required: String
9-
longer explanation available when compiling with `-explain`
9+
|-----------------------------------------------------------------------------
10+
| longer explanation available when compiling with `-explain`
11+
·-----------------------------------------------------------------------------
1012
1 error found
1113
scala> val y: List[List[String]] = List(List(1))
1214
-- [E007] Type Mismatch Error: -------------------------------------------------
1315
1 | val y: List[List[String]] = List(List(1))
1416
| ^
1517
| Found: (1 : Int)
1618
| Required: String
17-
longer explanation available when compiling with `-explain`
19+
|-----------------------------------------------------------------------------
20+
| longer explanation available when compiling with `-explain`
21+
·-----------------------------------------------------------------------------
1822
1 error found
1923
scala> val z: (List[String], List[Int]) = (List(1), List("a"))
2024
-- [E007] Type Mismatch Error: -------------------------------------------------
2125
1 | val z: (List[String], List[Int]) = (List(1), List("a"))
2226
| ^
2327
| Found: (1 : Int)
2428
| Required: String
25-
longer explanation available when compiling with `-explain`
29+
|-----------------------------------------------------------------------------
30+
| longer explanation available when compiling with `-explain`
31+
·-----------------------------------------------------------------------------
2632
-- [E007] Type Mismatch Error: -------------------------------------------------
2733
1 | val z: (List[String], List[Int]) = (List(1), List("a"))
2834
| ^^^
2935
| Found: ("a" : String)
3036
| Required: Int
31-
longer explanation available when compiling with `-explain`
37+
|-----------------------------------------------------------------------------
38+
| longer explanation available when compiling with `-explain`
39+
·-----------------------------------------------------------------------------
3240
2 errors found
3341
scala> val a: Inv[String] = new Inv(new Inv(1))
3442
-- [E007] Type Mismatch Error: -------------------------------------------------
3543
1 | val a: Inv[String] = new Inv(new Inv(1))
3644
| ^^^^^^^^^^
3745
| Found: Inv[Int]
3846
| Required: String
39-
longer explanation available when compiling with `-explain`
47+
|-----------------------------------------------------------------------------
48+
| longer explanation available when compiling with `-explain`
49+
·-----------------------------------------------------------------------------
4050
1 error found
4151
scala> val b: Inv[String] = new Inv(1)
4252
-- [E007] Type Mismatch Error: -------------------------------------------------
4353
1 | val b: Inv[String] = new Inv(1)
4454
| ^
4555
| Found: (1 : Int)
4656
| Required: String
47-
longer explanation available when compiling with `-explain`
57+
|-----------------------------------------------------------------------------
58+
| longer explanation available when compiling with `-explain`
59+
·-----------------------------------------------------------------------------
4860
1 error found
4961
scala> abstract class C { type T; val x: T; val s: Unit = { type T = String; var y: T = x; locally { def f() = { type T = Int; val z: T = y }; f() } }; }
5062
-- [E007] Type Mismatch Error: -------------------------------------------------
@@ -55,7 +67,9 @@ scala> abstract class C { type T; val x: T; val s: Unit = { type T = String; var
5567
|
5668
|where: T is a type in class C
5769
| T² is a type in the initializer of value s which is an alias of String
58-
longer explanation available when compiling with `-explain`
70+
|-----------------------------------------------------------------------------
71+
| longer explanation available when compiling with `-explain`
72+
·-----------------------------------------------------------------------------
5973
-- [E007] Type Mismatch Error: -------------------------------------------------
6074
1 | abstract class C { type T; val x: T; val s: Unit = { type T = String; var y: T = x; locally { def f() = { type T = Int; val z: T = y }; f() } }; }
6175
| ^
@@ -64,7 +78,9 @@ longer explanation available when compiling with `-explain`
6478
|
6579
|where: T is a type in the initializer of value s which is an alias of String
6680
| T² is a type in method f which is an alias of Int
67-
longer explanation available when compiling with `-explain`
81+
|-----------------------------------------------------------------------------
82+
| longer explanation available when compiling with `-explain`
83+
·-----------------------------------------------------------------------------
6884
2 errors found
6985
scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr
7086
-- [E008] Not Found Error: -----------------------------------------------------
@@ -78,33 +94,43 @@ scala> val x: List[Int] = "foo" :: List(1)
7894
| ^^^^^
7995
| Found: ("foo" : String)
8096
| Required: Int
81-
longer explanation available when compiling with `-explain`
97+
|-----------------------------------------------------------------------------
98+
| longer explanation available when compiling with `-explain`
99+
·-----------------------------------------------------------------------------
82100
1 error found
83101
scala> while ((( foo ))) {}
84102
-- [E006] Not Found Error: -----------------------------------------------------
85103
1 | while ((( foo ))) {}
86104
| ^^^
87105
| Not found: foo
88-
longer explanation available when compiling with `-explain`
106+
|-----------------------------------------------------------------------------
107+
| longer explanation available when compiling with `-explain`
108+
·-----------------------------------------------------------------------------
89109
1 error found
90110
scala> val a: iDontExist = 1
91111
-- [E006] Not Found Error: -----------------------------------------------------
92112
1 | val a: iDontExist = 1
93113
| ^^^^^^^^^^
94114
| Not found: type iDontExist
95-
longer explanation available when compiling with `-explain`
115+
|-----------------------------------------------------------------------------
116+
| longer explanation available when compiling with `-explain`
117+
·-----------------------------------------------------------------------------
96118
1 error found
97119
scala> def foo1(x: => Int) = x _
98120
-- [E099] Syntax Error: --------------------------------------------------------
99121
1 | def foo1(x: => Int) = x _
100122
| ^^^
101123
|Only function types can be followed by _ but the current expression has type Int
102-
longer explanation available when compiling with `-explain`
124+
|-----------------------------------------------------------------------------
125+
| longer explanation available when compiling with `-explain`
126+
·-----------------------------------------------------------------------------
103127
1 error found
104128
scala> def foo2(x: => Int): () => Int = x _
105129
-- [E099] Syntax Error: --------------------------------------------------------
106130
1 | def foo2(x: => Int): () => Int = x _
107131
| ^^^
108132
|Only function types can be followed by _ but the current expression has type Int
109-
longer explanation available when compiling with `-explain`
133+
|-----------------------------------------------------------------------------
134+
| longer explanation available when compiling with `-explain`
135+
·-----------------------------------------------------------------------------
110136
1 error found

compiler/test-resources/repl/i13208.default.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ scala> try 1
55
| ^^^^^
66
| A try without catch or finally is equivalent to putting
77
| its body in a block; no exceptions are handled.
8-
longer explanation available when compiling with `-explain`
8+
|-----------------------------------------------------------------------------
9+
| longer explanation available when compiling with `-explain`
10+
·-----------------------------------------------------------------------------
911
val res0: Int = 1

compiler/test-resources/repl/i2063

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ scala> class Foo extends Bar // with one tab
33
1 | class Foo extends Bar // with one tab
44
| ^^^
55
| Not found: type Bar
6-
longer explanation available when compiling with `-explain`
6+
|-----------------------------------------------------------------------------
7+
| longer explanation available when compiling with `-explain`
8+
·-----------------------------------------------------------------------------
79
1 error found
810
scala> class Foo extends Bar // with spaces
911
-- [E006] Not Found Error: -----------------------------------------------------
1012
1 | class Foo extends Bar // with spaces
1113
| ^^^
1214
| Not found: type Bar
13-
longer explanation available when compiling with `-explain`
15+
|-----------------------------------------------------------------------------
16+
| longer explanation available when compiling with `-explain`
17+
·-----------------------------------------------------------------------------
1418
1 error found
1519
scala> class Foo extends Bar // with tabs
1620
-- [E006] Not Found Error: -----------------------------------------------------
1721
1 | class Foo extends Bar // with tabs
1822
| ^^^
1923
| Not found: type Bar
20-
longer explanation available when compiling with `-explain`
24+
|-----------------------------------------------------------------------------
25+
| longer explanation available when compiling with `-explain`
26+
·-----------------------------------------------------------------------------
2127
1 error found

compiler/test-resources/repl/i2213

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ scala> def x
33
1 | def x
44
| ^
55
| Missing return type
6-
longer explanation available when compiling with `-explain`
6+
|-----------------------------------------------------------------------------
7+
| longer explanation available when compiling with `-explain`
8+
·-----------------------------------------------------------------------------
79
scala> def x: Int
810
-- [E067] Syntax Error: --------------------------------------------------------
911
1 | def x: Int

compiler/test-resources/repl/i4217

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ scala> def foo(x: Option[Int]) = x match { case None => }
66
| match may not be exhaustive.
77
|
88
| It would fail on pattern case: Some(_)
9-
longer explanation available when compiling with `-explain`
9+
|-----------------------------------------------------------------------------
10+
| longer explanation available when compiling with `-explain`
11+
·-----------------------------------------------------------------------------
1012
def foo(x: Option[Int]): Unit

compiler/test-resources/repl/i4566

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ scala> object test { type ::[A, B]; def a: Int :: Int = ???; def b: Int = a }
44
| ^
55
| Found: Int :: Int
66
| Required: Int
7-
longer explanation available when compiling with `-explain`
7+
|-----------------------------------------------------------------------------
8+
| longer explanation available when compiling with `-explain`
9+
·-----------------------------------------------------------------------------
810
1 error found

compiler/test-resources/repl/i7644

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ scala> class T extends CanEqual
33
1 | class T extends CanEqual
44
| ^
55
| Cannot extend sealed trait CanEqual in a different source file
6-
longer explanation available when compiling with `-explain`
6+
|-----------------------------------------------------------------------------
7+
| longer explanation available when compiling with `-explain`
8+
·-----------------------------------------------------------------------------
79
-- [E056] Syntax Error: --------------------------------------------------------
810
1 | class T extends CanEqual
911
| ^^^^^^^^
@@ -14,7 +16,9 @@ scala> class T extends CanEqual
1416
1 | class T extends CanEqual
1517
| ^
1618
| Cannot extend sealed trait CanEqual in a different source file
17-
longer explanation available when compiling with `-explain`
19+
|-----------------------------------------------------------------------------
20+
| longer explanation available when compiling with `-explain`
21+
·-----------------------------------------------------------------------------
1822
-- [E056] Syntax Error: --------------------------------------------------------
1923
1 | class T extends CanEqual
2024
| ^^^^^^^^

compiler/test-resources/repl/importFromObj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ scala> buf += xs
1010
| ^^
1111
| Found: (o.xs : List[Int])
1212
| Required: Int
13-
longer explanation available when compiling with `-explain`
13+
|-----------------------------------------------------------------------------
14+
| longer explanation available when compiling with `-explain`
15+
·-----------------------------------------------------------------------------
1416
1 error found
1517
scala> buf ++= xs
1618
val res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

compiler/test-resources/repl/notFound

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ scala> Foo
33
1 | Foo
44
| ^^^
55
| Not found: Foo
6-
longer explanation available when compiling with `-explain`
6+
|-----------------------------------------------------------------------------
7+
| longer explanation available when compiling with `-explain`
8+
·-----------------------------------------------------------------------------
79
1 error found
810
scala> Bar
911
-- [E006] Not Found Error: -----------------------------------------------------
1012
1 | Bar
1113
| ^^^
1214
| Not found: Bar
13-
longer explanation available when compiling with `-explain`
15+
|-----------------------------------------------------------------------------
16+
| longer explanation available when compiling with `-explain`
17+
·-----------------------------------------------------------------------------
1418
1 error found

compiler/test-resources/repl/nowarn.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ scala> @annotation.nowarn def f = try 1 // @nowarn doesn't work on first line, c
55
| ^^^^^
66
| A try without catch or finally is equivalent to putting
77
| its body in a block; no exceptions are handled.
8-
longer explanation available when compiling with `-explain`
8+
|-----------------------------------------------------------------------------
9+
| longer explanation available when compiling with `-explain`
10+
·-----------------------------------------------------------------------------
911
def f: Int
1012
scala> @annotation.nowarn def f = try 1
1113
def f: Int
@@ -16,7 +18,9 @@ scala> def f = try 1
1618
| ^^^^^
1719
| A try without catch or finally is equivalent to putting
1820
| its body in a block; no exceptions are handled.
19-
longer explanation available when compiling with `-explain`
21+
|-----------------------------------------------------------------------------
22+
| longer explanation available when compiling with `-explain`
23+
·-----------------------------------------------------------------------------
2024
def f: Int
2125
scala> @annotation.nowarn def f = { 1; 2 }
2226
def f: Int
@@ -26,5 +30,7 @@ scala> def f = { 1; 2 }
2630
1 | def f = { 1; 2 }
2731
| ^
2832
|A pure expression does nothing in statement position; you may be omitting necessary parentheses
29-
longer explanation available when compiling with `-explain`
33+
|-----------------------------------------------------------------------------
34+
| longer explanation available when compiling with `-explain`
35+
·-----------------------------------------------------------------------------
3036
def f: Int

compiler/test-resources/repl/overrides

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ scala> class B { override def foo(i: Int): Unit = {}; }
33
1 | class B { override def foo(i: Int): Unit = {}; }
44
| ^
55
| method foo overrides nothing
6-
longer explanation available when compiling with `-explain`
6+
|-----------------------------------------------------------------------------
7+
| longer explanation available when compiling with `-explain`
8+
·-----------------------------------------------------------------------------
79
1 error found
810
scala> class A { def foo: Unit = {}; }
911
// defined class A
@@ -12,5 +14,7 @@ scala> class B extends A { override def foo(i: Int): Unit = {}; }
1214
1 | class B extends A { override def foo(i: Int): Unit = {}; }
1315
| ^
1416
| method foo has a different signature than the overridden declaration
15-
longer explanation available when compiling with `-explain`
17+
|-----------------------------------------------------------------------------
18+
| longer explanation available when compiling with `-explain`
19+
·-----------------------------------------------------------------------------
1620
1 error found

compiler/test-resources/repl/reset-command

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ scala> resetNoArgsStillWorks
2222
1 | resetNoArgsStillWorks
2323
| ^^^^^^^^^^^^^^^^^^^^^
2424
| Not found: resetNoArgsStillWorks
25-
longer explanation available when compiling with `-explain`
25+
|-----------------------------------------------------------------------------
26+
| longer explanation available when compiling with `-explain`
27+
·-----------------------------------------------------------------------------
2628
1 error found
2729

2830
scala>:settings "-Dfoo=bar baz"

0 commit comments

Comments
 (0)