Skip to content

Commit c8842f2

Browse files
Alex1005atgodzik
authored andcommitted
Fix scala#20335: Try extension or implicit conversion for arguments with type mismatch error
[Cherry-picked 0632096]
1 parent 708d541 commit c8842f2

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ trait ShowMatchTrace(tps: Type*)(using Context) extends Message:
6060
override def msgPostscript(using Context): String =
6161
super.msgPostscript ++ matchReductionAddendum(tps*)
6262

63-
abstract class TypeMismatchMsg(found: Type, expected: Type)(errorId: ErrorMessageID)(using Context)
63+
abstract class TypeMismatchMsg(found: Type, val expected: Type)(errorId: ErrorMessageID)(using Context)
6464
extends Message(errorId), ShowMatchTrace(found, expected):
6565
def kind = MessageKind.TypeMismatch
6666
def explain(using Context) = err.whyNoMatchStr(found, expected)

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,25 @@ object ProtoTypes {
391391
* - t2 is a ascription (t22: T) and t1 is at the outside of t22
392392
* - t2 is a closure (...) => t22 and t1 is at the outside of t22
393393
*/
394-
def hasInnerErrors(t: Tree)(using Context): Boolean = t match
395-
case Typed(expr, tpe) => hasInnerErrors(expr)
396-
case closureDef(mdef) => hasInnerErrors(mdef.rhs)
394+
def hasInnerErrors(t: Tree, argType: Option[Type])(using Context): Boolean = t match
395+
case Typed(expr, tpe) => hasInnerErrors(expr, argType)
396+
case closureDef(mdef) => hasInnerErrors(mdef.rhs, argType)
397397
case _ =>
398398
t.existsSubTree { t1 =>
399399
if t1.typeOpt.isError
400400
&& t.span.toSynthetic != t1.span.toSynthetic
401401
&& t.typeOpt != t1.typeOpt then
402402
typr.println(i"error subtree $t1 of $t with ${t1.typeOpt}, spans = ${t1.span}, ${t.span}")
403-
true
403+
t1.typeOpt match
404+
case errorType: ErrorType if errorType.msg.isInstanceOf[TypeMismatchMsg] =>
405+
val typeMismtachMsg = errorType.msg.asInstanceOf[TypeMismatchMsg]
406+
!argType.contains(typeMismtachMsg.expected)
407+
case _ => true
404408
else
405409
false
406410
}
407411

408-
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean)(using Context): Tree = {
412+
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Option[Type])(using Context): Tree = {
409413
var targ = state.typedArg(arg)
410414
if (targ == null)
411415
untpd.functionWithUnknownParamType(arg) match {
@@ -423,7 +427,7 @@ object ProtoTypes {
423427
targ = typerFn(arg)
424428
// TODO: investigate why flow typing is not working on `targ`
425429
if ctx.reporter.hasUnreportedErrors then
426-
if hasInnerErrors(targ.nn) then
430+
if hasInnerErrors(targ.nn, argType) then
427431
state.errorArgs += arg
428432
else
429433
state.typedArg = state.typedArg.updated(arg, targ.nn)
@@ -451,7 +455,7 @@ object ProtoTypes {
451455
val protoTyperState = ctx.typerState
452456
val oldConstraint = protoTyperState.constraint
453457
val args1 = args.mapWithIndexConserve((arg, idx) =>
454-
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false))
458+
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, None))
455459
val newConstraint = protoTyperState.constraint
456460

457461
if !args1.exists(arg => isUndefined(arg.tpe)) then state.typedArgs = args1
@@ -498,7 +502,8 @@ object ProtoTypes {
498502
val locked = ctx.typerState.ownedVars
499503
val targ = cacheTypedArg(arg,
500504
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
501-
force = true)
505+
force = true,
506+
Some(wideFormal))
502507
val targ1 = typer.adapt(targ, wideFormal, locked)
503508
if wideFormal eq formal then targ1
504509
else checkNoWildcardCaptureForCBN(targ1)

0 commit comments

Comments
 (0)