Skip to content

Commit a37ae7c

Browse files
committed
Improve -explain rendering
1 parent 4c7ec9b commit a37ae7c

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

+399
-360
lines changed

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

-4
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

+13
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ 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 (enabled by `-explain`)")
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(offsetBox)
262+
sb.append(EOL).append(offsetBox).append(" longer explanation available when compiling with `-explain`")
263+
251264
sb.toString
252265
}
253266

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

+4-5
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

+26-13
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,51 @@ 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`
1011
1 error found
1112
scala> val y: List[List[String]] = List(List(1))
1213
-- [E007] Type Mismatch Error: -------------------------------------------------
1314
1 | val y: List[List[String]] = List(List(1))
1415
| ^
1516
| Found: (1 : Int)
1617
| Required: String
17-
longer explanation available when compiling with `-explain`
18+
|
19+
| longer explanation available when compiling with `-explain`
1820
1 error found
1921
scala> val z: (List[String], List[Int]) = (List(1), List("a"))
2022
-- [E007] Type Mismatch Error: -------------------------------------------------
2123
1 | val z: (List[String], List[Int]) = (List(1), List("a"))
2224
| ^
2325
| Found: (1 : Int)
2426
| Required: String
25-
longer explanation available when compiling with `-explain`
27+
|
28+
| longer explanation available when compiling with `-explain`
2629
-- [E007] Type Mismatch Error: -------------------------------------------------
2730
1 | val z: (List[String], List[Int]) = (List(1), List("a"))
2831
| ^^^
2932
| Found: ("a" : String)
3033
| Required: Int
31-
longer explanation available when compiling with `-explain`
34+
|
35+
| longer explanation available when compiling with `-explain`
3236
2 errors found
3337
scala> val a: Inv[String] = new Inv(new Inv(1))
3438
-- [E007] Type Mismatch Error: -------------------------------------------------
3539
1 | val a: Inv[String] = new Inv(new Inv(1))
3640
| ^^^^^^^^^^
3741
| Found: Inv[Int]
3842
| Required: String
39-
longer explanation available when compiling with `-explain`
43+
|
44+
| longer explanation available when compiling with `-explain`
4045
1 error found
4146
scala> val b: Inv[String] = new Inv(1)
4247
-- [E007] Type Mismatch Error: -------------------------------------------------
4348
1 | val b: Inv[String] = new Inv(1)
4449
| ^
4550
| Found: (1 : Int)
4651
| Required: String
47-
longer explanation available when compiling with `-explain`
52+
|
53+
| longer explanation available when compiling with `-explain`
4854
1 error found
4955
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() } }; }
5056
-- [E007] Type Mismatch Error: -------------------------------------------------
@@ -55,7 +61,8 @@ scala> abstract class C { type T; val x: T; val s: Unit = { type T = String; var
5561
|
5662
|where: T is a type in class C
5763
| T² is a type in the initializer of value s which is an alias of String
58-
longer explanation available when compiling with `-explain`
64+
|
65+
| longer explanation available when compiling with `-explain`
5966
-- [E007] Type Mismatch Error: -------------------------------------------------
6067
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() } }; }
6168
| ^
@@ -64,7 +71,8 @@ longer explanation available when compiling with `-explain`
6471
|
6572
|where: T is a type in the initializer of value s which is an alias of String
6673
| T² is a type in method f which is an alias of Int
67-
longer explanation available when compiling with `-explain`
74+
|
75+
| longer explanation available when compiling with `-explain`
6876
2 errors found
6977
scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr
7078
-- [E008] Not Found Error: -----------------------------------------------------
@@ -78,33 +86,38 @@ scala> val x: List[Int] = "foo" :: List(1)
7886
| ^^^^^
7987
| Found: ("foo" : String)
8088
| Required: Int
81-
longer explanation available when compiling with `-explain`
89+
|
90+
| longer explanation available when compiling with `-explain`
8291
1 error found
8392
scala> while ((( foo ))) {}
8493
-- [E006] Not Found Error: -----------------------------------------------------
8594
1 | while ((( foo ))) {}
8695
| ^^^
8796
| Not found: foo
88-
longer explanation available when compiling with `-explain`
97+
|
98+
| longer explanation available when compiling with `-explain`
8999
1 error found
90100
scala> val a: iDontExist = 1
91101
-- [E006] Not Found Error: -----------------------------------------------------
92102
1 | val a: iDontExist = 1
93103
| ^^^^^^^^^^
94104
| Not found: type iDontExist
95-
longer explanation available when compiling with `-explain`
105+
|
106+
| longer explanation available when compiling with `-explain`
96107
1 error found
97108
scala> def foo1(x: => Int) = x _
98109
-- [E099] Syntax Error: --------------------------------------------------------
99110
1 | def foo1(x: => Int) = x _
100111
| ^^^
101112
|Only function types can be followed by _ but the current expression has type Int
102-
longer explanation available when compiling with `-explain`
113+
|
114+
| longer explanation available when compiling with `-explain`
103115
1 error found
104116
scala> def foo2(x: => Int): () => Int = x _
105117
-- [E099] Syntax Error: --------------------------------------------------------
106118
1 | def foo2(x: => Int): () => Int = x _
107119
| ^^^
108120
|Only function types can be followed by _ but the current expression has type Int
109-
longer explanation available when compiling with `-explain`
121+
|
122+
| longer explanation available when compiling with `-explain`
110123
1 error found

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ 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`
910
val res0: Int = 1

