From beae7735b845c680331fd68004d52e681fe3b8d1 Mon Sep 17 00:00:00 2001 From: Simon Ochsenreither Date: Fri, 13 Apr 2012 18:59:35 +0200 Subject: Improved formatting/display of documentation. Made the primary constructors private to prevent them from appearing in ScalaDoc. --- src/compiler/scala/tools/cmd/gen/AnyVals.scala | 48 +++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala index 0869350dd3..85f0d65d6b 100644 --- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala +++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala @@ -19,15 +19,15 @@ trait AnyValReps { def unaryOps = { val ops = List( Op("+", "/**\n" + - " * @return this value, unmodified\n" + + " * Returns this value, unmodified.\n" + " */"), Op("-", "/**\n" + - " * @return the negation of this value\n" + + " * Returns the negation of this value.\n" + " */")) if(isCardinal) Op("~", "/**\n" + - " * @return the bitwise negation of this value\n" + + " * Returns the bitwise negation of this value.\n" + " * @example {{{\n" + " * ~5 == -6\n" + " * // in binary: ~00000101 ==\n" + @@ -41,7 +41,7 @@ trait AnyValReps { if (isCardinal) List( Op("|", "/**\n" + - " * @return the bitwise OR of this value and x\n" + + " * Returns the bitwise OR of this value and `x`.\n" + " * @example {{{\n" + " * (0xf0 | 0xaa) == 0xfa\n" + " * // in binary: 11110000\n" + @@ -51,7 +51,7 @@ trait AnyValReps { " * }}}\n" + " */"), Op("&", "/**\n" + - " * @return the bitwise AND of this value and x\n" + + " * Returns the bitwise AND of this value and `x`.\n" + " * @example {{{\n" + " * (0xf0 & 0xaa) == 0xa0\n" + " * // in binary: 11110000\n" + @@ -61,7 +61,7 @@ trait AnyValReps { " * }}}\n" + " */"), Op("^", "/**\n" + - " * @return the bitwise XOR of this value and x\n" + + " * Returns the bitwise XOR of this value and `x`.\n" + " * @example {{{\n" + " * (0xf0 ^ 0xaa) == 0x5a\n" + " * // in binary: 11110000\n" + @@ -76,13 +76,13 @@ trait AnyValReps { if (isCardinal) List( Op("<<", "/**\n" + - " * @return this value bit-shifted left by the specified number of bits,\n" + + " * Returns this value bit-shifted left by the specified number of bits,\n" + " * filling in the new right bits with zeroes.\n" + " * @example {{{ 6 << 3 == 48 // in binary: 0110 << 3 == 0110000 }}}\n" + " */"), Op(">>>", "/**\n" + - " * @return this value bit-shifted right by the specified number of bits,\n" + + " * Returns this value bit-shifted right by the specified number of bits,\n" + " * filling the new left bits with zeroes.\n" + " * @example {{{ 21 >>> 3 == 2 // in binary: 010101 >>> 3 == 010 }}}\n" + " * @example {{{\n" + @@ -93,7 +93,7 @@ trait AnyValReps { " */"), Op(">>", "/**\n" + - " * @return this value bit-shifted left by the specified number of bits,\n" + + " * Returns this value bit-shifted left by the specified number of bits,\n" + " * filling in the right bits with the same value as the left-most bit of this.\n" + " * The effect of this is to retain the sign of the value.\n" + " * @example {{{\n" + @@ -105,19 +105,19 @@ trait AnyValReps { else Nil def comparisonOps = List( - Op("==", "/**\n * @return `true` if this value is equal x, `false` otherwise\n */"), - Op("!=", "/**\n * @return `true` if this value is not equal to x, `false` otherwise\n */"), - Op("<", "/**\n * @return `true` if this value is less than x, `false` otherwise\n */"), - Op("<=", "/**\n * @return `true` if this value is less than or equal to x, `false` otherwise\n */"), - Op(">", "/**\n * @return `true` if this value is greater than x, `false` otherwise\n */"), - Op(">=", "/**\n * @return `true` if this value is greater than or equal to x, `false` otherwise\n */")) + Op("==", "/**\n * Returns `true` if this value is equal to x, `false` otherwise.\n */"), + Op("!=", "/**\n * Returns `true` if this value is not equal to x, `false` otherwise.\n */"), + Op("<", "/**\n * Returns `true` if this value is less than x, `false` otherwise.\n */"), + Op("<=", "/**\n * Returns `true` if this value is less than or equal to x, `false` otherwise.\n */"), + Op(">", "/**\n * Returns `true` if this value is greater than x, `false` otherwise.\n */"), + Op(">=", "/**\n * Returns `true` if this value is greater than or equal to x, `false` otherwise.\n */")) def otherOps = List( - Op("+", "/**\n * @return the sum of this value and x\n */"), - Op("-", "/**\n * @return the difference of this value and x\n */"), - Op("*", "/**\n * @return the product of this value and x\n */"), - Op("/", "/**\n * @return the quotient of this value and x\n */"), - Op("%", "/**\n * @return the remainder of the division of this value by x\n */")) + Op("+", "/**\n * Returns the sum of this value and `x`.\n */"), + Op("-", "/**\n * Returns the difference of this value and `x`.\n */"), + Op("*", "/**\n * Returns the product of this value and `x`.\n */"), + Op("/", "/**\n * Returns the quotient of this value and `x`.\n */"), + Op("%", "/**\n * Returns the remainder of the division of this value by `x`.\n */")) // Given two numeric value types S and T , the operation type of S and T is defined as follows: // If both S and T are subrange types then the operation type of S and T is Int. @@ -224,8 +224,8 @@ trait AnyValReps { def classDoc = interpolate(classDocTemplate) def objectDoc = "" def mkImports = "" - def mkClass = assemble("final class", "AnyVal", classLines) + "\n" - def mkObject = assemble("object", "AnyValCompanion", objectLines) + "\n" + def mkClass = assemble("final class", "private", "AnyVal", classLines) + "\n" + def mkObject = assemble("object", "", "AnyValCompanion", objectLines) + "\n" def make() = List[String]( headerTemplate, mkImports, @@ -235,8 +235,8 @@ trait AnyValReps { mkObject ) mkString "" - def assemble(what: String, parent: String, lines: List[String]): String = { - val decl = "%s %s extends %s ".format(what, name, parent) + def assemble(what: String, ctor: String, parent: String, lines: List[String]): String = { + val decl = "%s %s %s extends %s ".format(what, name, ctor, parent) val body = if (lines.isEmpty) "{ }\n\n" else lines map indent mkString ("{\n", "\n", "\n}\n") decl + body -- cgit v1.2.3 From 72d86cbe8cb4792a2eae190aee3677854bffb89f Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 11 Apr 2012 17:29:49 -0700 Subject: SI-5663: Tweak warnings on case class equals Re-enable testing the sensibility of comparing instances of two case classes. In particular, warn if we detect that the two objects inherit from different case classes. In addition, avoid emitting misleading warnings when comparing "new C". --- .../scala/tools/nsc/typechecker/RefChecks.scala | 65 +++++++++++------- test/files/neg/t5663-badwarneq.check | 22 +++++++ test/files/neg/t5663-badwarneq.flags | 1 + test/files/neg/t5663-badwarneq.scala | 76 ++++++++++++++++++++++ 4 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 test/files/neg/t5663-badwarneq.check create mode 100644 test/files/neg/t5663-badwarneq.flags create mode 100644 test/files/neg/t5663-badwarneq.scala (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 806ee480f0..c4b08f6ea2 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1059,12 +1059,10 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R // equals. def isUsingWarnableEquals = { val m = receiver.info.member(nme.equals_) - def n = actual.info.member(nme.equals_) - ( (m == Object_equals) - || (m == Any_equals) - || (m.isSynthetic && m.owner.isCase && !n.owner.isCase) - ) + ((m == Object_equals) || (m == Any_equals) || isMethodCaseEquals(m)) } + def isMethodCaseEquals(m: Symbol) = m.isSynthetic && m.owner.isCase + def isCaseEquals = isMethodCaseEquals(receiver.info.member(nme.equals_)) // Whether this == or != is one of those defined in Any/AnyRef or an overload from elsewhere. def isUsingDefaultScalaOp = { val s = fn.symbol @@ -1087,9 +1085,11 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R val msg = alwaysEqual == (name == nme.EQ || name == nme.eq) unit.warning(pos, "comparing "+what+" using `"+name.decode+"' will always yield " + msg) } - def nonSensible(pre: String, alwaysEqual: Boolean) = nonSensibleWarning(pre+"values of types "+typesString, alwaysEqual) + def nonSensiblyEq() = nonSensible("", true) + def nonSensiblyNeq() = nonSensible("", false) + def nonSensiblyNew() = nonSensibleWarning("a fresh object", false) def unrelatedTypes() = { val msg = if (name == nme.EQ || name == nme.eq) @@ -1097,52 +1097,73 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R unit.warning(pos, typesString + " are unrelated: they will most likely " + msg) } - if (nullCount == 2) - nonSensible("", true) // null == null + if (nullCount == 2) // null == null + nonSensiblyEq() else if (nullCount == 1) { if (onSyms(_ exists isPrimitiveValueClass)) // null == 5 - nonSensible("", false) + nonSensiblyNeq() else if (onTrees( _ exists isNew)) // null == new AnyRef - nonSensibleWarning("a fresh object", false) + nonSensiblyNew() } else if (isBoolean(receiver)) { if (!isBoolean(actual) && !isMaybeValue(actual)) // true == 5 - nonSensible("", false) + nonSensiblyNeq() } else if (isUnit(receiver)) { if (isUnit(actual)) // () == () - nonSensible("", true) + nonSensiblyEq() else if (!isUnit(actual) && !isMaybeValue(actual)) // () == "abc" - nonSensible("", false) + nonSensiblyNeq() } else if (isNumeric(receiver)) { if (!isNumeric(actual) && !forMSIL) if (isUnit(actual) || isBoolean(actual) || !isMaybeValue(actual)) // 5 == "abc" - nonSensible("", false) + nonSensiblyNeq() } - else if (isWarnable) { + else if (isWarnable && !isCaseEquals) { if (isNew(qual)) // new X == y - nonSensibleWarning("a fresh object", false) + nonSensiblyNew() else if (isNew(args.head) && (receiver.isEffectivelyFinal || isReferenceOp)) // object X ; X == new Y - nonSensibleWarning("a fresh object", false) + nonSensiblyNew() else if (receiver.isEffectivelyFinal && !(receiver isSubClass actual)) { // object X, Y; X == Y if (isEitherNullable) nonSensible("non-null ", false) else - nonSensible("", false) + nonSensiblyNeq() } } // possibleNumericCount is insufficient or this will warn on e.g. Boolean == j.l.Boolean if (isWarnable && nullCount == 0 && !(isSpecial(receiver) && isSpecial(actual))) { - if (actual isSubClass receiver) () - else if (receiver isSubClass actual) () - // warn only if they have no common supertype below Object - else { + // better to have lubbed and lost + def warnIfLubless(): Unit = { val common = global.lub(List(actual.tpe, receiver.tpe)) if (ObjectClass.tpe <:< common) unrelatedTypes() } + def eitherSubclasses = (actual isSubClass receiver) || (receiver isSubClass actual) + // warn if actual has a case parent that is not same as receiver's; + // if actual is not a case, then warn if no common supertype, as below + if (isCaseEquals) { + def thisCase = receiver.info.member(nme.equals_).owner + actual.info.baseClasses.find(_.isCase) match { + case Some(p) if (p != thisCase) => nonSensible("case class ", false) + case None => + // stronger message on (Some(1) == None) + //if (receiver.isCase && receiver.isEffectivelyFinal && !(receiver isSubClass actual)) nonSensiblyNeq() + //else + // if a class, it must be super to thisCase (and receiver) since not <: thisCase + if (!actual.isTrait && !(receiver isSubClass actual)) nonSensiblyNeq() + else if (!eitherSubclasses) warnIfLubless() + case _ => + } + } + else if (actual isSubClass receiver) () + else if (receiver isSubClass actual) () + // warn only if they have no common supertype below Object + else { + warnIfLubless() + } } case _ => } diff --git a/test/files/neg/t5663-badwarneq.check b/test/files/neg/t5663-badwarneq.check new file mode 100644 index 0000000000..00c2234e9d --- /dev/null +++ b/test/files/neg/t5663-badwarneq.check @@ -0,0 +1,22 @@ +t5663-badwarneq.scala:42: error: comparing case class values of types Some[Int] and None.type using `==' will always yield false + println(new Some(1) == None) // Should complain on type, was: spuriously complains on fresh object + ^ +t5663-badwarneq.scala:43: error: comparing case class values of types Some[Int] and Thing using `==' will always yield false + println(Some(1) == new Thing(1)) // Should complain on type, was: spuriously complains on fresh object + ^ +t5663-badwarneq.scala:51: error: ThingOne and Thingy are unrelated: they will most likely never compare equal + println(t1 == t2) // true, but apparently unrelated, a compromise warning + ^ +t5663-badwarneq.scala:52: error: ThingThree and Thingy are unrelated: they will most likely never compare equal + println(t4 == t2) // true, complains because ThingThree is final and Thingy not a subclass, stronger claim than unrelated + ^ +t5663-badwarneq.scala:55: error: comparing case class values of types ThingTwo and Some[Int] using `==' will always yield false + println(t3 == Some(1)) // false, warn on different cases + ^ +t5663-badwarneq.scala:56: error: comparing values of types ThingOne and Cousin using `==' will always yield false + println(t1 == c) // should warn + ^ +t5663-badwarneq.scala:64: error: comparing case class values of types Simple and SimpleSibling.type using `==' will always yield false + println(new Simple() == SimpleSibling) // like Some(1) == None, but needn't be final case + ^ +7 errors found diff --git a/test/files/neg/t5663-badwarneq.flags b/test/files/neg/t5663-badwarneq.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/neg/t5663-badwarneq.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/neg/t5663-badwarneq.scala b/test/files/neg/t5663-badwarneq.scala new file mode 100644 index 0000000000..56ec389c03 --- /dev/null +++ b/test/files/neg/t5663-badwarneq.scala @@ -0,0 +1,76 @@ + +// alias +trait Thingy + +class Gramps + +// sibling classes that extend a case class +case class Thing(i: Int) extends Gramps +class ThingOne(x:Int) extends Thing(x) +class ThingTwo(y:Int) extends Thing(y) with Thingy +final class ThingThree(z:Int) extends Thing(z) + +// not case cousin +class Cousin extends Gramps + +class SimpleParent +case class Simple() extends SimpleParent +case object SimpleSibling extends SimpleParent + +/* It's not possible to run partest without -deprecation. + * Since detecting the warnings requires a neg test with + * -Xfatal-warnings, and deprecation terminates the compile, + * we'll just comment out the nasty part. The point was + * just to show there's nothing special about a trait + * that extends a case class, which is only permitted + * (deprecatingly) by omitting the parens. + * +// common ancestor is something else +class AnyThing +case class SomeThing extends AnyThing // deprecation +class OtherThing extends AnyThing + +// how you inherit caseness doesn't matter +trait InThing extends SomeThing +class MyThing extends InThing +*/ + +object Test { + def main(a: Array[String]) { + // nothing to do with Gavin + println(new Some(1) == new Some(1)) // OK, true + println(new Some(1) == None) // Should complain on type, was: spuriously complains on fresh object + println(Some(1) == new Thing(1)) // Should complain on type, was: spuriously complains on fresh object + + val t1 = new ThingOne(11) + val t2: Thingy = new ThingTwo(11) + val t3 = new ThingTwo(11) + val t4 = new ThingThree(11) + val c = new Cousin + + println(t1 == t2) // true, but apparently unrelated, a compromise warning + println(t4 == t2) // true, complains because ThingThree is final and Thingy not a subclass, stronger claim than unrelated + println(t2 == t3) // OK, two Thingy + println(t3 == t2) // ditto with case receiver + println(t3 == Some(1)) // false, warn on different cases + println(t1 == c) // should warn + + // don't warn on fresh cases + println(new ThingOne(11) == t1) // OK, was: two cases not warnable on trunk + println(new ThingTwo(11) == t2) // true, was: spuriously complains on fresh object + println(new ThingOne(11) == t3) // two cases not warnable on trunk + println(new ThingTwo(11) == t3) // ditto + + println(new Simple() == SimpleSibling) // like Some(1) == None, but needn't be final case + + /* + val mine = new MyThing + val some = new SomeThing + val other = new OtherThing + println(mine == some) // OK, two Something + println(some == mine) + println(mine == other) // OK, two Anything? + println(mine == t1) // false + */ + } +} -- cgit v1.2.3 From 651f2219b325c1a4cf586da4cae58e60caec0860 Mon Sep 17 00:00:00 2001 From: Vlad Ureche Date: Sat, 14 Apr 2012 12:07:02 +0200 Subject: Fixing the docs.scalap breakage --- src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala b/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala index 9fcd43ac02..124a7509e8 100644 --- a/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala @@ -398,11 +398,13 @@ class ModelFactory(val global: Global, val settings: doc.Settings) { else if (bSym.isPackage) makeTemplate(bSym.owner) match { case inPkg: PackageImpl => makePackage(bSym, inPkg) getOrElse (new NoDocTemplateImpl(bSym, inPkg)) + case inNoDocTpl: NoDocTemplateImpl => new NoDocTemplateImpl(bSym, inNoDocTpl) case _ => throw new Error("'" + bSym + "' must be in a package") } else if (templateShouldDocument(bSym)) makeTemplate(bSym.owner) match { case inDTpl: DocTemplateImpl => makeDocTemplate(bSym, inDTpl) + case inNoDocTpl: NoDocTemplateImpl => new NoDocTemplateImpl(bSym, inNoDocTpl) case _ => throw new Error("'" + bSym + "' must be in documentable template") } else -- cgit v1.2.3 From cb2468a8a0971207ceb33c7bb9d2596bdbac8072 Mon Sep 17 00:00:00 2001 From: Vlad Ureche Date: Sat, 14 Apr 2012 12:40:03 +0200 Subject: Reimplemented shadowing between class members and implicit pimped on members. Thanks to Lukas for pointing it out! --- .../doc/model/ModelFactoryImplicitSupport.scala | 22 ++++++- test/scaladoc/resources/implicits-base-res.scala | 66 ++++++++++---------- .../resources/implicits-shadowing-res.scala | 64 ++++++++++++++++++++ test/scaladoc/run/implicits-shadowing.check | 1 + test/scaladoc/run/implicits-shadowing.scala | 70 ++++++++++++++++++++++ 5 files changed, 189 insertions(+), 34 deletions(-) create mode 100644 test/scaladoc/resources/implicits-shadowing-res.scala create mode 100644 test/scaladoc/run/implicits-shadowing.check create mode 100644 test/scaladoc/run/implicits-shadowing.scala (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala b/src/compiler/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala index 23bef02bed..0e44933ac6 100644 --- a/src/compiler/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala +++ b/src/compiler/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala @@ -137,8 +137,8 @@ trait ModelFactoryImplicitSupport { // Members inherited by implicit conversions cannot override actual members memberSyms = memberSyms.filterNot((sym1: Symbol) => - existingMembers.exists(sym2 => sym1.name == sym2.name && - isSameType(toType.memberInfo(sym1), sym.info.memberInfo(sym2)))) + existingMembers.exists(sym2 => sym1.name == sym2.name && + !isDistinguishableFrom(toType.memberInfo(sym1), sym.info.memberInfo(sym2)))) debug(" -> full type: " + toType) if (constraints.length != 0) { @@ -498,4 +498,22 @@ trait ModelFactoryImplicitSupport { (aSym.isMethod || aSym.isGetter || aSym.isSetter) && (aSym.nameString != "getClass") } + + /* To put it very bluntly: checks if you can call implicitly added method with t1 when t2 is already there in the + * class. We suppose the name of the two members coincides + * + * The trick here is that the resultType does not matter - the condition for removal it that paramss have the same + * structure (A => B => C may not override (A, B) => C) and that all the types involved are + * of the implcit conversion's member are subtypes of the parent members' parameters */ + def isDistinguishableFrom(t1: Type, t2: Type): Boolean = + if (t1.paramss.map(_.length) == t2.paramss.map(_.length)) { + for ((t1p, t2p) <- t1.paramss.flatten zip t2.paramss.flatten) + if (!isSubType(t1 memberInfo t1p, t2 memberInfo t2p)) + return true // if on the corresponding parameter you give a type that is in t1 but not in t2 + // example: + // def foo(a: Either[Int, Double]): Int = 3 + // def foo(b: Left[T1]): Int = 6 + // a.foo(Right(4.5d)) prints out 3 :) + false + } else true // the member structure is different foo(3, 5) vs foo(3)(5) } \ No newline at end of file diff --git a/test/scaladoc/resources/implicits-base-res.scala b/test/scaladoc/resources/implicits-base-res.scala index ce86ba8918..3e3d0f01a6 100644 --- a/test/scaladoc/resources/implicits-base-res.scala +++ b/test/scaladoc/resources/implicits-base-res.scala @@ -11,20 +11,22 @@ trait MyNumeric[R] * - tests the complete type inference * - the following inherited methods should appear: * {{{ - * def convToGtColonDoubleA: Double // pimpA3: with a constraint that T <: Double - * def convToIntA: Int // pimpA2: with a constraint that T = Int - * def convToManifestA: T // pimpA7: with 2 constraints: T: Manifest and T <: Double - * def convToMyNumericA: T // pimpA6: with a constraint that there is x: MyNumeric[T] implicit in scope - * def convToNumericA: T // pimpA1: with a constraint that there is x: Numeric[T] implicit in scope - * def convToPimpedA: Bar[Foo[T]] // pimpA5: no constraints - * def convToPimpedA: S // pimpA4: with 3 constraints: T = Foo[Bar[S]], S: Foo and S: Bar - * def convToTraversableOps: T // pimpA7: with 2 constraints: T: Manifest and T <: Double - * // should not be abstract! + * def convToGtColonDoubleA(x: Double) // pimpA3: with a constraint that T <: Double + * def convToIntA(x: Int) // pimpA2: with a constraint that T = Int + * def convToManifestA(x: T) // pimpA7: with 2 constraints: T: Manifest and T <: Double + * def convToMyNumericA(x: T) // pimpA6: with a constraint that there is x: MyNumeric[T] implicit in scope + * def convToNumericA(x: T) // pimpA1: with a constraint that there is x: Numeric[T] implicit in scope + * def convToPimpedA(x: Bar[Foo[T]]) // pimpA5: no constraints + * def convToPimpedA(x: S) // pimpA4: with 3 constraints: T = Foo[Bar[S]], S: Foo and S: Bar + * def convToTraversableOps(x: T) // pimpA7: with 2 constraints: T: Manifest and T <: Double + * // should not be abstract! * }}} */ class A[T] { /** This should prevent the implicitly inherited `def convToPimpedA: T` from `pimpA0` from showing up */ - def convToPimpedA: T = sys.error("Let's check it out!") + def convToPimpedA(x: T): T = sys.error("Let's check it out!") + /** This should check implicit member elimination in the case of subtyping */ + def foo(a: T, b: AnyRef): T } /** Companion object with implicit transformations */ object A { @@ -38,7 +40,7 @@ object A { implicit def pimpA5[Z](a: A[Z]): PimpedA[Bar[Foo[Z]]] = sys.error("not implemented") implicit def pimpA6[Z: MyNumeric](a: A[Z]) = new MyNumericA[Z](a) // TODO: Add H <: Double and see why it crashes for C and D -- context bounds, need to check! - implicit def pimpA7[H <: Double : Manifest](a: A[H]) = new ManifestA[H](a) with MyTraversableOps[H] { def convToTraversableOps: H = sys.error("no") } + implicit def pimpA7[H <: Double : Manifest](a: A[H]) = new ManifestA[H](a) with MyTraversableOps[H] { def convToTraversableOps(x: H): H = sys.error("no") } } @@ -46,13 +48,13 @@ object A { * - tests the existential type solving * - the following inherited methods should appear: * {{{ - * def convToGtColonDoubleA: Double // pimpA3: no constraints - * def convToManifestA: Double // pimpA7: no constraints - * def convToMyNumericA: Double // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Double] implicit in scope - * def convToNumericA: Double // pimpA1: no constraintsd - * def convToPimpedA: Bar[Foo[Double]] // pimpA5: no constraints - * def convToTraversableOps: Double // pimpA7: no constraints - * // should not be abstract! + * def convToGtColonDoubleA(x: Double) // pimpA3: no constraints + * def convToManifestA(x: Double) // pimpA7: no constraints + * def convToMyNumericA(x: Double) // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Double] implicit in scope + * def convToNumericA(x: Double) // pimpA1: no constraintsd + * def convToPimpedA(x: Bar[Foo[Double]]) // pimpA5: no constraints + * def convToTraversableOps(x: Double) // pimpA7: no constraints + * // should not be abstract! * }}} */ class B extends A[Double] @@ -63,10 +65,10 @@ object B extends A * - tests asSeenFrom * - the following inherited methods should appear: * {{{ - * def convToIntA: Int // pimpA2: no constraints - * def convToMyNumericA: Int // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Int] implicit in scope - * def convToNumericA: Int // pimpA1: no constraints - * def convToPimpedA: Bar[Foo[Int]] // pimpA5: no constraints + * def convToIntA(x: Int) // pimpA2: no constraints + * def convToMyNumericA(x: Int) // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Int] implicit in scope + * def convToNumericA(x: Int) // pimpA1: no constraints + * def convToPimpedA(x: Bar[Foo[Int]]) // pimpA5: no constraints * }}} */ class C extends A[Int] @@ -77,9 +79,9 @@ object C extends A * - tests implicit elimination * - the following inherited methods should appear: * {{{ - * def convToMyNumericA: String // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[String] implicit in scope - * def convToNumericA: String // pimpA1: (if showAll is set) with a constraint that there is x: Numeric[String] implicit in scope - * def convToPimpedA: Bar[Foo[String]] // pimpA5: no constraints + * def convToMyNumericA(x: String) // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[String] implicit in scope + * def convToNumericA(x: String) // pimpA1: (if showAll is set) with a constraint that there is x: Numeric[String] implicit in scope + * def convToPimpedA(x: Bar[Foo[String]]) // pimpA5: no constraints * }}} */ class D extends A[String] @@ -92,7 +94,7 @@ object D extends A * - A, B and C should be implicitly converted to this */ class PimpedA[V](a: A[V]) { /** The convToPimpedA: V documentation... */ - def convToPimpedA: V = sys.error("Not implemented") + def convToPimpedA(x: V): V = sys.error("Not implemented") } /** NumericA class
@@ -100,7 +102,7 @@ class PimpedA[V](a: A[V]) { * - A, B and C should be implicitly converted to this */ class NumericA[U: Numeric](a: A[U]) { /** The convToNumericA: U documentation... */ - def convToNumericA: U = implicitly[Numeric[U]].zero + def convToNumericA(x: U): U = implicitly[Numeric[U]].zero } /** IntA class
@@ -108,7 +110,7 @@ class NumericA[U: Numeric](a: A[U]) { * - A and C should be implicitly converted to this */ class IntA(a: A[Int]) { /** The convToIntA: Int documentation... */ - def convToIntA: Int = 0 + def convToIntA(x: Int): Int = 0 } /** GtColonDoubleA class
@@ -116,7 +118,7 @@ class IntA(a: A[Int]) { * - A and B should be implicitly converted to this */ class GtColonDoubleA(a: A[T] forSome { type T <: Double }) { /** The convToGtColonDoubleA: Double documentation... */ - def convToGtColonDoubleA: Double = 0 + def convToGtColonDoubleA(x: Double): Double = 0 } /** MyNumericA class
@@ -124,7 +126,7 @@ class GtColonDoubleA(a: A[T] forSome { type T <: Double }) { * - A should be implicitly converted to this */ class MyNumericA[U: MyNumeric](a: A[U]) { /** The convToMyNumericA: U documentation... */ - def convToMyNumericA: U = sys.error("dunno") + def convToMyNumericA(x: U): U = sys.error("dunno") } /** ManifestA class
@@ -132,7 +134,7 @@ class MyNumericA[U: MyNumeric](a: A[U]) { * - A, B, C, D should be implicitly converted to this */ class ManifestA[W: Manifest](a: A[W]) { /** The convToManifestA: W documentation... */ - def convToManifestA: W = sys.error("dunno") + def convToManifestA(x: W): W = sys.error("dunno") } /** MyTraversableOps class
@@ -140,6 +142,6 @@ class ManifestA[W: Manifest](a: A[W]) { */ trait MyTraversableOps[S] { /** The convToTraversableOps: S documentation... */ - def convToTraversableOps: S + def convToTraversableOps(x: S): S } diff --git a/test/scaladoc/resources/implicits-shadowing-res.scala b/test/scaladoc/resources/implicits-shadowing-res.scala new file mode 100644 index 0000000000..c5e9493bf3 --- /dev/null +++ b/test/scaladoc/resources/implicits-shadowing-res.scala @@ -0,0 +1,64 @@ +/** + * Test scaladoc implicits distinguishing -- supress all members by implicit conversion that are shadowed by the + * class' own members + * + * {{{ + * scala> class A { def foo(t: String) = 4 } + * defined class A + * + * scala> class B { def foo(t: Any) = 5 } + * defined class B + * + * scala> implicit def AtoB(a:A) = new B + * AtoB: (a: A)B + * + * scala> val a = new A + * a: A = A@28f553e3 + * + * scala> a.foo("T") + * res1: Int = 4 + * + * scala> a.foo(4) + * res2: Int = 5 + * }}} + */ +package scala.test.scaladoc.implicits.shadowing +import language.implicitConversions // according to SIP18 + +/** conv5, conv8, conv9, conv10, conv11 should be visible */ +class A[T] { + def conv1: AnyRef = ??? + def conv2: T = ??? + def conv3(l: Int): AnyRef = ??? + def conv4(l: AnyRef): AnyRef = ??? + def conv5(l: String): AnyRef = ??? + def conv6(l: AnyRef): AnyRef = ??? + def conv7(l: AnyRef): String = ??? + def conv8(l: String)(m: String): AnyRef = ??? + def conv9(l: AnyRef)(m: AnyRef): AnyRef = ??? + def conv10(l: T): T = ??? + def conv11(l: T): T = ??? +} +/** conv5, conv8, conv9, conv11 should be visible */ +class B extends A[Int] +/** conv5, conv8, conv9, conv10, conv11 should be visible */ +class C extends A[Double] +/** conv5, conv8, conv9, conv10 should be visible */ +class D extends A[AnyRef] + +class Z[T] { + def conv1: AnyRef = ??? + def conv2: T = ??? + def conv3(p: Int): AnyRef = ??? + def conv4(p: String): AnyRef = ??? + def conv5(p: AnyRef): AnyRef = ??? + def conv6(p: AnyRef): String = ??? + def conv7(p: AnyRef): AnyRef = ??? + def conv8(p: String, q: String): AnyRef = ??? + def conv9(p: AnyRef, q: AnyRef): AnyRef = ??? + def conv10(p: Int): T = ??? + def conv11(p: String): T = ??? +} +object A { + implicit def AtoZ[T](a: A[T]) = new Z[T] +} diff --git a/test/scaladoc/run/implicits-shadowing.check b/test/scaladoc/run/implicits-shadowing.check new file mode 100644 index 0000000000..619c56180b --- /dev/null +++ b/test/scaladoc/run/implicits-shadowing.check @@ -0,0 +1 @@ +Done. diff --git a/test/scaladoc/run/implicits-shadowing.scala b/test/scaladoc/run/implicits-shadowing.scala new file mode 100644 index 0000000000..7835223d21 --- /dev/null +++ b/test/scaladoc/run/implicits-shadowing.scala @@ -0,0 +1,70 @@ +import scala.tools.nsc.doc.model._ +import scala.tools.partest.ScaladocModelTest + +object Test extends ScaladocModelTest { + + // test a file instead of a piece of code + override def resourceFile = "implicits-shadowing-res.scala" + + // start implicits + def scaladocSettings = "-implicits" + + def testModel(root: Package) = { + // get the quick access implicit defs in scope (_package(s), _class(es), _trait(s), object(s) _method(s), _value(s)) + import access._ + + // SEE THE test/resources/implicits-chaining-res.scala FOR THE EXPLANATION OF WHAT'S CHECKED HERE: + val base = root._package("scala")._package("test")._package("scaladoc")._package("implicits")._object("shadowing") + var conv: ImplicitConversion = null + +//// class A /////////////////////////////////////////////////////////////////////////////////////////////////////////// + + val A = base._class("A") + + conv = A._conversion(base._object("A").qualifiedName + ".AtoZ") + assert(conv.members.length == 5) + conv._member("conv5") + conv._member("conv8") + conv._member("conv9") + conv._member("conv10") + conv._member("conv11") + assert(conv.constraints.length == 0) + +//// class B /////////////////////////////////////////////////////////////////////////////////////////////////////////// + + val B = base._class("B") + + conv = B._conversion(base._object("A").qualifiedName + ".AtoZ") + assert(conv.members.length == 4) + conv._member("conv5") + conv._member("conv8") + conv._member("conv9") + conv._member("conv11") + assert(conv.constraints.length == 0) + +//// class C /////////////////////////////////////////////////////////////////////////////////////////////////////////// + + val C = base._class("C") + + conv = C._conversion(base._object("A").qualifiedName + ".AtoZ") + assert(conv.members.length == 5) + conv._member("conv5") + conv._member("conv8") + conv._member("conv9") + conv._member("conv10") + conv._member("conv11") + assert(conv.constraints.length == 0) + +//// class D /////////////////////////////////////////////////////////////////////////////////////////////////////////// + + val D = base._class("D") + + conv = D._conversion(base._object("A").qualifiedName + ".AtoZ") + assert(conv.members.length == 4) + conv._member("conv5") + conv._member("conv8") + conv._member("conv9") + conv._member("conv10") + assert(conv.constraints.length == 0) + } +} \ No newline at end of file -- cgit v1.2.3