Skip to content

Commit 40aa842

Browse files
committed
Don’t suggest to import things that are already available
1 parent 63605ca commit 40aa842

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
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: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
-- Error: tests/neg/missing-implicit3.scala:3:37 -----------------------------------------------------------------------
22
3 |val sortedFoos = List(new Foo).sorted // error
33
| ^
4-
| No implicit Ordering defined for B
4+
| No implicit Ordering defined for B
55
|
6-
| where: B is a type variable with constraint >: Foo
7-
| ..
8-
| I found:
6+
| where: B is a type variable with constraint >: Foo
7+
| ..
8+
| I found:
99
|
10-
| scala.math.Ordering.ordered[A](/* missing */implicitly[scala.math.Ordering.AsComparable[B]])
10+
| scala.math.Ordering.comparatorToOrdering[A](/* missing */implicitly[java.util.Comparator[B]])
1111
|
12-
| 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-
|
12+
| But no implicit values were found that match type java.util.Comparator[B].

0 commit comments

Comments
 (0)