Skip to content
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

Refine condition when to report errors in an inserted apply #15016

Merged
merged 1 commit into from
May 9, 2022
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
22 changes: 17 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3184,12 +3184,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case _ => false
}

var assumeApplyExists = false
// if true, issue any errors about the apply instead of `fallBack`,
// since they are more likely to be informative.

def tryApply(using Context) = {
val pt1 = pt.withContext(ctx)
val sel = typedSelect(untpd.Select(untpd.TypedSplice(tree), nme.apply), pt1)
.withAttachment(InsertedApply, ())
if (sel.tpe.isError) sel
else try adapt(simplify(sel, pt1, locked), pt1, locked) finally sel.removeAttachment(InsertedApply)
if sel.tpe.isError then
// assume the apply exists if qualifier has a hidden search failure of type
// FailedExtension or NestedFailure
sel match
case Select(qual, _) =>
assumeApplyExists = qual.getAttachment(Typer.HiddenSearchFailure).exists(
_.exists(_.reason.isInstanceOf[FailedExtension | NestedFailure]))
case _ =>
sel
else
assumeApplyExists = true
try adapt(simplify(sel, pt1, locked), pt1, locked) finally sel.removeAttachment(InsertedApply)
}

def tryImplicit(fallBack: => Tree) =
Expand All @@ -3209,11 +3223,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if (isApplyProto(pt) || isMethod(tree) || isSyntheticApply(tree)) tryImplicit(fallBack)
else tryEither(tryApply) { (app, appState) =>
tryImplicit {
if (tree.tpe.member(nme.apply).exists) {
// issue the error about the apply, since it is likely more informative than the fallback
if assumeApplyExists then
appState.commit()
app
}
else fallBack
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/neg/i15000.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- [E006] Not Found Error: tests/neg/i15000.scala:19:11 ----------------------------------------------------------------
19 | str(x, barY) // error
| ^^^^
| Not found: barY
|
| longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: tests/neg/i15000.scala:20:9 -----------------------------------------------------------------
20 | c(x, barY) // error // error
| ^^^^
| Not found: barY
|
| longer explanation available when compiling with `-explain`
-- [E008] Not Found Error: tests/neg/i15000.scala:20:4 -----------------------------------------------------------------
20 | c(x, barY) // error // error
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we get the ambiguity error also for str(x, barY)?

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's more subtle. In the stringExtensions case, we try the implicit conversion any2stringadd first, and that causes the "not found" error.

| ^
|value apply is not a member of object ExtensionMethodReproduction.c.
|An extension method was tried, but could not be fully constructed:
|
| apply(ExtensionMethodReproduction.c) failed with
|
| Ambiguous overload. The overloaded alternatives of method apply in object ExtensionMethodReproduction with types
| (c: ExtensionMethodReproduction.C)(x: Int, y: Int): String
| (c: ExtensionMethodReproduction.C)(x: Int, y: String): String
| both match arguments (ExtensionMethodReproduction.c.type)
21 changes: 21 additions & 0 deletions tests/neg/i15000.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
object ExtensionMethodReproduction {
import scala.language.implicitConversions
class StringExtensions {
def apply(x: Int, y: String): String = "foo"
def apply(x: Int, y: Int): String = "foo"
}
implicit def stringExtensions(m: String): StringExtensions = new StringExtensions()

class C
object c extends C

extension (c: C)
def apply(x: Int, y: String): String = "foo"
def apply(x: Int, y: Int): String = "foo"

val str = "POST"
val x: Int = 1
val fooY: String = "foo"
str(x, barY) // error
c(x, barY) // error // error
}