@@ -25,17 +25,18 @@ import reporting.*
25
25
import Nullables .* , NullOpsDecorator .*
26
26
import config .{Feature , SourceVersion }
27
27
28
- import collection .mutable
28
+ import collection .mutable . ListBuffer
29
29
import config .Printers .{overload , typr , unapp }
30
+ import inlines .Inlines
31
+ import interfaces .Diagnostic .ERROR
30
32
import TypeApplications .*
31
33
import Annotations .Annotation
32
34
33
35
import Constants .{Constant , IntTag }
34
36
import Denotations .SingleDenotation
35
37
import annotation .threadUnsafe
36
38
37
- import scala .util .control .NonFatal
38
- import dotty .tools .dotc .inlines .Inlines
39
+ import scala .util .chaining .given
39
40
40
41
object Applications {
41
42
import tpd .*
@@ -260,7 +261,7 @@ object Applications {
260
261
261
262
end UnapplyArgs
262
263
263
- def wrapDefs (defs : mutable. ListBuffer [Tree ] | Null , tree : Tree )(using Context ): Tree =
264
+ def wrapDefs (defs : ListBuffer [Tree ] | Null , tree : Tree )(using Context ): Tree =
264
265
if (defs != null && defs.nonEmpty) tpd.Block (defs.toList, tree) else tree
265
266
266
267
/** Optionally, if `sym` is a symbol created by `resolveMapped`, i.e. representing
@@ -872,8 +873,8 @@ trait Applications extends Compatibility {
872
873
extends Application (methRef, fun.tpe, args, resultType) {
873
874
type TypedArg = Tree
874
875
def isVarArg (arg : Trees .Tree [T ]): Boolean = untpd.isWildcardStarArg(arg)
875
- private var typedArgBuf = new mutable. ListBuffer [Tree ]
876
- private var liftedDefs : mutable. ListBuffer [Tree ] | Null = null
876
+ private var typedArgBuf = ListBuffer .empty [Tree ]
877
+ private var liftedDefs : ListBuffer [Tree ] | Null = null
877
878
private var myNormalizedFun : Tree = fun
878
879
init()
879
880
@@ -911,11 +912,11 @@ trait Applications extends Compatibility {
911
912
912
913
override def liftFun (): Unit =
913
914
if (liftedDefs == null ) {
914
- liftedDefs = new mutable. ListBuffer [Tree ]
915
+ liftedDefs = ListBuffer .empty [Tree ]
915
916
myNormalizedFun = lifter.liftApp(liftedDefs.uncheckedNN, myNormalizedFun)
916
917
}
917
918
918
- /** The index of the first difference between lists of trees `xs` and `ys`
919
+ /** The index of the first difference between lists of trees `xs` and `ys`.
919
920
* -1 if there are no differences.
920
921
*/
921
922
private def firstDiff [T <: Trees .Tree [? ]](xs : List [T ], ys : List [T ], n : Int = 0 ): Int = xs match {
@@ -939,11 +940,25 @@ trait Applications extends Compatibility {
939
940
isPureExpr(arg)
940
941
|| arg.isInstanceOf [RefTree | Apply | TypeApply ] && arg.symbol.name.is(DefaultGetterName )
941
942
942
- val result : Tree = {
943
+ def defaultsAddendum (args : List [Tree ]): Unit =
944
+ def check (arg : Tree ): Boolean = arg match
945
+ case TypeApply (Select (_, name), _) => name.is(DefaultGetterName )
946
+ case Apply (Select (_, name), _) => name.is(DefaultGetterName )
947
+ case _ => false
948
+ val faulties = args.filter(check)
949
+ ctx.reporter.mapBufferedMessages:
950
+ case Diagnostic (msg : TypeMismatch , pos, ERROR )
951
+ if msg.inTree.exists(t => faulties.exists(_.span == t.span)) =>
952
+ val noteText = i " Error occurred in an application involving default arguments. "
953
+ val explained = i " Expanded application: ${cpy.Apply (app)(normalizedFun, args)}"
954
+ Diagnostic .Error (msg.append(s " \n $noteText" ).appendExplanation(s " \n\n $explained" ), pos)
955
+ case dia => dia
956
+
957
+ val result : Tree = {
943
958
var typedArgs = typedArgBuf.toList
944
959
def app0 = cpy.Apply (app)(normalizedFun, typedArgs) // needs to be a `def` because typedArgs can change later
945
960
val app1 =
946
- if ( ! success || typedArgs.exists(_.tpe.isError)) app0.withType(UnspecifiedErrorType )
961
+ if ! success || typedArgs.exists(_.tpe.isError).tap( if (_) then defaultsAddendum(typedArgs)) then app0.withType(UnspecifiedErrorType )
947
962
else {
948
963
if isJavaAnnotConstr(methRef.symbol) then
949
964
// #19951 Make sure all arguments are NamedArgs for Java annotations
@@ -960,7 +975,7 @@ trait Applications extends Compatibility {
960
975
liftFun()
961
976
962
977
// lift arguments in the definition order
963
- val argDefBuf = mutable. ListBuffer .empty[Tree ]
978
+ val argDefBuf = ListBuffer .empty[Tree ]
964
979
typedArgs = lifter.liftArgs(argDefBuf, methType, typedArgs)
965
980
// Lifted arguments ordered based on the original order of typedArgBuf and
966
981
// with all non-explicit default parameters at the end in declaration order.
@@ -1105,7 +1120,6 @@ trait Applications extends Compatibility {
1105
1120
if fun1.symbol.name == nme.apply && fun1.span.isSynthetic then
1106
1121
fun1 match
1107
1122
case Select (qualifier, _) =>
1108
- import dotty .tools .dotc .interfaces .Diagnostic .ERROR
1109
1123
failedState.reporter.mapBufferedMessages:
1110
1124
case Diagnostic (msg : TypeMismatch , pos, ERROR )
1111
1125
if msg.inTree.exists(t => tree.args.exists(_.span == t.span)) =>
@@ -1204,7 +1218,7 @@ trait Applications extends Compatibility {
1204
1218
val (lhs1, name, rhss) = (tree : @ unchecked) match
1205
1219
case Apply (Select (lhs, name), rhss) => (typedExpr(lhs), name, rhss)
1206
1220
case Apply (untpd.TypedSplice (Select (lhs1, name)), rhss) => (lhs1, name, rhss)
1207
- val liftedDefs = new mutable. ListBuffer [Tree ]
1221
+ val liftedDefs = ListBuffer .empty [Tree ]
1208
1222
val lhs2 = untpd.TypedSplice (LiftComplex .liftAssigned(liftedDefs, lhs1))
1209
1223
val assign = untpd.Assign (lhs2,
1210
1224
untpd.Apply (untpd.Select (lhs2, name.asSimpleName.dropRight(1 )), rhss))
0 commit comments