From 6e1761de0cce5dd806984fa0f9710b9ded1d2bd5 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Tue, 31 Mar 2020 13:57:17 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20suggest=20to=20import=20things?= =?UTF-8?q?=20that=20are=20already=20available?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #8051 --- .../tools/dotc/typer/ImportSuggestions.scala | 15 ++++++++++++++- tests/neg/missing-implicit3.check | 5 ----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala b/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala index 78dc5c43a9ff..99ef281172b4 100644 --- a/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala +++ b/compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala @@ -144,6 +144,17 @@ trait ImportSuggestions: val timer = new Timer() val deadLine = System.currentTimeMillis() + suggestImplicitTimeOut + // Candidates that are already available without explicit import because they + // are already provided by the context (imported or inherited) or because they + // are in the implicit scope of `pt`. + val alreadyAvailableCandidates: Set[Symbol] = { + val wildProto = wildApprox(pt) + val contextualCandidates = ctx.implicits.eligible(wildProto) + val implicitScopeCandidates = ctx.run.implicitScope(wildProto, ctx).eligible + val allCandidates = contextualCandidates ++ implicitScopeCandidates + allCandidates.map(_.implicitRef.underlyingRef.symbol).toSet + } + /** Test whether the head of a given instance matches the expected type `pt`, * ignoring any dependent implicit arguments. */ @@ -220,7 +231,9 @@ trait ImportSuggestions: Nil roots - .flatMap(_.implicitMembers.filter(shallowTest)) + .flatMap(_.implicitMembers.filter { ref => + !alreadyAvailableCandidates(ref.symbol) && shallowTest(ref) + }) // filter whether the head of the implicit can match .partition(deepTest) // partition into full matches and head matches diff --git a/tests/neg/missing-implicit3.check b/tests/neg/missing-implicit3.check index a33052ef6c9d..3c2ab8ffb83f 100644 --- a/tests/neg/missing-implicit3.check +++ b/tests/neg/missing-implicit3.check @@ -7,8 +7,3 @@ | ord.Ord.ordered[A](/* missing */implicitly[ord.Foo => Comparable[? >: ord.Foo]]) | |But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo]. - | - |The following import might make progress towards fixing the problem: - | - | import ord.Ord.ordered - |