Skip to content

Commit 64a34af

Browse files
authored
Merge pull request #10974 from dotty-staging/fix-#9436
Better specification of references in error messages
2 parents 8b430a2 + e3667c4 commit 64a34af

File tree

7 files changed

+31
-6
lines changed

7 files changed

+31
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ import transform.SymUtils._
17781778
|To convert to a function value, you need to explicitly write ${hl("() => x")}"""
17791779
}
17801780

1781-
class MissingEmptyArgumentList(method: Symbol)(using Context)
1781+
class MissingEmptyArgumentList(method: String)(using Context)
17821782
extends SyntaxMsg(MissingEmptyArgumentListID) {
17831783
def msg = em"$method must be called with ${hl("()")} argument"
17841784
def explain = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ trait Applications extends Compatibility {
373373
def success: Boolean = ok
374374

375375
protected def methodType: MethodType = methType.asInstanceOf[MethodType]
376-
private def methString: String = i"${methRef.symbol}: ${methType.show}"
376+
private def methString: String = i"${err.refStr(methRef)}: ${methType.show}"
377377

378378
/** Re-order arguments to correctly align named arguments */
379379
def reorder[T >: Untyped](args: List[Trees.Tree[T]]): List[Trees.Tree[T]] = {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ object ErrorReporting {
8484
else anonymousTypeMemberStr(denot.info)
8585

8686
def refStr(tp: Type): String = tp match {
87-
case tp: NamedType => denotStr(tp.denot)
87+
case tp: NamedType =>
88+
if tp.denot.symbol.exists then tp.denot.symbol.showLocated
89+
else
90+
val kind = tp.info match
91+
case _: MethodOrPoly | _: ExprType => "method"
92+
case _ => if tp.isType then "type" else "value"
93+
s"$kind ${tp.name}"
8894
case _ => anonymousTypeMemberStr(tp)
8995
}
9096

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,7 @@ class Typer extends Namer
29512951
def readaptSimplified(tree: Tree)(using Context) = readapt(simplify(tree, pt, locked))
29522952

29532953
def missingArgs(mt: MethodType) = {
2954-
val meth = methPart(tree).symbol
2954+
val meth = err.exprStr(methPart(tree))
29552955
if (mt.paramNames.length == 0) report.error(MissingEmptyArgumentList(meth), tree.srcPos)
29562956
else report.error(em"missing arguments for $meth", tree.srcPos)
29572957
tree.withType(mt.resultType)
@@ -3221,7 +3221,7 @@ class Typer extends Namer
32213221
def isAutoApplied(sym: Symbol): Boolean =
32223222
sym.isConstructor
32233223
|| sym.matchNullaryLoosely
3224-
|| warnOnMigration(MissingEmptyArgumentList(sym), tree.srcPos)
3224+
|| warnOnMigration(MissingEmptyArgumentList(sym.show), tree.srcPos)
32253225
&& { patch(tree.span.endPos, "()"); true }
32263226

32273227
// Reasons NOT to eta expand:

tests/neg/i2033.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg/i2033.scala:7:30 -----------------------------------------------------------------------------------
22
7 | val arr = bos toByteArray () // error
33
| ^^
4-
|can't supply unit value with infix notation because nullary method toByteArray: (): Array[Byte] takes no arguments; use dotted invocation instead: (...).toByteArray()
4+
|can't supply unit value with infix notation because nullary method toByteArray in class ByteArrayOutputStream: (): Array[Byte] takes no arguments; use dotted invocation instead: (...).toByteArray()
55
-- [E007] Type Mismatch Error: tests/neg/i2033.scala:20:35 -------------------------------------------------------------
66
20 | val out = new ObjectOutputStream(println) // error
77
| ^^^^^^^

tests/neg/i9436.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- [E100] Syntax Error: tests/neg/i9436.scala:8:12 ---------------------------------------------------------------------
2+
8 | println(x.f1) // error
3+
| ^^^^
4+
| method f1 must be called with () argument
5+
6+
longer explanation available when compiling with `-explain`
7+
-- Error: tests/neg/i9436.scala:9:14 -----------------------------------------------------------------------------------
8+
9 | println(x.f2(1)) // error
9+
| ^^^^^^^
10+
| missing argument for parameter y of method f2: (x: Int, y: Int): Int

tests/neg/i9436.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Bag extends reflect.Selectable
2+
3+
@main def Test =
4+
val x = new Bag:
5+
def f1() = 23
6+
def f2(x: Int, y: Int) = x + y
7+
8+
println(x.f1) // error
9+
println(x.f2(1)) // error

0 commit comments

Comments
 (0)