Skip to content

Commit a137c52

Browse files
committed
Fix #7986: Issue warning on cyclic reference for extmethod
Issue a warning if a cyclic reference is encountered when searching for an applicable extension method.
1 parent f3d7b34 commit a137c52

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,17 +3048,24 @@ class Typer extends Namer
30483048
// try an extension method in scope
30493049
pt match {
30503050
case SelectionProto(name, mbrType, _, _) =>
3051+
val origCtx = ctx
30513052
def tryExtension(implicit ctx: Context): Tree =
30523053
try
30533054
findRef(name, WildcardType, ExtensionMethod, tree.posd) match {
30543055
case ref: TermRef =>
30553056
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
30563057
case _ => EmptyTree
30573058
}
3058-
catch {
3059-
case ex: CyclicReference => throw ex
3060-
case ex: TypeError => errorTree(tree, ex, tree.sourcePos)
3061-
}
3059+
catch
3060+
case ex: CyclicReference =>
3061+
if ex.denot.is(Extension) && ex.denot.name == name then
3062+
origCtx.warning(
3063+
em"""An extension method was tried but could not be fully constructed
3064+
|since there was a cyclic reference involving ${ex.denot.symbol.showLocated}""",
3065+
tree.sourcePos)
3066+
EmptyTree
3067+
case ex: TypeError =>
3068+
EmptyTree
30623069
val nestedCtx = ctx.fresh.setNewTyperState()
30633070
val app = tryExtension(nestedCtx)
30643071
if (!app.isEmpty && !nestedCtx.reporter.hasErrors) {

tests/neg/i7986.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
case class Project(name: String)
22
def (name: String) dependencies = ???
3-
def (project: Project) dependencies = project.name.dependencies // error: needs return type
3+
def (project: Project) dependencies = project.name.dependencies // error, following a cyclic reference warning

0 commit comments

Comments
 (0)