Skip to content
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

Fix beta-reduction with Nothing and null args #16938

Merged
merged 1 commit into from
Feb 21, 2023
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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ object BetaReduce:
ref.symbol
case _ =>
val flags = Synthetic | (param.symbol.flags & Erased)
val tpe = if arg.tpe.dealias.isInstanceOf[ConstantType] then arg.tpe.dealias else arg.tpe.widen
val tpe =
if arg.tpe.isBottomType then param.tpe.widenTermRefExpr
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why we use widenTermRefExpr here (and not widen for example), just curious.

Copy link
Contributor Author

@nicolasstucki nicolasstucki Feb 21, 2023

Choose a reason for hiding this comment

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

To keep the type precise. For example, if we have a singleton String type we do not want to widen it to String. For example ((x: "foo") => x: "foo").apply(???) should become val x: "foo" = ???; x: "foo" and not val x: String = ???; x: "foo". The second one would not type check after beta-reduction.

else if arg.tpe.dealias.isInstanceOf[ConstantType] then arg.tpe.dealias
else arg.tpe.widen
val binding = ValDef(newSymbol(ctx.owner, param.name, flags, tpe, coord = arg.span), arg).withSpan(arg.span)
if !(tpe.isInstanceOf[ConstantType] && isPureExpr(arg)) then
bindings += binding
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i15165.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test1 = { (y: Int) => y + 1 }.apply(???)

class C:
def x: Int = 8

def test2 = { (c: C) => c.x }.apply(null)