Skip to content

Commit 8239bb6

Browse files
committed
add safety for unknown type
1 parent 48ab2ea commit 8239bb6

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,22 @@ object TypeOps:
873873
end healPrefix
874874

875875
/** lift the prefix of an applied type, without normalisation or forcing */
876-
def appliedTypePrefix(tpe: Type): Type =
876+
def appliedTypePrefix(tpe: Type)(using Context): Option[Type] =
877877
tpe match
878-
case AppliedType(tycon, _) => appliedTypePrefix(tycon)
879-
case tpe: NamedType => tpe.prefix
880-
case tpe: ThisType => tpe.tref.prefix
881-
case _ => NoPrefix
878+
case tpe: NamedType => Some(tpe.prefix)
879+
case tpe: ThisType => appliedTypePrefix(tpe.tref)
880+
case tpe: AppliedType => appliedTypePrefix(tpe.tycon)
881+
case tpe: AnnotatedType => appliedTypePrefix(tpe.parent) // TODO: test
882+
case tpe: AndOrType => Some(NoPrefix) // TODO: test
883+
case _ =>
884+
// ignore HKTypeLambda/TypeBounds
885+
// what do do about
886+
// - MatchType -- will this occur or will it be reduced?
887+
// - TypeParamRef -- likely collapse to bounds?
888+
// - TypeVar/ExprType -- when can this occur
889+
// - RecThis -- what is the prefix of this
890+
// - RefinedOrRecType -- do we need to refine the children?
891+
// - AnnotatedType -- do we need to annotate the children?
892+
None
882893

883894
end TypeOps

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
236236
monoMap(mirroredType.resultType)
237237

238238
private def productMirror(mirroredType: Type, formal: Type, span: Span)(using Context): Tree =
239-
val pre = TypeOps.appliedTypePrefix(mirroredType)
239+
val pre = TypeOps.appliedTypePrefix(mirroredType) match
240+
case Some(pre) => pre
241+
case _ => return EmptyTree
240242
mirroredType match
241243
case AndType(tp1, tp2) =>
242244
productMirror(tp1, formal, span).orElse(productMirror(tp2, formal, span))
@@ -285,7 +287,9 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
285287
end productMirror
286288

287289
private def sumMirror(mirroredType: Type, formal: Type, span: Span)(using Context): Tree =
288-
val pre = TypeOps.appliedTypePrefix(mirroredType)
290+
val pre = TypeOps.appliedTypePrefix(mirroredType) match
291+
case Some(pre) => pre
292+
case _ => return EmptyTree
289293
val cls = mirroredType.classSymbol
290294
val useCompanion = cls.useCompanionAsMirror
291295
val declScope = if useCompanion then cls.linkedClass else ctx.owner

0 commit comments

Comments
 (0)