@@ -1020,6 +1020,33 @@ trait Applications extends Compatibility {
10201020 }
10211021 }
10221022
1023+ /** If the applied function is an automatically inserted `apply`
1024+ * method and one of its arguments has a type mismatch , append
1025+ * a note to the error message that explains where the required
1026+ * type comes from. See #19680 and associated test case.
1027+ */
1028+ def maybeAddInsertedApplyNote (failedState : TyperState , fun1 : Tree )(using Context ): Unit =
1029+ if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
1030+ fun1 match
1031+ case Select (qualifier, _) =>
1032+ def mapMessage (dia : Diagnostic ): Diagnostic =
1033+ dia match
1034+ case dia : Diagnostic .Error =>
1035+ dia.msg match
1036+ case msg : TypeMismatch =>
1037+ msg.inTree match
1038+ case Some (arg) if tree.args.exists(_.span == arg.span) =>
1039+ val noteText =
1040+ i """ The required type comes from a parameter of the automatically
1041+ |inserted `apply` method of ` ${qualifier.tpe}`. """ .stripMargin
1042+ Diagnostic .Error (msg.appendExplanation(" \n\n " + noteText), dia.pos)
1043+ case _ => dia
1044+ case msg => dia
1045+ case dia => dia
1046+ failedState.reporter.mapBufferedMessages(mapMessage)
1047+ case _ => ()
1048+ else ()
1049+
10231050 fun1.tpe match {
10241051 case err : ErrorType => cpy.Apply (tree)(fun1, proto.typedArgs()).withType(err)
10251052 case TryDynamicCallType =>
@@ -1080,7 +1107,11 @@ trait Applications extends Compatibility {
10801107 simpleApply(fun1, proto)
10811108 } {
10821109 (failedVal, failedState) =>
1083- def fail = { failedState.commit(); failedVal }
1110+ def fail =
1111+ maybeAddInsertedApplyNote(failedState, fun1)
1112+ failedState.commit()
1113+ failedVal
1114+
10841115 // Try once with original prototype and once (if different) with tupled one.
10851116 // The reason we need to try both is that the decision whether to use tupled
10861117 // or not was already taken but might have to be revised when an implicit
0 commit comments