Skip to content

Commit 07b6199

Browse files
committed
Don’t suggest to import things that are already available
Fixes #8051
1 parent 63605ca commit 07b6199

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ trait ImportSuggestions:
144144
val timer = new Timer()
145145
val deadLine = System.currentTimeMillis() + suggestImplicitTimeOut
146146

147+
// Candidates that are already available without explicit import because they
148+
// are already provided by the context (imported or inherited) or because they
149+
// are in the implicit scope of `pt`.
150+
val alreadyAvailableCandidates: Set[Symbol] = {
151+
val wildProto = wildApprox(pt)
152+
val contextualCandidates = ctx.implicits.eligible(wildProto)
153+
val implicitScopeCandidates = ctx.run.implicitScope(wildProto, ctx).eligible
154+
val allCandidates = contextualCandidates ++ implicitScopeCandidates
155+
allCandidates.map(_.implicitRef.underlyingRef.symbol).toSet
156+
}
157+
147158
/** Test whether the head of a given instance matches the expected type `pt`,
148159
* ignoring any dependent implicit arguments.
149160
*/
@@ -221,7 +232,9 @@ trait ImportSuggestions:
221232
Nil
222233

223234
roots
224-
.flatMap(_.implicitMembers.filter(shallowTest))
235+
.flatMap(_.implicitMembers.filter { ref =>
236+
!alreadyAvailableCandidates(ref.symbol) && shallowTest(ref)
237+
})
225238
// filter whether the head of the implicit can match
226239
.partition(deepTest)
227240
// partition into full matches and head matches

tests/neg/missing-implicit3.check

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@
1010
| scala.math.Ordering.ordered[A](/* missing */implicitly[scala.math.Ordering.AsComparable[B]])
1111
|
1212
| But no implicit values were found that match type scala.math.Ordering.AsComparable[B].
13-
|
14-
| One of the following imports might make progress towards fixing the problem:
15-
|
16-
| import math.Ordering.comparatorToOrdering
17-
| import math.Ordering.ordered
18-
|

0 commit comments

Comments
 (0)