@@ -1084,7 +1084,37 @@ trait Applications extends Compatibility {
10841084 simpleApply(fun1, proto)
10851085 } {
10861086 (failedVal, failedState) =>
1087- def fail = { failedState.commit(); failedVal }
1087+ def fail =
1088+ insertedApplyNote()
1089+ failedState.commit()
1090+ failedVal
1091+
1092+ /** If the applied function is an automatically inserted `apply`
1093+ * method and one of its arguments has a type mismatch , append
1094+ * a note to the error message that explains where the required
1095+ * type comes from. See #19680 and associated test case.
1096+ */
1097+ def insertedApplyNote () =
1098+ if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
1099+ fun1 match
1100+ case Select (qualifier, _) =>
1101+ failedState.reporter.mapBufferedMessages:
1102+ case dia : Diagnostic .Error =>
1103+ dia.msg match
1104+ case msg : TypeMismatch =>
1105+ msg.inTree match
1106+ case Some (arg) if tree.args.exists(_.span == arg.span) =>
1107+ val Select (qualifier, _) = fun1 : @ unchecked
1108+ val noteText =
1109+ i """ The required type comes from a parameter of the automatically
1110+ |inserted `apply` method of ` ${qualifier.tpe}`,
1111+ |which is the type of ` ${qualifier.show}`. """ .stripMargin
1112+ Diagnostic .Error (msg.appendExplanation(" \n\n " + noteText), dia.pos)
1113+ case _ => dia
1114+ case msg => dia
1115+ case dia => dia
1116+ case _ => ()
1117+
10881118 // Try once with original prototype and once (if different) with tupled one.
10891119 // The reason we need to try both is that the decision whether to use tupled
10901120 // or not was already taken but might have to be revised when an implicit
0 commit comments