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 1 commit
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3425,7 +3425,7 @@ class Typer extends Namer
// try converting a constant to the target type
val folded = ConstFold(tree, pt)
if (folded ne tree)
return adaptConstant(folded, folded.tpe.asInstanceOf[ConstantType])
return adaptConstant(tree, folded.tpe.asInstanceOf[ConstantType])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum. Looking at this fix and at ConstFold.apply(tree, pt), I think the latter basically produces an invalid tree, that is never safe to reuse.

I also see that ConstFold.Apply(tree, pt) is used only here. With the fix, we are therefore patching the invalid result of ConstFold.apply at its only call site; but if someone starts calling that overload somewhere else, they will likely reintroduce a similar bug.

Consider inlining the contents of that overload of ConstFold(tree, tp) here instead. Once you do that, you won't have to create that transient invalid tree, since you'll be able to directly use x.convertTo(tp) instead of folded.tpe.

This would prevent further invalid uses of that overload.


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)
}