Skip to content

Commit 90f7b0f

Browse files
authored
Merge pull request #7978 from dotty-staging/fix-#7977
Fix #7977: Disallow unsupported pattern in summonFrom
2 parents 37a954f + b1a64c9 commit 90f7b0f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,13 @@ trait Applications extends Compatibility {
915915
// applications of inline functions.
916916
tree.args match {
917917
case (arg @ Match(EmptyTree, cases)) :: Nil =>
918+
cases.foreach {
919+
case CaseDef(Typed(_: Ident, _), _, _) => // OK
920+
case CaseDef(Bind(_, Typed(_: Ident, _)), _, _) => // OK
921+
case CaseDef(Ident(name), _, _) if name == nme.WILDCARD => // Ok
922+
case CaseDef(pat, _, _) =>
923+
ctx.error("Unexpected pattern for summonFrom. Expeced `x: T` or `_`", pat.sourcePos)
924+
}
918925
typed(untpd.InlineMatch(EmptyTree, cases).withSpan(arg.span), pt)
919926
case _ =>
920927
errorTree(tree, em"argument to summonFrom must be a pattern matching closure")

tests/neg/i7977.scala

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import scala.compiletime.summonFrom
2+
class Main(b: Boolean) {
3+
inline def foo(): Any = {
4+
summonFrom {
5+
case x => println(x) // error
6+
}
7+
8+
summonFrom {
9+
case x if b => println(x) // error
10+
}
11+
12+
summonFrom {
13+
case Some(x) if b => println(x) // error
14+
}
15+
16+
summonFrom {
17+
case x if b => println(x) // error
18+
case _ => println()
19+
}
20+
21+
summonFrom { // ok but useless
22+
case _ if b => println()
23+
case _ => println()
24+
}
25+
}
26+
27+
foo()
28+
}

0 commit comments

Comments
 (0)