Skip to content

Commit f995a8c

Browse files
committed
Avoid crash arising from trying to find conversions from polymorphic singleton types
This is an alternative fix for #18695, which already got fixed in a different way by #18719. This PR adds the actual tests, and leaves in the fix as a defensive measure in case the situation arises by some other means than the one foxed in #18719.
1 parent 38559d7 commit f995a8c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,19 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
4545
def Apply(fn: Tree, args: List[Tree])(using Context): Apply = fn match
4646
case Block(Nil, expr) =>
4747
Apply(expr, args)
48+
case _: RefTree | _: GenericApply | _: Inlined | _: Hole =>
49+
ta.assignType(untpd.Apply(fn, args), fn, args)
4850
case _ =>
49-
assert(
50-
fn.isInstanceOf[RefTree | GenericApply | Inlined | Hole] || ctx.reporter.errorsReported,
51-
s"Illegal Apply function prefix: $fn"
52-
)
51+
assert(ctx.reporter.errorsReported)
5352
ta.assignType(untpd.Apply(fn, args), fn, args)
5453

5554
def TypeApply(fn: Tree, args: List[Tree])(using Context): TypeApply = fn match
5655
case Block(Nil, expr) =>
5756
TypeApply(expr, args)
57+
case _: RefTree | _: GenericApply =>
58+
ta.assignType(untpd.TypeApply(fn, args), fn, args)
5859
case _ =>
59-
assert(
60-
fn.isInstanceOf[RefTree | GenericApply] || ctx.reporter.errorsReported,
61-
s"Illegal TypeApply function prefix: $fn"
62-
)
60+
assert(ctx.reporter.errorsReported, s"unexpected tree for type application: $fn")
6361
ta.assignType(untpd.TypeApply(fn, args), fn, args)
6462

6563
def Literal(const: Constant)(using Context): Literal =

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -4366,7 +4366,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43664366
case _ =>
43674367
adaptOverloaded(ref)
43684368
}
4369-
case poly: PolyType if !(ctx.mode is Mode.Type) =>
4369+
case poly: PolyType
4370+
if !(ctx.mode is Mode.Type) && dummyTreeOfType.unapply(tree).isEmpty =>
4371+
// If we are in a conversion from a TermRef with polymorphic underlying
4372+
// type, give up. In this case the typed `null` literal cannot be instantiated.
4373+
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
4374+
// We leave test for this condition in as a defensive measure in case
4375+
// it arises somewhere else.
43704376
if isApplyProxy(tree) then newExpr
43714377
else if pt.isInstanceOf[PolyProto] then tree
43724378
else

0 commit comments

Comments
 (0)