Skip to content

Commit 7650d0a

Browse files
Alex1005atgodzik
authored andcommitted
add test case for scala#20335
[Cherry-picked 6fcab69]
1 parent c8842f2 commit 7650d0a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ 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, argType: Option[Type])(using Context): Boolean = t match
394+
def hasInnerErrors(t: Tree, argType: Type)(using Context): Boolean = t match
395395
case Typed(expr, tpe) => hasInnerErrors(expr, argType)
396396
case closureDef(mdef) => hasInnerErrors(mdef.rhs, argType)
397397
case _ =>
@@ -402,14 +402,17 @@ object ProtoTypes {
402402
typr.println(i"error subtree $t1 of $t with ${t1.typeOpt}, spans = ${t1.span}, ${t.span}")
403403
t1.typeOpt match
404404
case errorType: ErrorType if errorType.msg.isInstanceOf[TypeMismatchMsg] =>
405+
// if error is caused by an argument type mismatch,
406+
// then return false to try to find an extension.
407+
// see i20335.scala for test case.
405408
val typeMismtachMsg = errorType.msg.asInstanceOf[TypeMismatchMsg]
406-
!argType.contains(typeMismtachMsg.expected)
409+
argType != typeMismtachMsg.expected
407410
case _ => true
408411
else
409412
false
410413
}
411414

412-
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Option[Type])(using Context): Tree = {
415+
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Type)(using Context): Tree = {
413416
var targ = state.typedArg(arg)
414417
if (targ == null)
415418
untpd.functionWithUnknownParamType(arg) match {
@@ -455,7 +458,7 @@ object ProtoTypes {
455458
val protoTyperState = ctx.typerState
456459
val oldConstraint = protoTyperState.constraint
457460
val args1 = args.mapWithIndexConserve((arg, idx) =>
458-
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, None))
461+
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, NoType))
459462
val newConstraint = protoTyperState.constraint
460463

461464
if !args1.exists(arg => isUndefined(arg.tpe)) then state.typedArgs = args1
@@ -503,7 +506,7 @@ object ProtoTypes {
503506
val targ = cacheTypedArg(arg,
504507
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
505508
force = true,
506-
Some(wideFormal))
509+
wideFormal)
507510
val targ1 = typer.adapt(targ, wideFormal, locked)
508511
if wideFormal eq formal then targ1
509512
else checkNoWildcardCaptureForCBN(targ1)

tests/pos/i20335.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.time.OffsetDateTime
2+
import scala.concurrent.duration.*
3+
4+
val dateTime: OffsetDateTime = OffsetDateTime.now()
5+
6+
implicit class DateTimeOps(val dateTime: OffsetDateTime) {
7+
def plus(amount: FiniteDuration): OffsetDateTime =
8+
dateTime
9+
}
10+
11+
def test = {
12+
dateTime.plus(Duration.Zero)
13+
dateTime.plus(if (true) Duration.Zero else Duration.Zero)
14+
}

0 commit comments

Comments
 (0)