Skip to content

Commit e1edca9

Browse files
committed
Port Applications errors to the new scheme
1 parent 54af543 commit e1edca9

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
157157
AbstractCannotBeUsedForObjectsID,
158158
ModifierRedundantForObjectsID,
159159
TypedCaseDoesNotExplicitlyExtendTypedEnumID,
160-
IllegalRedefinitionOfStandardKindID
160+
IllegalRedefinitionOfStandardKindID,
161+
UnexpectedPatternForSummonFromID
161162

162163
def errorNumber = ordinal - 2
163164
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,4 +2469,25 @@ object messages {
24692469
| Please choose a different name to avoid conflicts
24702470
|""".stripMargin
24712471
}
2472+
2473+
case class UnexpectedPatternForSummonFrom(tree: Tree[_])(implicit ctx: Context)
2474+
extends Message(UnexpectedPatternForSummonFromID) {
2475+
val kind: String = "Syntax"
2476+
val msg: String = em"Unexpected pattern for summonFrom. Expected ${hl("`x: T`")} or ${hl("`_`")}"
2477+
2478+
val explanation: String =
2479+
em"""|The pattern "${tree.show}" provided in the ${hl("case")} expression of the ${hl("summonFrom")},
2480+
| needs to be of the form ${hl("`x: T`")} or ${hl("`_`")}.
2481+
|
2482+
| Example usage:
2483+
| inline def a = summonFrom {
2484+
| case x: T => ???
2485+
| }
2486+
|
2487+
| or
2488+
| inline def a = summonFrom {
2489+
| case _ => ???
2490+
| }
2491+
|""".stripMargin
2492+
}
24722493
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import config.Printers.{overload, typr, unapp}
2929
import TypeApplications._
3030

3131
import reporting.diagnostic.Message
32+
import reporting.diagnostic.messages.UnexpectedPatternForSummonFrom
3233
import reporting.trace
3334
import Constants.{Constant, IntTag, LongTag}
3435
import dotty.tools.dotc.reporting.diagnostic.messages.{UnapplyInvalidReturnType, NotAnExtractor, UnapplyInvalidNumberOfArguments}
@@ -916,7 +917,7 @@ trait Applications extends Compatibility {
916917
case CaseDef(Bind(_, Typed(_: Ident, _)), _, _) => // OK
917918
case CaseDef(Ident(name), _, _) if name == nme.WILDCARD => // Ok
918919
case CaseDef(pat, _, _) =>
919-
ctx.error("Unexpected pattern for summonFrom. Expeced `x: T` or `_`", pat.sourcePos)
920+
ctx.error(UnexpectedPatternForSummonFrom(pat), pat.sourcePos)
920921
}
921922
typed(untpd.InlineMatch(EmptyTree, cases).withSpan(arg.span), pt)
922923
case _ =>

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,4 +1805,38 @@ class ErrorMessagesTests extends ErrorMessagesTest {
18051805
assertEquals("class", kind)
18061806
assertEquals("Any", name.toString)
18071807
}
1808+
1809+
@Test def unexpectedPatternForSummonFrom =
1810+
checkMessagesAfter(RefChecks.name) {
1811+
"""import compiletime.summonFrom
1812+
|inline def a = summonFrom {
1813+
| case x => ???
1814+
|}
1815+
""".stripMargin
1816+
}
1817+
.expect { (ictx, messages)
1818+
implicit val ctx: Context = ictx
1819+
assertMessageCount(1, messages)
1820+
val errorMsg = messages.head.msg
1821+
val UnexpectedPatternForSummonFrom(x) :: Nil = messages
1822+
assertEquals("Unexpected pattern for summonFrom. Expected `x: T` or `_`", errorMsg)
1823+
assertEquals("x", x.show)
1824+
}
1825+
1826+
@Test def unexpectedPatternForSummonWithPatternBinder =
1827+
checkMessagesAfter(RefChecks.name) {
1828+
"""import compiletime.summonFrom
1829+
|inline def a = summonFrom {
1830+
| case x@String => ???
1831+
|}
1832+
""".stripMargin
1833+
}
1834+
.expect { (ictx, messages)
1835+
implicit val ctx: Context = ictx
1836+
assertMessageCount(1, messages)
1837+
val errorMsg = messages.head.msg
1838+
val UnexpectedPatternForSummonFrom(x) :: Nil = messages
1839+
assertEquals("Unexpected pattern for summonFrom. Expected `x: T` or `_`", errorMsg)
1840+
assertEquals("given x @ String", x.show)
1841+
}
18081842
}

tests/neg/SummonFrom.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- [E150] Syntax Error: tests/neg/SummonFrom.scala:4:7 -----------------------------------------------------------------
2+
4 | case x => ??? // error
3+
| ^
4+
| Unexpected pattern for summonFrom. Expected `x: T` or `_`
5+
6+
longer explanation available when compiling with `-explain`
7+
-- [E150] Syntax Error: tests/neg/SummonFrom.scala:8:7 -----------------------------------------------------------------
8+
8 | case x@String => ??? // error
9+
| ^^^^^^^^
10+
| Unexpected pattern for summonFrom. Expected `x: T` or `_`
11+
12+
longer explanation available when compiling with `-explain`

tests/neg/SummonFrom.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import compiletime.summonFrom
2+
3+
inline def a = summonFrom {
4+
case x => ??? // error
5+
}
6+
7+
inline def b = summonFrom {
8+
case x@String => ??? // error
9+
}

0 commit comments

Comments
 (0)