Skip to content

Commit 1931bc7

Browse files
authored
Merge pull request #8640 from scalacenter/fix/remove-suggestions-already-in-scope
Don’t suggest to import things that are already in scope
2 parents 5ec51f2 + 6e1761d commit 1931bc7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
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
*/
@@ -220,7 +231,9 @@ trait ImportSuggestions:
220231
Nil
221232

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

tests/neg/missing-implicit3.check

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@
77
| ord.Ord.ordered[A](/* missing */implicitly[ord.Foo => Comparable[? >: ord.Foo]])
88
|
99
|But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo].
10-
|
11-
|The following import might make progress towards fixing the problem:
12-
|
13-
| import ord.Ord.ordered
14-
|

0 commit comments

Comments
 (0)