@@ -1037,6 +1037,33 @@ trait Applications extends Compatibility {
1037
1037
}
1038
1038
}
1039
1039
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
+
1040
1067
fun1.tpe match {
1041
1068
case err : ErrorType => cpy.Apply (tree)(fun1, proto.typedArgs()).withType(err)
1042
1069
case TryDynamicCallType =>
@@ -1097,7 +1124,11 @@ trait Applications extends Compatibility {
1097
1124
simpleApply(fun1, proto)
1098
1125
} {
1099
1126
(failedVal, failedState) =>
1100
- def fail = { failedState.commit(); failedVal }
1127
+ def fail =
1128
+ maybeAddInsertedApplyNote(failedState, fun1)
1129
+ failedState.commit()
1130
+ failedVal
1131
+
1101
1132
// Try once with original prototype and once (if different) with tupled one.
1102
1133
// The reason we need to try both is that the decision whether to use tupled
1103
1134
// or not was already taken but might have to be revised when an implicit
0 commit comments