From 634fcd12976bbc2877c71b8a850253791907d5d7 Mon Sep 17 00:00:00 2001 From: Joel Wilsson Date: Fri, 30 Aug 2024 14:45:51 +0200 Subject: [PATCH] Re-use attachment in exportForwarders to handle ambiguous overloads exportForwarders can be called more than once for the same expression if there are ambiguous overloads. Just return the already computed ExportForwarders if that happens. Closes #21071 --- .../src/dotty/tools/dotc/typer/Namer.scala | 9 ++++---- tests/neg/i21071.check | 9 ++++++++ tests/neg/i21071.scala | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/neg/i21071.check create mode 100644 tests/neg/i21071.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 0a1a70b98bbb..6167db62fbe0 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -1452,10 +1452,11 @@ class Namer { typer: Typer => forwarders.derivedCons(forwarder2, avoidClashes(forwarders2)) case Nil => forwarders - addForwarders(selectors, Nil) - val forwarders = avoidClashes(buf.toList) - exp.pushAttachment(ExportForwarders, forwarders) - forwarders + exp.getAttachment(ExportForwarders).getOrElse: + addForwarders(selectors, Nil) + val forwarders = avoidClashes(buf.toList) + exp.pushAttachment(ExportForwarders, forwarders) + forwarders end exportForwarders /** Add forwarders as required by the export statements in this class */ diff --git a/tests/neg/i21071.check b/tests/neg/i21071.check new file mode 100644 index 000000000000..b2a3233a31c0 --- /dev/null +++ b/tests/neg/i21071.check @@ -0,0 +1,9 @@ +-- [E051] Reference Error: tests/neg/i21071.scala:9:2 ------------------------------------------------------------------ +9 | foo { // error + | ^^^ + | Ambiguous overload. The overloaded alternatives of method foo in object MySuite with types + | (a: String): Nothing + | (a: List[String]): Nothing + | both match arguments ((??? : => Nothing)) + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i21071.scala b/tests/neg/i21071.scala new file mode 100644 index 000000000000..ac222cad7936 --- /dev/null +++ b/tests/neg/i21071.scala @@ -0,0 +1,21 @@ +trait Service { + def method: String +} + +object MySuite { + def foo(a: List[String]) = ??? + def foo(a: String) = ??? + + foo { // error + + new Service { + private val underlying: Service = ??? + private val s = "foo" + + export underlying.* + export s.toLowerCase + } + + ??? + } +}