Skip to content

Fix #7986: Warn on cyclic references when looking for extension methods #7988

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

Closed
wants to merge 4 commits into from
Closed
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
35 changes: 24 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3483,28 +3483,41 @@ class Typer extends Namer
}

// try an extension method in scope
pt match {
pt match
case selProto @ SelectionProto(selName: TermName, mbrType, _, _) =>
val origCtx = ctx
def tryExtension(using Context): Tree =
var tried: untpd.Tree = untpd.EmptyTree
try
findRef(selName, WildcardType, ExtensionMethod, tree.srcPos) match
case ref: TermRef =>
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
tried = untpd.ref(ref).withSpan(tree.span)
extMethodApply(tried, tree, mbrType)
case _ => findRef(selProto.extensionName, WildcardType, ExtensionMethod, tree.srcPos) match
case ref: TermRef =>
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
tried = untpd.ref(ref).withSpan(tree.span)
extMethodApply(tried, tree, mbrType)
case _ => EmptyTree
catch case ex: TypeError => errorTree(tree, ex, tree.srcPos)
catch case ex: TypeError =>
ex match
case ex: CyclicReference if ex.denot.is(Extension) && ex.denot.name == selName =>
report.error(
em"""An extension method for `.$selName` was tried but could not be fully constructed
|since there was a cyclic reference involving ${ex.denot.symbol.showLocated}""",
Copy link
Contributor

@anatoliykmetyuk anatoliykmetyuk Jan 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user, I do not necessarily understand what's a cyclic reference and how to fix it. Would it be exhaustive to say here that the user must explicitly specify the return types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out to be quite hard to figure out accurately that a return type is missing.

tree.srcPos)(using origCtx)
case _ =>
if tried.isEmpty then EmptyTree
else errorTree(tried, ex, tree.srcPos)

val nestedCtx = ctx.fresh.setNewTyperState()
val app = tryExtension(using nestedCtx)
if (!app.isEmpty && !nestedCtx.reporter.hasErrors) {
val tried = tryExtension(using nestedCtx)
if !tried.isEmpty && !nestedCtx.reporter.hasErrors then
nestedCtx.typerState.commit()
return ExtMethodApply(app)
}
else if !app.isEmpty then
rememberSearchFailure(tree, SearchFailure(app.withType(FailedExtension(app, pt))))
return ExtMethodApply(tried)
else if !tried.isEmpty then
rememberSearchFailure(tree,
SearchFailure(tried.withType(FailedExtension(tried, pt))))
case _ =>
}

// try an implicit conversion
val prevConstraint = ctx.typerState.constraint
Expand Down
8 changes: 5 additions & 3 deletions scala3doc/src/dotty/dokka/tasty/BasicSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.dokka.model._
import collection.JavaConverters._
import dotty.dokka._
import dotty.dokka.model.api.Annotation
import doc.DocumentationNode
import dotty.dokka.model.api.TastyDocumentableSource

trait BasicSupport:
Expand Down Expand Up @@ -34,10 +35,11 @@ trait BasicSupport:


extension (sym: Symbol):
def documentation(using cxt: Context) = sym.documentation match
case Some(comment) =>
def documentation(using cxt: Context): Map[SourceSetWrapper, DocumentationNode] =
qctx.reflect.SymbolMethods.documentation(sym) match
case Some(comment) =>
Map(sourceSet -> parseComment(comment, sym.tree))
case None =>
case None =>
Map.empty

def source(using ctx: Context) =
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/i7986.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/i7986.scala:3:56 -----------------------------------------------------------------------------------
3 |extension (project: Project) def dependencies = project.name.dependencies // error, following a cyclic reference warning
| ^^^^^^^^^^^^
| An extension method for `.dependencies` was tried but could not be fully constructed
| since there was a cyclic reference involving method dependencies
3 changes: 3 additions & 0 deletions tests/neg/i7986.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
case class Project(name: String)
extension (name: String) def dependencies = ???
extension (project: Project) def dependencies = project.name.dependencies // error, following a cyclic reference warning
3 changes: 3 additions & 0 deletions tests/pos/i7986.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
case class Project(name: String)
extension (name: String) def dependencies: String = ???
extension (project: Project) def dependencies: String = project.name.dependencies
2 changes: 1 addition & 1 deletion tests/pos/i8256.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ val a = 1

val fetch = implicitly[Test[1]]

@main def main() : Unit = {}
@main def test() : Unit = {}