Skip to content

Commit 4b23ff9

Browse files
mbovelolhotaknatsukagami
committed
Avoid infinite recursion when looking for import suggestions
Co-authored-by: Ondřej Lhoták <[email protected]> Co-authored-by: Nguyen Pham <[email protected]>
1 parent af655c9 commit 4b23ff9

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ trait ImportSuggestions:
9797
if refSym.is(JavaDefined) then Nil
9898
else nestedRoots(site)
9999
nested
100+
.filter(mbr => mbr.asTerm != ref.termSymbol)
100101
.map(mbr => TermRef(ref, mbr.asTerm))
101102
.flatMap(rootsIn)
102103
.toList

tests/neg/22145.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- [E008] Not Found Error: tests/neg/22145.scala:3:7 -------------------------------------------------------------------
2+
3 | base.foo() // error
3+
| ^^^^^^^^
4+
| value foo is not a member of Collection

tests/neg/22145.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Collection:
2+
val base: Collection = ???
3+
base.foo() // error
4+
5+
object O extends Collection:
6+
def foo(): Int = ???

tests/neg/22145b.check

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- [E008] Not Found Error: tests/neg/22145b.scala:13:19 ----------------------------------------------------------------
2+
13 | require(base.isWithin(p, start, end), "position is out of bounds") // error
3+
| ^^^^^^^^^^^^^
4+
| value isWithin is not a member of Collection.this.Self
5+
-- [E008] Not Found Error: tests/neg/22145b.scala:26:59 ----------------------------------------------------------------
6+
26 | def positionAfter(p: Position): Position = self.base.positionAfter(p) // error
7+
| ^^^^^^^^^^^^^^^^^^^^^^^
8+
|value positionAfter is not a member of Collection.this.Self.
9+
|An extension method was tried, but could not be fully constructed:
10+
|
11+
| this.positionAfter(self.base)
12+
|
13+
| failed with:
14+
|
15+
| Found: (self.base : Collection.this.Self)
16+
| Required: Collection.given_is_Slice_Collection.Self²
17+
|
18+
| where: Self is a type in trait Collection
19+
| Self² is a type in object given_is_Slice_Collection which is an alias of Collection.this.Slice
20+
|
21+
-- [E008] Not Found Error: tests/neg/22145b.scala:27:50 ----------------------------------------------------------------
22+
27 | def apply(p: Position): Element = self.base.apply(p) // error
23+
| ^^^^^^^^^^^^^^^
24+
|value apply is not a member of Collection.this.Self.
25+
|An extension method was tried, but could not be fully constructed:
26+
|
27+
| this.apply(self.base)
28+
|
29+
| failed with:
30+
|
31+
| Found: (self.base : Collection.this.Self)
32+
| Required: Collection.given_is_Slice_Collection.Self²
33+
|
34+
| where: Self is a type in trait Collection
35+
| Self² is a type in object given_is_Slice_Collection which is an alias of Collection.this.Slice
36+
|

tests/neg/22145b.scala

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import language.experimental.modularity
2+
3+
trait Collection:
4+
me =>
5+
6+
type Self
7+
type Position
8+
type Element
9+
10+
final class Slice(private[Collection] val base: Self, val start: Position, val end: Position):
11+
12+
final def apply(p: Position): Element =
13+
require(base.isWithin(p, start, end), "position is out of bounds") // error
14+
base.apply(p)
15+
16+
end Slice
17+
18+
given Slice is Collection:
19+
20+
type Position = me.Position
21+
type Element = me.Element
22+
23+
extension (self: Self)
24+
def start: Position = self.start
25+
def end: Position = self.end
26+
def positionAfter(p: Position): Position = self.base.positionAfter(p) // error
27+
def apply(p: Position): Element = self.base.apply(p) // error
28+
29+
end given
30+
31+
extension (self: Self)
32+
33+
def start: Position
34+
def end: Position
35+
def positionAfter(p: Position): Position
36+
def apply(p: Position): Element
37+
38+
end extension

0 commit comments

Comments
 (0)