Skip to content

Commit 03511d1

Browse files
committed
Fix #3082: Don't generate accessors for local inline methods
1 parent af78a4c commit 03511d1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ object Inliner {
3939
*/
4040
private def makeInlineable(tree: Tree)(implicit ctx: Context) = {
4141

42+
val inlineMethod = ctx.owner
43+
4244
/** A tree map which inserts accessors for all non-public term members accessed
4345
* from inlined code. Accessors are collected in the `accessors` buffer.
4446
*/
4547
object addAccessors extends TreeMap {
46-
val inlineMethod = ctx.owner
4748
val accessors = new mutable.ListBuffer[MemberDef]
4849

4950
/** A definition needs an accessor if it is private, protected, or qualified private
@@ -180,8 +181,14 @@ object Inliner {
180181
}
181182
}
182183

183-
val tree1 = addAccessors.transform(tree)
184-
flatTree(tree1 :: addAccessors.accessors.toList)
184+
if (inlineMethod.owner.isTerm)
185+
// Inline methods in local scopes can only be called in the scope they are defined,
186+
// so no accessors are needed for them.
187+
tree
188+
else {
189+
val tree1 = addAccessors.transform(tree)
190+
flatTree(tree1 :: addAccessors.accessors.toList)
191+
}
185192
}
186193

187194
/** Register inline info for given inline method `sym`.

tests/pos/i3082.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
private def foo(arg1: Int): Int = {
3+
inline def bar: Int = foo(0)
4+
if (arg1 == 0) 0 else bar
5+
}
6+
assert(foo(11) == 0)
7+
}

0 commit comments

Comments
 (0)