From 43818d4e5d8369387e7b315eafde01aae73acaa6 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Thu, 18 Dec 2014 00:47:00 -0800 Subject: SI-7623 Trailing sequence wildcard warning An -Xlint:stars-align warning for the case of patterns with at least one "fixed" component and a varargs component. Warn if the fixed patterns don't exactly align with the fixed value components, such that a sequence wildcard aligns exactly with the varargs component (either a T* parameter in a case class or a Seq[T] in an extractor result). This addresses the case of the xml.Elem extractor, which does not correspond to the Elem class constructor. One can be fooled into supplying an extra field for extraction. Vanilla extractors of type `Option[Seq[_]]` are unaffected by this flag. It's OK to ask for `case X(a, b, c)` in the expectation that three results are forthcoming. There is no semantic confusion over where the varargs begin. --- src/compiler/scala/tools/nsc/settings/Warnings.scala | 13 ++++++++----- .../tools/nsc/transform/patmat/ScalacPatternExpanders.scala | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/settings/Warnings.scala b/src/compiler/scala/tools/nsc/settings/Warnings.scala index d174dc86c7..41ce0837cb 100644 --- a/src/compiler/scala/tools/nsc/settings/Warnings.scala +++ b/src/compiler/scala/tools/nsc/settings/Warnings.scala @@ -28,10 +28,11 @@ trait Warnings { val warnUnusedImport = BooleanSetting("-Ywarn-unused-import", "Warn when imports are unused.") // Experimental lint warnings that are turned off, but which could be turned on programmatically. - // These warnings are said to blind those who dare enable them. - // They are not activated by -Xlint and can't be enabled on the command line. - val warnValueOverrides = { // Currently turned off as experimental. Created using constructor (new BS), so not available on the command line. - val flag = new BooleanSetting("value-overrides", "Generated value class method overrides an implementation") + // They are not activated by -Xlint and can't be enabled on the command line because they are not + // created using the standard factory methods. + + val warnValueOverrides = { + val flag = new BooleanSetting("value-overrides", "Generated value class method overrides an implementation.") flag.value = false flag } @@ -53,10 +54,11 @@ trait Warnings { val TypeParameterShadow = LintWarning("type-parameter-shadow", "A local type parameter shadows a type already in scope.") val PolyImplicitOverload = LintWarning("poly-implicit-overload", "Parameterized overloaded implicit methods are not visible as view bounds.") val OptionImplicit = LintWarning("option-implicit", "Option.apply used implicit view.") - val DelayedInitSelect = LintWarning("delayedinit-select", "Selecting member of DelayedInit") + val DelayedInitSelect = LintWarning("delayedinit-select", "Selecting member of DelayedInit.") val ByNameRightAssociative = LintWarning("by-name-right-associative", "By-name parameter of right associative operator.") val PackageObjectClasses = LintWarning("package-object-classes", "Class or object defined in package object.") val UnsoundMatch = LintWarning("unsound-match", "Pattern match may not be typesafe.") + val StarsAlign = LintWarning("stars-align", "Pattern sequence wildcard must align with sequence component.") def allLintWarnings = values.toSeq.asInstanceOf[Seq[LintWarning]] } @@ -77,6 +79,7 @@ trait Warnings { def warnByNameRightAssociative = lint contains ByNameRightAssociative def warnPackageObjectClasses = lint contains PackageObjectClasses def warnUnsoundMatch = lint contains UnsoundMatch + def warnStarsAlign = lint contains StarsAlign // Lint warnings that are currently -Y, but deprecated in that usage @deprecated("Use warnAdaptedArgs", since="2.11.2") diff --git a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala index 8924394b72..2753baa51d 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala @@ -110,8 +110,10 @@ trait ScalacPatternExpanders { err("Star pattern must correspond with varargs or unapplySeq") else if (elementArity < 0) arityError("not enough") - else if (elementArity > 0 && !extractor.hasSeq) + else if (elementArity > 0 && !isSeq) arityError("too many") + else if (settings.warnStarsAlign && isSeq && productArity > 0 && (elementArity > 0 || !isStar)) + warn("A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).") aligned } -- cgit v1.2.3