Skip to content

Commit d82b700

Browse files
authored
Merge pull request #9870 from dotty-staging/findref-tweak
Tweak performance of findRef
2 parents f4528ce + db92078 commit d82b700

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,16 @@ class Typer extends Namer
503503
// Convert a reference `f` to an extension method select `p.f`, where
504504
// `p` is the closest enclosing extension parameter, or else convert to `this.f`.
505505
val xmethod = ctx.owner.enclosingExtensionMethod
506-
val qualifier =
507-
if xmethod.exists then untpd.ref(xmethod.extensionParam.termRef)
508-
else untpd.This(untpd.EmptyTypeIdent)
509-
val selection = untpd.cpy.Select(tree)(qualifier, name)
510-
val result = tryEither(typed(selection, pt))((_, _) => fail)
511-
def canAccessUnqualified(sym: Symbol) =
512-
sym.is(ExtensionMethod) && (sym.extensionParam.span == xmethod.extensionParam.span)
513-
if !xmethod.exists || result.tpe.isError || canAccessUnqualified(result.symbol) then
514-
result
506+
if xmethod.exists then
507+
val qualifier = untpd.ref(xmethod.extensionParam.termRef)
508+
val selection = untpd.cpy.Select(tree)(qualifier, name)
509+
val result = tryEither(typed(selection, pt))((_, _) => fail)
510+
def canAccessUnqualified(sym: Symbol) =
511+
sym.is(ExtensionMethod) && (sym.extensionParam.span == xmethod.extensionParam.span)
512+
if result.tpe.isError || canAccessUnqualified(result.symbol) then
513+
result
514+
else
515+
fail
515516
else
516517
fail
517518
else

tests/bench/FindRef.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class MyInt(val x: Int) {
2+
def eq(that: MyInt): Boolean = this.x == that.x
3+
}
4+
5+
class Test {
6+
def foo(x: MyInt, y: MyInt): Boolean = x.eq(y)
7+
8+
val a = MyInt(2)
9+
val b = MyInt(3)
10+
foo(a, b)
11+
}

0 commit comments

Comments
 (0)