Skip to content

Don’t suggest to import things that are already in scope #8640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/ImportSuggestions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions tests/neg/missing-implicit3.check
Original file line number Diff line number Diff line change
Expand Up @@ -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
|