Skip to content

Commit 6fcab69

Browse files
committed
add test case for scala#20335
1 parent 0632096 commit 6fcab69

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
@@ -427,7 +427,7 @@ object ProtoTypes {
427427
* - t2 is a ascription (t22: T) and t1 is at the outside of t22
428428
* - t2 is a closure (...) => t22 and t1 is at the outside of t22
429429
*/
430-
def hasInnerErrors(t: Tree, argType: Option[Type])(using Context): Boolean = t match
430+
def hasInnerErrors(t: Tree, argType: Type)(using Context): Boolean = t match
431431
case Typed(expr, tpe) => hasInnerErrors(expr, argType)
432432
case closureDef(mdef) => hasInnerErrors(mdef.rhs, argType)
433433
case _ =>
@@ -438,14 +438,17 @@ object ProtoTypes {
438438
typr.println(i"error subtree $t1 of $t with ${t1.typeOpt}, spans = ${t1.span}, ${t.span}")
439439
t1.typeOpt match
440440
case errorType: ErrorType if errorType.msg.isInstanceOf[TypeMismatchMsg] =>
441+
// if error is caused by an argument type mismatch,
442+
// then return false to try to find an extension.
443+
// see i20335.scala for test case.
441444
val typeMismtachMsg = errorType.msg.asInstanceOf[TypeMismatchMsg]
442-
!argType.contains(typeMismtachMsg.expected)
445+
argType != typeMismtachMsg.expected
443446
case _ => true
444447
else
445448
false
446449
}
447450

448-
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Option[Type])(using Context): Tree = {
451+
private def cacheTypedArg(arg: untpd.Tree, typerFn: untpd.Tree => Tree, force: Boolean, argType: Type)(using Context): Tree = {
449452
var targ = state.typedArg(arg)
450453
if (targ == null)
451454
untpd.functionWithUnknownParamType(arg) match {
@@ -491,7 +494,7 @@ object ProtoTypes {
491494
val protoTyperState = ctx.typerState
492495
val oldConstraint = protoTyperState.constraint
493496
val args1 = args.mapWithIndexConserve((arg, idx) =>
494-
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, None))
497+
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false, NoType))
495498
val newConstraint = protoTyperState.constraint
496499

497500
if !args1.exists(arg => isUndefined(arg.tpe)) then state.typedArgs = args1
@@ -539,7 +542,7 @@ object ProtoTypes {
539542
val targ = cacheTypedArg(arg,
540543
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
541544
force = true,
542-
Some(wideFormal))
545+
wideFormal)
543546
val targ1 = typer.adapt(targ, wideFormal, locked)
544547
if wideFormal eq formal then targ1
545548
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)