Skip to content

Commit afd323d

Browse files
committed
fix: simplify types before checking if implicit is underspecified
1 parent b2850be commit afd323d

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ object Types extends TypeUtils {
229229

230230
def isAny(using Context): Boolean = isRef(defn.AnyClass, skipRefined = false)
231231
def isAnyRef(using Context): Boolean = isRef(defn.ObjectClass, skipRefined = false)
232+
def isAnyVal(using Context): Boolean = isRef(defn.AnyValClass, skipRefined = false)
232233
def isAnyKind(using Context): Boolean = isRef(defn.AnyKindClass, skipRefined = false)
233234

234235
def isTopType(using Context): Boolean = dealias match

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,15 +1668,14 @@ trait Implicits:
16681668
def isUnderSpecifiedArgument(tp: Type): Boolean =
16691669
tp.isRef(defn.NothingClass) || tp.isRef(defn.NullClass) || (tp eq NoPrefix)
16701670

1671-
private def isUnderspecified(tp: Type): Boolean = tp.stripTypeVar match
1671+
private def isUnderspecified(tp: Type): Boolean = tp.simplified match
16721672
case tp: WildcardType =>
16731673
!tp.optBounds.exists || isUnderspecified(tp.optBounds.hiBound)
16741674
case tp: ViewProto =>
16751675
isUnderspecified(tp.resType)
16761676
|| tp.resType.isRef(defn.UnitClass)
16771677
|| isUnderSpecifiedArgument(tp.argType.widen)
1678-
case _ =>
1679-
tp.isAny || tp.isAnyRef
1678+
case tp => tp.isAny || tp.isAnyRef || tp.isAnyVal
16801679

16811680
/** Search implicit in context `ctxImplicits` or else in implicit scope
16821681
* of expected type if `ctxImplicits == null`.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,10 +4952,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
49524952
case _ if ctx.mode.is(Mode.ImplicitsEnabled) && tree.tpe.isValueType =>
49534953
if tree.tpe.isNamedTupleType && pt.derivesFrom(defn.TupleClass) then
49544954
readapt(typed(untpd.Select(untpd.TypedSplice(tree), nme.toTuple)))
4955-
else if pt.isRef(defn.AnyValClass, skipRefined = false)
4956-
|| pt.isRef(defn.ObjectClass, skipRefined = false)
4957-
then
4958-
recover(TooUnspecific(pt))
49594955
else inferView(tree, pt) match
49604956
case SearchSuccess(found, _, _, isExtension) =>
49614957
if isExtension then found

tests/neg/i21304.check

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i21304.scala:5:6 --------------------------------------------------------------
2+
5 | foo(10) // error
3+
| ^^
4+
| Found: (10 : Int)
5+
| Required: AnyRef | Null
6+
| Note that implicit conversions were not tried because the result of an implicit conversion
7+
| must be more specific than AnyRef | Null
8+
|
9+
| One of the following imports might fix the problem:
10+
|
11+
| import scala.math.BigDecimal.int2bigDecimal
12+
| import scala.math.BigInt.int2bigInt
13+
| import scala.math.Numeric.IntIsIntegral.mkNumericOps
14+
| import scala.math.Numeric.IntIsIntegral.mkOrderingOps
15+
| import scala.math.Ordering.Int.mkOrderingOps
16+
| import scala.math.Integral.Implicits.infixIntegralOps
17+
| import scala.math.Ordered.orderingToOrdered
18+
| import scala.math.Ordering.Implicits.infixOrderingOps
19+
| import scala.math.Numeric.Implicits.infixNumericOps
20+
| import scala.reflect.Selectable.reflectiveSelectable
21+
|
22+
|
23+
| longer explanation available when compiling with `-explain`

tests/neg/i21304.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo(x: AnyRef | Null): Unit =
2+
println(x)
3+
4+
@main def main(): Unit =
5+
foo(10) // error

tests/neg/implicits-numeric.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- [E172] Type Error: tests/neg/implicits-numeric.scala:6:28 -----------------------------------------------------------
2+
6 | println(implicitly[AnyVal]) // error
3+
| ^
4+
| No implicit search was attempted for parameter e of method implicitly in object Predef
5+
| since the expected type AnyVal is not specific enough

tests/run/implicits-numeric.scala renamed to tests/neg/implicits-numeric.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ object Test extends App {
33
implicit def _1: Long = 1L
44
implicit def _2: Int = 0
55

6-
println(implicitly[AnyVal])
6+
println(implicitly[AnyVal]) // error
77
}

0 commit comments

Comments
 (0)