Skip to content

Commit 61aeabf

Browse files
committed
Format message, fix tests
1 parent 127372d commit 61aeabf

File tree

6 files changed

+81
-20
lines changed

6 files changed

+81
-20
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import Inferencing.*
2323
import reporting.*
2424
import Nullables.*, NullOpsDecorator.*
2525
import config.Feature
26-
import printing.Texts.{stringToText, Text}
2726

2827
import collection.mutable
2928
import config.Printers.{overload, typr, unapp}
@@ -1074,25 +1073,30 @@ trait Applications extends Compatibility {
10741073
failedState.commit()
10751074
failedVal
10761075

1077-
/** If the applied function is an automatically inserted `apply` method and one of its
1078-
* arguments has a type mistmathc , append a note
1079-
* to the error message that explains that the required type comes from a parameter
1080-
* of the inserted `apply` method. See #19680 and associated test case.
1076+
/** If the applied function is an automatically inserted `apply`
1077+
* method and one of its arguments has a type mismatch , append
1078+
* a note to the error message that explains where the required
1079+
* type comes from. See #19680 and associated test case.
10811080
*/
10821081
def insertedApplyNote() =
10831082
if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
1084-
failedState.reporter.mapBufferedMessages:
1085-
case dia: Diagnostic.Error =>
1086-
dia.msg match
1087-
case msg: TypeMismatch =>
1088-
msg.inTree match
1089-
case Some(arg) if tree.args.exists(_.span == arg.span) =>
1090-
val Select(prefix, _apply) = fun1: @unchecked
1091-
val txt = ("\n\nThe required type comes from a parameter of the automatically inserted " ~ ("`" + ctx.printer.nameString(nme.apply ) + "`") ~ " method of " ~ prefix.tpe.show ~ ", which is the type of " ~ prefix.show ~ ".").show
1092-
Diagnostic.Error(msg.append(txt), dia.pos)
1093-
case _ => dia
1094-
case msg => dia
1095-
case dia => dia
1083+
fun1 match
1084+
case Select(qualifier, _) =>
1085+
failedState.reporter.mapBufferedMessages:
1086+
case dia: Diagnostic.Error =>
1087+
dia.msg match
1088+
case msg: TypeMismatch =>
1089+
msg.inTree match
1090+
case Some(arg) if tree.args.exists(_.span == arg.span) =>
1091+
val Select(qualifier, _) = fun1: @unchecked
1092+
val noteText =
1093+
i"""The required type comes from a parameter of the automatically
1094+
|inserted `apply` method of `${qualifier.tpe}`,
1095+
|which is the type of `${qualifier.show}`.""".stripMargin
1096+
Diagnostic.Error(msg.appendExplanation("\n\n" + noteText), dia.pos)
1097+
case _ => dia
1098+
case msg => dia
1099+
case dia => dia
10961100

10971101
// Try once with original prototype and once (if different) with tupled one.
10981102
// The reason we need to try both is that the decision whether to use tupled

tests/neg/19680.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- [E007] Type Mismatch Error: tests/neg/19680.scala:9:67 --------------------------------------------------------------
2+
9 |def renderWidget(using Config): Unit = renderWebsite("/tmp")(Config()) // error: found Config, required Int
3+
| ^^^^^^^^
4+
| Found: Config
5+
| Required: Int
6+
|---------------------------------------------------------------------------------------------------------------------
7+
| Explanation (enabled by `-explain`)
8+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9+
|
10+
| Tree: new Config()
11+
| I tried to show that
12+
| Config
13+
| conforms to
14+
| Int
15+
| but none of the attempts shown below succeeded:
16+
|
17+
| ==> Config <: Int = false
18+
|
19+
| The tests were made under the empty constraint
20+
|
21+
| The required type comes from a parameter of the automatically
22+
| inserted `apply` method of `scala.collection.StringOps`,
23+
| which is the type of `augmentString(renderWebsite("/tmp")(x$1))`.
24+
---------------------------------------------------------------------------------------------------------------------

tests/neg/19680.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -explain
2+
3+
// Tests that the error message indicates that the required type `Int` comes
4+
// from the automatically inserted `apply` method of `String`. This note is
5+
// inserted by `insertedApplyNote` in `Applications`.
6+
7+
class Config()
8+
def renderWebsite(path: String)(using config: Config): String = ???
9+
def renderWidget(using Config): Unit = renderWebsite("/tmp")(Config()) // error: found Config, required Int

tests/neg/19680b.check

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- [E007] Type Mismatch Error: tests/neg/19680b.scala:2:21 -------------------------------------------------------------
2+
2 |def Test = List(1,2)("hello") // error: found String, required Int
3+
| ^^^^^^^
4+
| Found: ("hello" : String)
5+
| Required: Int
6+
|---------------------------------------------------------------------------------------------------------------------
7+
| Explanation (enabled by `-explain`)
8+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9+
|
10+
| Tree: "hello"
11+
| I tried to show that
12+
| ("hello" : String)
13+
| conforms to
14+
| Int
15+
| but none of the attempts shown below succeeded:
16+
|
17+
| ==> ("hello" : String) <: Int
18+
| ==> String <: Int = false
19+
|
20+
| The tests were made under the empty constraint
21+
|
22+
| The required type comes from a parameter of the automatically
23+
| inserted `apply` method of `List[Int]`,
24+
| which is the type of `List.apply[Int]([1,2 : Int]*)`.
25+
---------------------------------------------------------------------------------------------------------------------

tests/neg/19680b.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//> using options -explain
2+
def Test = List(1,2)("hello") // error: found String, required Int

tests/pos/19680.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)