@@ -1037,6 +1037,33 @@ trait Applications extends Compatibility {
10371037 }
10381038 }
10391039
1040+ /** If the applied function is an automatically inserted `apply`
1041+ * method and one of its arguments has a type mismatch , append
1042+ * a note to the error message that explains where the required
1043+ * type comes from. See #19680 and associated test case.
1044+ */
1045+ def maybeAddInsertedApplyNote (failedState : TyperState , fun1 : Tree )(using Context ): Unit =
1046+ if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
1047+ fun1 match
1048+ case Select (qualifier, _) =>
1049+ def mapMessage (dia : Diagnostic ): Diagnostic =
1050+ dia match
1051+ case dia : Diagnostic .Error =>
1052+ dia.msg match
1053+ case msg : TypeMismatch =>
1054+ msg.inTree match
1055+ case Some (arg) if tree.args.exists(_.span == arg.span) =>
1056+ val noteText =
1057+ i """ The required type comes from a parameter of the automatically
1058+ |inserted `apply` method of ` ${qualifier.tpe}`. """ .stripMargin
1059+ Diagnostic .Error (msg.appendExplanation(" \n\n " + noteText), dia.pos)
1060+ case _ => dia
1061+ case msg => dia
1062+ case dia => dia
1063+ failedState.reporter.mapBufferedMessages(mapMessage)
1064+ case _ => ()
1065+ else ()
1066+
10401067 fun1.tpe match {
10411068 case err : ErrorType => cpy.Apply (tree)(fun1, proto.typedArgs()).withType(err)
10421069 case TryDynamicCallType =>
@@ -1097,7 +1124,11 @@ trait Applications extends Compatibility {
10971124 simpleApply(fun1, proto)
10981125 } {
10991126 (failedVal, failedState) =>
1100- def fail = { failedState.commit(); failedVal }
1127+ def fail =
1128+ maybeAddInsertedApplyNote(failedState, fun1)
1129+ failedState.commit()
1130+ failedVal
1131+
11011132 // Try once with original prototype and once (if different) with tupled one.
11021133 // The reason we need to try both is that the decision whether to use tupled
11031134 // or not was already taken but might have to be revised when an implicit
0 commit comments