Skip to content

Commit 8d15906

Browse files
committed
Add null-check to <id>Show method
Previously if user code was equal to null, then the accompanying show method would in effect call `null.show` which would crash the repl in a mysterious way.
1 parent 2fd10d9 commit 8d15906

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

repl/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotc.ast.{ untpd, tpd }
88
import dotc.{ Run, CompilationUnit, Compiler }
99
import dotc.core.Decorators._, dotc.core.Flags._, dotc.core.Phases
1010
import dotc.core.Names._, dotc.core.Contexts._, dotc.core.StdNames._
11+
import dotc.core.Constants.Constant
1112
import dotc.util.SourceFile
1213
import dotc.typer.{ ImportInfo, FrontEnd }
1314
import backend.jvm.GenBCode
@@ -68,7 +69,12 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
6869
def createShow(name: TermName, pos: Position) = {
6970
val showName = name ++ "Show"
7071
val select = Select(Ident(name), "show".toTermName)
71-
DefDef(showName, Nil, Nil, TypeTree(), select).withFlags(Synthetic).withPos(pos)
72+
val showWithNullCheck = If(
73+
Parens(InfixOp(Ident(name),Ident(nme.EQ), Literal(Constant(null)))),
74+
Literal(Constant("null")),
75+
select
76+
)
77+
DefDef(showName, Nil, Nil, TypeTree(), showWithNullCheck).withFlags(Synthetic).withPos(pos)
7278
}
7379

7480
val (exps, other) = trees.partition(_.isTerm)

repl/test-resources/repl/renderNull

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scala> val x: String = null
2+
val x: String = null

0 commit comments

Comments
 (0)