File tree 3 files changed +32
-3
lines changed
compiler/test/dotty/tools/backend/jvm
3 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,30 @@ class InlineBytecodeTests extends DottyBytecodeTest {
104
104
}
105
105
}
106
106
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
+
107
131
@ Test def i4947 = {
108
132
val source = """ class Foo {
109
133
| 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 {
79
79
*
80
80
* Note that `.nn` performs a checked cast, so if invoked on a null value it'll throw an NPE.
81
81
*/
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)
85
84
}
Original file line number Diff line number Diff line change @@ -10,4 +10,10 @@ object Scala3RunTime:
10
10
def assertFailed (): Nothing =
11
11
throw new java.lang.AssertionError (" assertion failed" )
12
12
13
+ // Called by the inline extension def nn. Extract to minimize the bytecode size at call site.
14
+
15
+ def nn [T ](x : T | Null ): x.type & T =
16
+ if (x == null ) throw new NullPointerException (" tried to cast away nullability, but value is null" )
17
+ else x.asInstanceOf [x.type & T ]
18
+
13
19
end Scala3RunTime
You can’t perform that action at this time.
0 commit comments