Skip to content

Commit f8f5176

Browse files
committed
Harden typedUnaply
In the tested case, an unApply ended up with an inserted toplevel apply method, which confused the follow-on logic. This case is now rejected.
1 parent f7c46af commit f8f5176

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,19 +879,25 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
879879
*/
880880
def trySelectUnapply(qual: untpd.Tree)(fallBack: Tree => Tree): Tree = {
881881
// try first for non-overloaded, then for overloaded ocurrences
882-
def tryWithName(name: TermName)(fallBack: Tree => Tree)(implicit ctx: Context): Tree =
883-
tryEither { implicit ctx =>
884-
val specificProto = new UnapplyFunProto(selType, this)
885-
typedExpr(untpd.Select(qual, name), specificProto)
882+
def tryWithName(name: TermName)(fallBack: Tree => Tree)(implicit ctx: Context): Tree = {
883+
def tryWithProto(pt: Type)(implicit ctx: Context) = {
884+
val result = typedExpr(untpd.Select(qual, name), new UnapplyFunProto(pt, this))
885+
if (!result.symbol.exists || result.symbol.name == name) result
886+
else notAnExtractor(result)
887+
// It might be that the result of typedExpr is an `apply` selection or implicit conversion.
888+
// Reject in this case.
889+
}
890+
tryEither {
891+
implicit ctx => tryWithProto(selType)
886892
} {
887893
(sel, _) =>
888-
tryEither { implicit ctx =>
889-
val genericProto = new UnapplyFunProto(WildcardType, this)
890-
typedExpr(untpd.Select(qual, name), genericProto)
894+
tryEither {
895+
implicit ctx => tryWithProto(WildcardType)
891896
} {
892897
(_, _) => fallBack(sel)
893898
}
894899
}
900+
}
895901
// try first for unapply, then for unapplySeq
896902
tryWithName(nme.unapply) {
897903
sel => tryWithName(nme.unapplySeq)(_ => fallBack(sel)) // for backwards compatibility; will be dropped

tests/neg/parser-stability-20.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object x0 {
2+
def unapply= Array
3+
x0 match
4+
x0 // error
5+
case x0( // error // error

0 commit comments

Comments
 (0)