compiler/test-resources/repl/i2063

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ 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`
78
1 error found
89
scala> class Foo extends Bar // with spaces
910
-- [E006] Not Found Error: -----------------------------------------------------
1011
1 | class Foo extends Bar // with spaces
1112
| ^^^
1213
| Not found: type Bar
13-
longer explanation available when compiling with `-explain`
14+
|
15+
| longer explanation available when compiling with `-explain`
1416
1 error found
1517
scala> class Foo extends Bar // with tabs
1618
-- [E006] Not Found Error: -----------------------------------------------------
1719
1 | class Foo extends Bar // with tabs
1820
| ^^^
1921
| Not found: type Bar
20-
longer explanation available when compiling with `-explain`
22+
|
23+
| longer explanation available when compiling with `-explain`
2124
1 error found

compiler/test-resources/repl/i2213

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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`
78
scala> def x: Int
89
-- [E067] Syntax Error: --------------------------------------------------------
910
1 | def x: Int

compiler/test-resources/repl/i4217

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ 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`
1011
def foo(x: Option[Int]): Unit

compiler/test-resources/repl/i4566

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ 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`
89
1 error found

compiler/test-resources/repl/i7644

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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`
78
-- [E056] Syntax Error: --------------------------------------------------------
89
1 | class T extends CanEqual
910
| ^^^^^^^^
@@ -14,7 +15,8 @@ scala> class T extends CanEqual
1415
1 | class T extends CanEqual
1516
| ^
1617
| Cannot extend sealed trait CanEqual in a different source file
17-
longer explanation available when compiling with `-explain`
18+
|
19+
| longer explanation available when compiling with `-explain`
1820
-- [E056] Syntax Error: --------------------------------------------------------
1921
1 | class T extends CanEqual
2022
| ^^^^^^^^

compiler/test-resources/repl/importFromObj

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ 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`
1415
1 error found
1516
scala> buf ++= xs
1617
val res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

compiler/test-resources/repl/notFound

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ 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`
78
1 error found
89
scala> Bar
910
-- [E006] Not Found Error: -----------------------------------------------------
1011
1 | Bar
1112
| ^^^
1213
| Not found: Bar
13-
longer explanation available when compiling with `-explain`
14+
|
15+
| longer explanation available when compiling with `-explain`
1416
1 error found

compiler/test-resources/repl/nowarn.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ 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`
910
def f: Int
1011
scala> @annotation.nowarn def f = try 1
1112
def f: Int
@@ -16,7 +17,8 @@ scala> def f = try 1
1617
| ^^^^^
1718
| A try without catch or finally is equivalent to putting
1819
| its body in a block; no exceptions are handled.
19-
longer explanation available when compiling with `-explain`
20+
|
21+
| longer explanation available when compiling with `-explain`
2022
def f: Int
2123
scala> @annotation.nowarn def f = { 1; 2 }
2224
def f: Int
@@ -26,5 +28,6 @@ scala> def f = { 1; 2 }
2628
1 | def f = { 1; 2 }
2729
| ^
2830
|A pure expression does nothing in statement position; you may be omitting necessary parentheses
29-
longer explanation available when compiling with `-explain`
31+
|
32+
| longer explanation available when compiling with `-explain`
3033
def f: Int

compiler/test-resources/repl/overrides

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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`
78
1 error found
89
scala> class A { def foo: Unit = {}; }
910
// defined class A
@@ -12,5 +13,6 @@ scala> class B extends A { override def foo(i: Int): Unit = {}; }
1213
1 | class B extends A { override def foo(i: Int): Unit = {}; }
1314
| ^
1415
| method foo has a different signature than the overridden declaration
15-
longer explanation available when compiling with `-explain`
16+
|
17+
| longer explanation available when compiling with `-explain`
1618
1 error found

compiler/test-resources/repl/reset-command

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ 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`
2627
1 error found
2728

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

compiler/test-resources/type-printer/type-mismatch

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ scala> val x: Foo[String] = res0
88
| ^^^^
99
| Found: (res0 : Foo[Int])
1010
| Required: Foo[String]
11-
longer explanation available when compiling with `-explain`
11+
|
12+
| longer explanation available when compiling with `-explain`
1213
1 error found

tests/neg-custom-args/explicit-nulls/byname-nullables.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
| ^
44
| Found: (x : String | Null)
55
| Required: String
6-
7-
longer explanation available when compiling with `-explain`
6+
|
7+
| longer explanation available when compiling with `-explain`
88
-- Error: tests/neg-custom-args/explicit-nulls/byname-nullables.scala:43:32 --------------------------------------------
99
43 | if x != null then f(identity(x), 1) // error: dropping not null check fails typing
1010
| ^^^^^^^^^^^

tests/neg-custom-args/explicit-nulls/i7883.check

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
6 | case r(hd, tl) => Some((hd, tl)) // error // error // error
1111
| ^^
1212
| Not found: hd
13-
14-
longer explanation available when compiling with `-explain`
13+
|
14+
| longer explanation available when compiling with `-explain`
1515
-- [E006] Not Found Error: tests/neg-custom-args/explicit-nulls/i7883.scala:6:34 ---------------------------------------
1616
6 | case r(hd, tl) => Some((hd, tl)) // error // error // error
1717
| ^^
1818
| Not found: tl
19-
20-
longer explanation available when compiling with `-explain`
19+
|
20+
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)