File tree 2 files changed +61
-1
lines changed
compiler/test/dotty/tools/backend/jvm
2 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,37 @@ class InlineBytecodeTests extends DottyBytecodeTest {
73
73
}
74
74
}
75
75
76
+ @ Test def inlineLocally = {
77
+ val source =
78
+ """
79
+ |class Foo {
80
+ | def meth1: Unit = locally {
81
+ | val a = 5
82
+ | a
83
+ | }
84
+ |
85
+ | def meth2: Unit = {
86
+ | val a = 5
87
+ | a
88
+ | }
89
+ |}
90
+ """ .stripMargin
91
+
92
+ checkBCode(source) { dir =>
93
+ val clsIn = dir.lookupName(" Foo.class" , directory = false ).input
94
+ val clsNode = loadClassNode(clsIn)
95
+ val meth1 = getMethod(clsNode, " meth1" )
96
+ val meth2 = getMethod(clsNode, " meth2" )
97
+
98
+ val instructions1 = instructionsFromMethod(meth1)
99
+ val instructions2 = instructionsFromMethod(meth2)
100
+
101
+ assert(instructions1 == instructions2,
102
+ " `locally` was not properly inlined in `meth1`\n " +
103
+ diffInstructions(instructions1, instructions2))
104
+ }
105
+ }
106
+
76
107
@ Test def i4947 = {
77
108
val source = """ class Foo {
78
109
| transparent inline def track[T](inline f: T): T = {
Original file line number Diff line number Diff line change @@ -18,7 +18,36 @@ object DottyPredef {
18
18
19
19
inline final def implicitly [T ](implicit ev : T ): T = ev
20
20
21
- inline def locally [T ](body : => T ): T = body
21
+ /** Used to mark code blocks as being expressions, instead of being taken as part of anonymous classes and the like.
22
+ * This is just a different name for [[identity ]].
23
+ *
24
+ * @example Separating code blocks from `new`:
25
+ * {{{
26
+ * val x = new AnyRef
27
+ * {
28
+ * val y = ...
29
+ * println(y)
30
+ * }
31
+ * // the { ... } block is seen as the body of an anonymous class
32
+ *
33
+ * val x = new AnyRef
34
+ *
35
+ * {
36
+ * val y = ...
37
+ * println(y)
38
+ * }
39
+ * // an empty line is a brittle "fix"
40
+ *
41
+ * val x = new AnyRef
42
+ * locally {
43
+ * val y = ...
44
+ * println(y)
45
+ * }
46
+ * // locally guards the block and helps communicate intent
47
+ * }}}
48
+ * @group utilities
49
+ */
50
+ inline def locally [T ](inline body : T ): T = body
22
51
23
52
/**
24
53
* Retrieve the single value of a type with a unique inhabitant.
You can’t perform that action at this time.
0 commit comments