Skip to content

Commit 2788c99

Browse files
authored
Merge pull request #10280 from dotty-staging/fix-#10116
Fix #10116: Fix adaptConstant to that it survives Ycheck
2 parents 3a0c38a + 2be0eca commit 2788c99

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ object ConstFold:
5959
tree.withFoldedType(Constant(targ.tpe))
6060
case _ => tree
6161

62-
/** If tree is a constant value that can be converted to type `pt`, perform
63-
* the conversion.
64-
*/
65-
def apply[T <: Tree](tree: T, pt: Type)(using Context): T =
66-
val tree1 = apply(tree)
67-
tree.tpe.widenTermRefExpr.normalized match
68-
case ConstantType(x) => tree1.withFoldedType(x.convertTo(pt))
69-
case _ => tree1
70-
7162
extension [T <: Tree](tree: T)(using Context)
7263
private def withFoldedType(c: Constant | Null): T =
7364
if c == null then tree else tree.withType(ConstantType(c)).asInstanceOf[T]

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,9 +3423,12 @@ class Typer extends Namer
34233423

34243424
def adaptToSubType(wtp: Type): Tree = {
34253425
// try converting a constant to the target type
3426-
val folded = ConstFold(tree, pt)
3427-
if (folded ne tree)
3428-
return adaptConstant(folded, folded.tpe.asInstanceOf[ConstantType])
3426+
ConstFold(tree).tpe.widenTermRefExpr.normalized match
3427+
case ConstantType(x) =>
3428+
val converted = x.convertTo(pt)
3429+
if converted != null && (converted ne x) then
3430+
return adaptConstant(tree, ConstantType(converted))
3431+
case _ =>
34293432

34303433
val captured = captureWildcards(wtp)
34313434
if (captured `ne` wtp)

tests/pos/i10116.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object OverloadedWithLong {
2+
def overloaded(x: Long): Any =
3+
x
4+
5+
def overloaded(x: Any): Unit =
6+
???
7+
}
8+
9+
object Test {
10+
def main(args: Array[String]): Unit =
11+
import OverloadedWithLong._
12+
13+
val l: Any = 0 :: Nil
14+
val r = overloaded(l match {
15+
case x :: xs => 5
16+
})
17+
assert(r == 5L)
18+
}

0 commit comments

Comments
 (0)