Skip to content

Fix #10116: Fix adaptConstant to that it survives Ycheck #10280

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

Merged
merged 2 commits into from
Nov 11, 2020
Merged
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
9 changes: 0 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ object ConstFold:
tree.withFoldedType(Constant(targ.tpe))
case _ => tree

/** If tree is a constant value that can be converted to type `pt`, perform
* the conversion.
*/
def apply[T <: Tree](tree: T, pt: Type)(using Context): T =
val tree1 = apply(tree)
tree.tpe.widenTermRefExpr.normalized match
case ConstantType(x) => tree1.withFoldedType(x.convertTo(pt))
case _ => tree1

extension [T <: Tree](tree: T)(using Context)
private def withFoldedType(c: Constant | Null): T =
if c == null then tree else tree.withType(ConstantType(c)).asInstanceOf[T]
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3423,9 +3423,12 @@ class Typer extends Namer

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

val captured = captureWildcards(wtp)
if (captured `ne` wtp)
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i10116.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
object OverloadedWithLong {
def overloaded(x: Long): Any =
x

def overloaded(x: Any): Unit =
???
}

object Test {
def main(args: Array[String]): Unit =
import OverloadedWithLong._

val l: Any = 0 :: Nil
val r = overloaded(l match {
case x :: xs => 5
})
assert(r == 5L)
}