Skip to content

Fix #20335: Try extensions for arguments with type mismatch error #23212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ trait ShowMatchTrace(tps: Type*)(using Context) extends Message:
override def msgPostscript(using Context): String =
super.msgPostscript ++ matchReductionAddendum(tps*)

abstract class TypeMismatchMsg(found: Type, expected: Type)(errorId: ErrorMessageID)(using Context)
abstract class TypeMismatchMsg(found: Type, val expected: Type)(errorId: ErrorMessageID)(using Context)
extends Message(errorId), ShowMatchTrace(found, expected):
def kind = MessageKind.TypeMismatch
def explain(using Context) = err.whyNoMatchStr(found, expected)
Expand Down
24 changes: 16 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,28 @@ object ProtoTypes {
* - t2 is a ascription (t22: T) and t1 is at the outside of t22
* - t2 is a closure (...) => t22 and t1 is at the outside of t22
*/
def hasInnerErrors(t: Tree)(using Context): Boolean = t match
case Typed(expr, tpe) => hasInnerErrors(expr)
case closureDef(mdef) => hasInnerErrors(mdef.rhs)
def hasInnerErrors(t: Tree, argType: Type)(using Context): Boolean = t match
case Typed(expr, tpe) => hasInnerErrors(expr, argType)
case closureDef(mdef) => hasInnerErrors(mdef.rhs, argType)
case _ =>
t.existsSubTree { t1 =>
if t1.typeOpt.isError
&& t.span.toSynthetic != t1.span.toSynthetic
&& t.typeOpt != t1.typeOpt then
typr.println(i"error subtree $t1 of $t with ${t1.typeOpt}, spans = ${t1.span}, ${t.span}")
true
t1.typeOpt match
case errorType: ErrorType if errorType.msg.isInstanceOf[TypeMismatchMsg] =>
// if error is caused by an argument type mismatch,
// then return false to try to find an extension.
// see i20335.scala for test case.
val typeMismtachMsg = errorType.msg.asInstanceOf[TypeMismatchMsg]
argType != typeMismtachMsg.expected
case _ => true
else
false
}

private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean)(using Context): Tree = {
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Type)(using Context): Tree = {
var targ = state.typedArg(arg)
if (targ == null)
untpd.functionWithUnknownParamType(arg) match {
Expand All @@ -459,7 +466,7 @@ object ProtoTypes {
targ = typerFn(arg)
// TODO: investigate why flow typing is not working on `targ`
if ctx.reporter.hasUnreportedErrors then
if hasInnerErrors(targ.nn) then
if hasInnerErrors(targ.nn, argType) then
state.errorArgs += arg
else
state.typedArg = state.typedArg.updated(arg, targ.nn)
Expand Down Expand Up @@ -487,7 +494,7 @@ object ProtoTypes {
val protoTyperState = ctx.typerState
val oldConstraint = protoTyperState.constraint
val args1 = args.mapWithIndexConserve((arg, idx) =>
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false))
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, NoType))
val newConstraint = protoTyperState.constraint

if !args1.exists(arg => isUndefined(arg.tpe)) then state.typedArgs = args1
Expand Down Expand Up @@ -534,7 +541,8 @@ object ProtoTypes {
val locked = ctx.typerState.ownedVars
val targ = cacheTypedArg(arg,
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
force = true)
force = true,
wideFormal)
val targ1 = typer.adapt(targ, wideFormal, locked)
if wideFormal eq formal then targ1
else checkNoWildcardCaptureForCBN(targ1)
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i20335.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.time.OffsetDateTime
import scala.concurrent.duration.*

val dateTime: OffsetDateTime = OffsetDateTime.now()

implicit class DateTimeOps(val dateTime: OffsetDateTime) {
def plus(amount: FiniteDuration): OffsetDateTime =
dateTime
}

def test = {
dateTime.plus(Duration.Zero)
dateTime.plus(if (true) Duration.Zero else Duration.Zero)
}
Loading