Skip to content

Commit 1541fd7

Browse files
committed
Error instead of crash when generating trees referring to skolems
Previously, the valueOf inline call succeeded (because the ValueOf synthesizer calls `ref` which calls `singleton`), leading to an invalid tree which crashed in the backend with: "assertion failed: Cannot create ClassBType from NoSymbol"
1 parent 32bfd5f commit 1541fd7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
517517
def singleton(tp: Type, needLoad: Boolean = true)(using Context): Tree = tp.dealias match {
518518
case tp: TermRef => ref(tp, needLoad)
519519
case tp: ThisType => This(tp.cls)
520-
case tp: SkolemType => singleton(tp.narrow(), needLoad)
521520
case SuperType(qual, _) => singleton(qual, needLoad)
522521
case ConstantType(value) => Literal(value)
522+
case tp: SkolemType =>
523+
throw TypeError(em"cannot construct a tree referring to skolem $tp")
523524
}
524525

525526
/** A tree representing a `newXYZArray` operation of the right

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,12 @@ object Synthesizer:
828828

829829
/** Tuple used to store the synthesis result with a list of errors. */
830830
type TreeWithErrors = (Tree, List[String])
831-
private def withNoErrors(tree: Tree): TreeWithErrors = (tree, List.empty)
831+
private inline def withNoErrors(inline tree: => Tree): TreeWithErrors =
832+
try
833+
(tree, List.empty)
834+
catch
835+
case tp: TypeError =>
836+
withErrors(tp.getMessage)
832837
private def withErrors(errors: String*): TreeWithErrors = (EmptyTree, errors.toList)
833838

834839
private val EmptyTreeNoError: TreeWithErrors = withNoErrors(EmptyTree)

tests/neg/valueOf-skolem.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
case class Foo(
2+
aaaa: Int
3+
)
4+
5+
case class Bar(
6+
foo: Foo,
7+
bar: Bla[foo.aaaa.type]
8+
)
9+
10+
class Bla[T](using Ev[T])
11+
12+
class Ev[T](x: T)
13+
object Ev:
14+
inline given ev: [T] => Ev[T] =
15+
Ev(valueOf[T])
16+
17+
object Test:
18+
def test: Unit =
19+
val x =
20+
Bar(
21+
Foo(0),
22+
Bla() // error
23+
)

0 commit comments

Comments
 (0)