File tree Expand file tree Collapse file tree 3 files changed +34
-3
lines changed
compiler/test/dotty/tools/backend/jvm Expand file tree Collapse file tree 3 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,30 @@ class InlineBytecodeTests extends DottyBytecodeTest {
104104 }
105105 }
106106
107+ @ Test def inlineNn = {
108+ val source =
109+ s """
110+ |class Foo {
111+ | def meth1(x: Int | Null): Int = x.nn
112+ | def meth2(x: Int | Null): Int = scala.runtime.Scala3RunTime.nn(x)
113+ |}
114+ """ .stripMargin
115+
116+ checkBCode(source) { dir =>
117+ val clsIn = dir.lookupName(" Foo.class" , directory = false ).input
118+ val clsNode = loadClassNode(clsIn)
119+ val meth1 = getMethod(clsNode, " meth1" )
120+ val meth2 = getMethod(clsNode, " meth2" )
121+
122+ val instructions1 = instructionsFromMethod(meth1)
123+ val instructions2 = instructionsFromMethod(meth2)
124+
125+ assert(instructions1 == instructions2,
126+ " `nn` was not properly inlined in `meth1`\n " +
127+ diffInstructions(instructions1, instructions2))
128+ }
129+ }
130+
107131 @ Test def i4947 = {
108132 val source = """ class Foo {
109133 | transparent inline def track[T](inline f: T): T = {
Original file line number Diff line number Diff line change @@ -79,7 +79,6 @@ object DottyPredef {
7979 *
8080 * Note that `.nn` performs a checked cast, so if invoked on a null value it'll throw an NPE.
8181 */
82- extension [T ](x : T | Null ) def nn : x.type & T =
83- if (x == null ) throw new NullPointerException (" tried to cast away nullability, but value is null" )
84- else x.asInstanceOf [x.type & T ]
82+ extension [T ](x : T | Null ) inline def nn : x.type & T =
83+ scala.runtime.Scala3RunTime .nn(x)
8584}
Original file line number Diff line number Diff line change @@ -10,4 +10,12 @@ object Scala3RunTime:
1010 def assertFailed (): Nothing =
1111 throw new java.lang.AssertionError (" assertion failed" )
1212
13+ /** Called by the inline extension def `nn`.
14+ *
15+ * Extracted to minimize the bytecode size at call site.
16+ */
17+ def nn [T ](x : T | Null ): x.type & T =
18+ if (x == null ) throw new NullPointerException (" tried to cast away nullability, but value is null" )
19+ else x.asInstanceOf [x.type & T ]
20+
1321end Scala3RunTime
You can’t perform that action at this time.
0 commit comments