Skip to content

Warn when matching against an opaque type #10664

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

Merged
merged 3 commits into from
Dec 6, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ object PickleBuffer {
val termMap, typeMap = new Array[Long](64)
for (idx <- 0 until ScalaFlagEnd)
corr get (1L << idx) match {
case Some((termFlag: FlagSet, typeFlag: FlagSet)) =>
case Some((termFlag: FlagSet @unchecked, typeFlag: FlagSet @unchecked)) =>
termMap(idx) |= termFlag.bits
typeMap(idx) |= typeFlag.bits
case Some(commonFlag: FlagSet) =>
case Some(commonFlag: FlagSet @unchecked) =>
termMap(idx) |= commonFlag.toTermFlags.bits
typeMap(idx) |= commonFlag.toTypeFlags.bits
case _ =>
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ object TypeTestsCasts {
* 7. if `P` is a refinement type, FALSE
* 8. otherwise, TRUE
*/
def checkable(X: Type, P: Type, span: Span)(using Context): Boolean = {
def checkable(X: Type, P: Type, span: Span)(using Context): Boolean = atPhase(Phases.refchecksPhase.next) {
// Run just before ElimOpaque transform (which follows RefChecks)
def isAbstract(P: Type) = !P.dealias.typeSymbol.isClass

def replaceP(tp: Type)(using Context) = new TypeMap {
Expand All @@ -71,7 +72,7 @@ object TypeTestsCasts {
case tref: TypeRef if tref.typeSymbol.isPatternBound =>
if (variance == 1) tref.info.hiBound
else if (variance == -1) tref.info.loBound
else OrType(defn.AnyType, defn.NothingType, soft = true)
else OrType(defn.AnyType, defn.NothingType, soft = true) // TODO: what does this line do?
case _ => mapOver(tp)
}
}.apply(tp)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object Tuple {
/** Convert an immutable array into a tuple of unknown arity and types */
def fromIArray[T](xs: IArray[T]): Tuple = {
val xs2: IArray[Object] = xs match {
case xs: IArray[Object] => xs
case xs: IArray[Object] @unchecked => xs
case xs =>
// TODO support IArray.map
xs.asInstanceOf[Array[T]].map(_.asInstanceOf[Object]).asInstanceOf[IArray[Object]]
Expand Down
26 changes: 26 additions & 0 deletions tests/neg-custom-args/fatal-warnings/opaque-match.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
case class C()

object O:
opaque type T <: C = C
val x: T = C()
(??? : Any) match
case _: T => ??? // OK

def Test[T] =
O.x match
case _: C => ??? // ok
C() match
case _: O.T => ??? // error
C() match
case _: T => ??? // error

(??? : Any) match
case _: List[O.T] => ??? // error
(??? : Any) match
case _: List[O.T @unchecked] => ??? // OK
(??? : Any) match
case _: List[T] => ??? // error