diff --git a/compiler/src/dotty/tools/dotc/core/Names.scala b/compiler/src/dotty/tools/dotc/core/Names.scala index cd58977f9dda..9799418484d4 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -146,7 +146,9 @@ object Names { def startsWith(str: String, start: Int = 0): Boolean = firstPart.startsWith(str, start) /** Does (the last part of) this name end with `str`? */ - def endsWith(str: String): Boolean = lastPart.endsWith(str) + def endsWith(suffix: String): Boolean = lastPart.endsWith(suffix) + + def endsWith(suffix: SimpleName): Boolean = lastPart.endsWith(suffix) override def hashCode: Int = System.identityHashCode(this) override def equals(that: Any): Boolean = this eq that.asInstanceOf[AnyRef] @@ -363,11 +365,15 @@ object Names { i == str.length } - override def endsWith(str: String): Boolean = { + override def endsWith(suffix: String): Boolean = var i = 1 - while (i <= str.length && i <= length && apply(length - i) == str(str.length - i)) i += 1 - i > str.length - } + while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1 + i > suffix.length + + override def endsWith(suffix: SimpleName): Boolean = + var i = 1 + while i <= suffix.length && i <= length && apply(length - i) == suffix(suffix.length - i) do i += 1 + i > suffix.length override def replace(from: Char, to: Char): SimpleName = { val cs = new Array[Char](length) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 405e450a4691..b1df2c376bda 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -215,8 +215,11 @@ class Typer extends Namer def namedImportRef(imp: ImportInfo)(using Context): Type = { val termName = name.toTermName - def adjustExtension(name: Name) = - if required.is(ExtensionMethod) then name.toExtensionName else name + def adjustExtension(n: Name) = + if required.is(ExtensionMethod) && termName.endsWith(n.lastPart) + // pre-check to avoid forming a new string; form extension only if it has a chance of matching `termName` + then n.toExtensionName + else n def recur(selectors: List[untpd.ImportSelector]): Type = selectors match case selector :: rest =>