diff --git a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala index 153ceaaee9e9..bea42e82ce6f 100644 --- a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala @@ -270,12 +270,15 @@ class Inliner(val call: tpd.Tree)(using Context): assert(argss.isEmpty) true + /** The number of enclosing classes of this class, plus one */ + private def classNestingLevel(cls: Symbol) = cls.ownersIterator.count(_.isClass) + // Compute val-definitions for all this-proxies and append them to `bindingsBuf` private def computeThisBindings() = { // All needed this-proxies, paired-with and sorted-by nesting depth of // the classes they represent (innermost first) val sortedProxies = thisProxy.toList - .map((cls, proxy) => (cls.ownersIterator.length, proxy.symbol, cls)) + .map((cls, proxy) => (classNestingLevel(cls), proxy.symbol, cls)) .sortBy(-_._1) def outerSelect(prefix: Tree, prefixCls: Symbol, hops: Int, info: Type) = @@ -303,7 +306,7 @@ class Inliner(val call: tpd.Tree)(using Context): val pre = inlineCallPrefix match case Super(qual, _) => qual case pre => pre - val preLevel = inlinedMethod.owner.ownersIterator.length + val preLevel = classNestingLevel(inlinedMethod.owner) if preLevel > level then outerSelect(pre, inlinedMethod.owner, preLevel - level, selfSym.info) else pre diff --git a/tests/pos/i15666.scala b/tests/pos/i15666.scala new file mode 100644 index 000000000000..ad7314201565 --- /dev/null +++ b/tests/pos/i15666.scala @@ -0,0 +1,11 @@ +trait GetInt { + def value: Int // if we add inline here, the program compiles +} + +class Newtype { + def f: Int = ??? + + val g = new GetInt { + inline def value: Int = f // has to be inline to crash + } +} \ No newline at end of file