Skip to content

Commit cb4f807

Browse files
authored
Merge pull request #15671 from dotty-staging/fix-15666
Fix computation of class nesting level in inliner
2 parents c8ddbc0 + 0103917 commit cb4f807

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/inlines/Inliner.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,15 @@ class Inliner(val call: tpd.Tree)(using Context):
270270
assert(argss.isEmpty)
271271
true
272272

273+
/** The number of enclosing classes of this class, plus one */
274+
private def classNestingLevel(cls: Symbol) = cls.ownersIterator.count(_.isClass)
275+
273276
// Compute val-definitions for all this-proxies and append them to `bindingsBuf`
274277
private def computeThisBindings() = {
275278
// All needed this-proxies, paired-with and sorted-by nesting depth of
276279
// the classes they represent (innermost first)
277280
val sortedProxies = thisProxy.toList
278-
.map((cls, proxy) => (cls.ownersIterator.length, proxy.symbol, cls))
281+
.map((cls, proxy) => (classNestingLevel(cls), proxy.symbol, cls))
279282
.sortBy(-_._1)
280283

281284
def outerSelect(prefix: Tree, prefixCls: Symbol, hops: Int, info: Type) =
@@ -303,7 +306,7 @@ class Inliner(val call: tpd.Tree)(using Context):
303306
val pre = inlineCallPrefix match
304307
case Super(qual, _) => qual
305308
case pre => pre
306-
val preLevel = inlinedMethod.owner.ownersIterator.length
309+
val preLevel = classNestingLevel(inlinedMethod.owner)
307310
if preLevel > level then outerSelect(pre, inlinedMethod.owner, preLevel - level, selfSym.info)
308311
else pre
309312

Diff for: tests/pos/i15666.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait GetInt {
2+
def value: Int // if we add inline here, the program compiles
3+
}
4+
5+
class Newtype {
6+
def f: Int = ???
7+
8+
val g = new GetInt {
9+
inline def value: Int = f // has to be inline to crash
10+
}
11+
}

0 commit comments

Comments
 (0)