diff options
author | Paul Phillips <paulp@improving.org> | 2011-02-09 09:32:40 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-02-09 09:32:40 +0000 |
commit | 756a086802b90674545888c9138575ea9a2237f4 (patch) | |
tree | c25e64b9b9fe8cc01a3d3aeff71b8b4ac78bab0b | |
parent | 27a658c86e06b865cba1b3d1d2b0bc423fe0ad9a (diff) | |
download | scala-756a086802b90674545888c9138575ea9a2237f4.tar.gz scala-756a086802b90674545888c9138575ea9a2237f4.tar.bz2 scala-756a086802b90674545888c9138575ea9a2237f4.zip |
Improved an error message involving overload+de...
Improved an error message involving overload+default args. Closes #3909,
no review.
-rw-r--r-- | src/compiler/scala/tools/nsc/symtab/NameManglers.scala | 5 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Contexts.scala | 26 | ||||
-rw-r--r-- | test/files/neg/bug3909.check | 4 | ||||
-rw-r--r-- | test/files/neg/bug3909.scala | 12 |
4 files changed, 39 insertions, 8 deletions
diff --git a/src/compiler/scala/tools/nsc/symtab/NameManglers.scala b/src/compiler/scala/tools/nsc/symtab/NameManglers.scala index 828ca5b0dd..3a5c69e8a6 100644 --- a/src/compiler/scala/tools/nsc/symtab/NameManglers.scala +++ b/src/compiler/scala/tools/nsc/symtab/NameManglers.scala @@ -132,6 +132,11 @@ trait NameManglers { val prefix = if (isConstructorName(name)) "init" else name newTermName(prefix + DEFAULT_GETTER_STRING + pos) } + def defaultGetterToMethod(name: Name): TermName = { + val p = name.pos(DEFAULT_GETTER_STRING) + if (p < name.length) name.subName(0, p) + else name + } def implClassName(name: Name): TypeName = name append IMPL_CLASS_SUFFIX toTypeName def interfaceName(implname: Name): TypeName = implname stripEnd IMPL_CLASS_SUFFIX toTypeName diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index e53ec4a987..eb9eb9dbec 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -302,16 +302,26 @@ trait Contexts { self: Analyzer => * @param sym2 ... * @param rest ... */ - def ambiguousError(pos: Position, pre: Type, sym1: Symbol, - sym2: Symbol, rest: String) { - val msg = - ("ambiguous reference to overloaded definition,\n" + - "both " + sym1 + sym1.locationString + " of type " + pre.memberType(sym1) + - "\nand " + sym2 + sym2.locationString + " of type " + pre.memberType(sym2) + - "\nmatch " + rest) + def ambiguousError(pos: Position, pre: Type, sym1: Symbol, sym2: Symbol, rest: String) { + val (reportPos, msg) = ( + if (sym1.hasDefaultFlag && sym2.hasDefaultFlag && sym1.enclClass == sym2.enclClass) { + val methodName = nme.defaultGetterToMethod(sym1.name) + (sym1.enclClass.pos, + "in "+ sym1.enclClass +", multiple overloaded alternatives of " + methodName + + " define default arguments") + } + else { + (pos, + ("ambiguous reference to overloaded definition,\n" + + "both " + sym1 + sym1.locationString + " of type " + pre.memberType(sym1) + + "\nand " + sym2 + sym2.locationString + " of type " + pre.memberType(sym2) + + "\nmatch " + rest) + ) + } + ) if (reportAmbiguousErrors) { if (!pre.isErroneous && !sym1.isErroneous && !sym2.isErroneous) - unit.error(pos, msg) + unit.error(reportPos, msg) } else throw new TypeError(pos, msg) } diff --git a/test/files/neg/bug3909.check b/test/files/neg/bug3909.check new file mode 100644 index 0000000000..af79579d82 --- /dev/null +++ b/test/files/neg/bug3909.check @@ -0,0 +1,4 @@ +bug3909.scala:1: error: in object DO, multiple overloaded alternatives of m1 define default arguments +object DO { + ^ +one error found diff --git a/test/files/neg/bug3909.scala b/test/files/neg/bug3909.scala new file mode 100644 index 0000000000..758dc70fd4 --- /dev/null +++ b/test/files/neg/bug3909.scala @@ -0,0 +1,12 @@ +object DO { + class Extras { } + object Extras { val defaultValue = new Extras } + + def m1(str: String, extraStuff: Extras = Extras.defaultValue): Int = str.length + def m1(i: Int, extraStuff: Extras = Extras.defaultValue): Int = 2 * i + + def main(args: Array[String]): Unit = { + val m1s = m1("foo") + val m1i = m1(42) + } +} |