-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #8681: handle scrutinee of union types #8722
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ object TypeTestsCasts { | |
* 7. if `P` is a refinement type, FALSE | ||
* 8. otherwise, TRUE | ||
*/ | ||
def checkable(X: Type, P: Type, span: Span)(implicit ctx: Context): Boolean = { | ||
def checkable(X: Type, P: Type, span: Span)(using Context): Boolean = { | ||
def isAbstract(P: Type) = !P.dealias.typeSymbol.isClass | ||
def isPatternTypeSymbol(sym: Symbol) = !sym.isClass && sym.is(Case) | ||
|
||
|
@@ -155,7 +155,7 @@ object TypeTestsCasts { | |
res | ||
} | ||
|
||
def interceptTypeApply(tree: TypeApply)(implicit ctx: Context): Tree = trace(s"transforming ${tree.show}", show = true) { | ||
def interceptTypeApply(tree: TypeApply)(using Context): Tree = trace(s"transforming ${tree.show}", show = true) { | ||
/** Intercept `expr.xyz[XYZ]` */ | ||
def interceptWith(expr: Tree): Tree = | ||
if (expr.isEmpty) tree | ||
|
@@ -172,7 +172,9 @@ object TypeTestsCasts { | |
else if tp.isRef(defn.AnyValClass) then defn.AnyClass | ||
else tp.classSymbol | ||
|
||
def foundCls = effectiveClass(expr.tpe.widen) | ||
def foundClasses(tp: Type, acc: List[Symbol]): List[Symbol] = tp match | ||
case OrType(tp1, tp2) => foundClasses(tp2, foundClasses(tp1, acc)) | ||
case _ => effectiveClass(tp) :: acc | ||
|
||
def inMatch = | ||
tree.fun.symbol == defn.Any_typeTest || // new scheme | ||
|
@@ -181,7 +183,7 @@ object TypeTestsCasts { | |
def transformIsInstanceOf(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = { | ||
def testCls = effectiveClass(testType.widen) | ||
|
||
def unreachable(why: => String): Boolean = { | ||
def unreachable(why: => String)(using ctx: Context): Boolean = { | ||
if (flagUnrelated) | ||
if (inMatch) ctx.error(em"this case is unreachable since $why", expr.sourcePos) | ||
else ctx.warning(em"this will always yield false since $why", expr.sourcePos) | ||
|
@@ -191,7 +193,7 @@ object TypeTestsCasts { | |
/** Are `foundCls` and `testCls` classes that allow checks | ||
* whether a test would be always false? | ||
*/ | ||
def isCheckable = | ||
def isCheckable(foundCls: Symbol) = | ||
foundCls.isClass && testCls.isClass && | ||
!(testCls.isPrimitiveValueClass && !foundCls.isPrimitiveValueClass) && | ||
// if `test` is primitive but `found` is not, we might have a case like | ||
|
@@ -203,8 +205,8 @@ object TypeTestsCasts { | |
/** Check whether a runtime test that a value of `foundCls` can be a `testCls` | ||
* can be true in some cases. Issues a warning or an error otherwise. | ||
*/ | ||
def checkSensical: Boolean = | ||
if (!isCheckable) true | ||
def checkSensical(foundCls: Symbol)(using Context): Boolean = | ||
if (!isCheckable(foundCls)) true | ||
else if (foundCls.isPrimitiveValueClass && !testCls.isPrimitiveValueClass) { | ||
ctx.error("cannot test if value types are references", tree.sourcePos) | ||
false | ||
|
@@ -214,38 +216,49 @@ object TypeTestsCasts { | |
testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait) | ||
) | ||
if (foundCls.is(Final)) | ||
unreachable(i"$foundCls is not a subclass of $testCls") | ||
unreachable(i"type ${expr.tpe.widen} is not a subclass of $testCls") | ||
else if (unrelated) | ||
unreachable(i"$foundCls and $testCls are unrelated") | ||
unreachable(i"type ${expr.tpe.widen} and $testCls are unrelated") | ||
else true | ||
} | ||
else true | ||
|
||
if (expr.tpe <:< testType) | ||
if (expr.tpe.isNotNull) { | ||
if (!inMatch) ctx.warning(TypeTestAlwaysSucceeds(foundCls, testCls), tree.sourcePos) | ||
if (!inMatch) ctx.warning(TypeTestAlwaysSucceeds(expr.tpe, testType), tree.sourcePos) | ||
constant(expr, Literal(Constant(true))) | ||
} | ||
else expr.testNotNull | ||
else if (!checkSensical) | ||
constant(expr, Literal(Constant(false))) | ||
else if (testCls.isPrimitiveValueClass) | ||
if (foundCls.isPrimitiveValueClass) | ||
constant(expr, Literal(Constant(foundCls == testCls))) | ||
else { | ||
val nestedCtx = ctx.fresh.setNewTyperState() | ||
val foundClsSyms = foundClasses(expr.tpe.widen, Nil) | ||
val sensical = foundClsSyms.exists(sym => checkSensical(sym)(using nestedCtx)) | ||
if (!sensical) { | ||
nestedCtx.typerState.commit() | ||
constant(expr, Literal(Constant(false))) | ||
} | ||
else if (testCls.isPrimitiveValueClass ) | ||
if (foundClsSyms.size == 1 && foundClsSyms.head.isPrimitiveValueClass) | ||
constant(expr, Literal(Constant(foundClsSyms.head == testCls))) | ||
else | ||
transformIsInstanceOf(expr, defn.boxedType(testCls.typeRef), flagUnrelated) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be nicer as a pattern match |
||
else | ||
transformIsInstanceOf(expr, defn.boxedType(testCls.typeRef), flagUnrelated) | ||
else | ||
derivedTree(expr, defn.Any_isInstanceOf, testType) | ||
derivedTree(expr, defn.Any_isInstanceOf, testType) | ||
} | ||
} | ||
|
||
def transformAsInstanceOf(testType: Type): Tree = { | ||
def testCls = testType.widen.classSymbol | ||
def testCls = effectiveClass(testType.widen) | ||
def foundClsSymPrimitive = { | ||
val foundClsSyms = foundClasses(expr.tpe.widen, Nil) | ||
foundClsSyms.size == 1 && foundClsSyms.head.isPrimitiveValueClass | ||
} | ||
if (erasure(expr.tpe) <:< testType) | ||
Typed(expr, tree.args.head) // Replace cast by type ascription (which does not generate any bytecode) | ||
else if (testCls eq defn.BoxedUnitClass) | ||
// as a special case, casting to Unit always successfully returns Unit | ||
Block(expr :: Nil, Literal(Constant(()))).withSpan(expr.span) | ||
else if (foundCls.isPrimitiveValueClass) | ||
else if (foundClsSymPrimitive) | ||
if (testCls.isPrimitiveValueClass) primitiveConversion(expr, testCls) | ||
else derivedTree(box(expr), defn.Any_asInstanceOf, testType) | ||
else if (testCls.isPrimitiveValueClass) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
case class A(a: Int) | ||
case class B(b: Int) | ||
case class C(c: Int) | ||
|
||
val a = (A(1): A) match { | ||
case A(_) => "OK" | ||
case B(_) => "NOT OK" // error: this case is unreachable since class A and class B are unrelated | ||
} | ||
|
||
val b = (A(1): A | B) match { | ||
case A(_) => "OK" | ||
case B(_) => "OK" | ||
case C(_) => "NOT OK" // error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Error: tests/neg-custom-args/fatal-warnings/i8711.scala:7:9 --------------------------------------------------------- | ||
7 | case x: B => x // error: this case is unreachable since class A is not a subclass of class B | ||
| ^ | ||
| this case is unreachable since type A and class B are unrelated | ||
-- Error: tests/neg-custom-args/fatal-warnings/i8711.scala:12:9 -------------------------------------------------------- | ||
12 | case x: C => x // error | ||
| ^ | ||
| this case is unreachable since type A | B and class C are unrelated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class A | ||
class B | ||
class C | ||
|
||
object Test { | ||
def foo(x: A) = x match { | ||
case x: B => x // error: this case is unreachable since class A is not a subclass of class B | ||
case _ => | ||
} | ||
|
||
def bar(x: A | B) = x match { | ||
case x: C => x // error | ||
case _ => | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.