Skip to content

Commit f0afb47

Browse files
Merge pull request #10396 from dotty-staging/dotty-predef-nn-inline
Make the extension method `nn` inline.
2 parents 97a6f30 + fe84848 commit f0afb47

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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 = {

library/src/dotty/DottyPredef.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

library/src/scala/runtime/Scala3RunTime.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
1321
end Scala3RunTime

0 commit comments

Comments
 (0)