Skip to content

Commit

Permalink
Move maybeAddInsertedApplyNote to outer scope, add explicit types
Browse files Browse the repository at this point in the history
[Cherry-picked 1f06af1]
  • Loading branch information
mbovel authored and WojciechMazur committed Jul 6, 2024
1 parent 561b46e commit 8fa10ee
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,34 @@ trait Applications extends Compatibility {
}
}

/** If the applied function is an automatically inserted `apply`
* method and one of its arguments has a type mismatch , append
* a note to the error message that explains where the required
* type comes from. See #19680 and associated test case.
*/
def maybeAddInsertedApplyNote(failedState: TyperState, fun1: Tree)(using Context): Unit =
if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
fun1 match
case Select(qualifier, _) =>
def mapMessage(dia: Diagnostic): Diagnostic =
dia match
case dia: Diagnostic.Error =>
dia.msg match
case msg: TypeMismatch =>
msg.inTree match
case Some(arg) if tree.args.exists(_.span == arg.span) =>
val noteText =
i"""The required type comes from a parameter of the automatically
|inserted `apply` method of `${qualifier.tpe}`,
|which is the type of `${qualifier.show}`.""".stripMargin
Diagnostic.Error(msg.appendExplanation("\n\n" + noteText), dia.pos)
case _ => dia
case msg => dia
case dia => dia
failedState.reporter.mapBufferedMessages(mapMessage)
case _ => ()
else ()

fun1.tpe match {
case err: ErrorType => cpy.Apply(tree)(fun1, proto.typedArgs()).withType(err)
case TryDynamicCallType =>
Expand Down Expand Up @@ -1081,35 +1109,10 @@ trait Applications extends Compatibility {
} {
(failedVal, failedState) =>
def fail =
insertedApplyNote()
maybeAddInsertedApplyNote(failedState, fun1)
failedState.commit()
failedVal

/** If the applied function is an automatically inserted `apply`
* method and one of its arguments has a type mismatch , append
* a note to the error message that explains where the required
* type comes from. See #19680 and associated test case.
*/
def insertedApplyNote() =
if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
fun1 match
case Select(qualifier, _) =>
failedState.reporter.mapBufferedMessages:
case dia: Diagnostic.Error =>
dia.msg match
case msg: TypeMismatch =>
msg.inTree match
case Some(arg) if tree.args.exists(_.span == arg.span) =>
val noteText =
i"""The required type comes from a parameter of the automatically
|inserted `apply` method of `${qualifier.tpe}`,
|which is the type of `${qualifier.show}`.""".stripMargin
Diagnostic.Error(msg.appendExplanation("\n\n" + noteText), dia.pos)
case _ => dia
case msg => dia
case dia => dia
case _ => ()

// Try once with original prototype and once (if different) with tupled one.
// The reason we need to try both is that the decision whether to use tupled
// or not was already taken but might have to be revised when an implicit
Expand Down

0 comments on commit 8fa10ee

Please sign in to comment.