Skip to content

Commit e90d376

Browse files
oderskyWojciechMazur
authored andcommitted
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. [Cherry-picked f995a8c]
1 parent 68127fd commit e90d376

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
@@ -4267,7 +4267,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42674267
case _ =>
42684268
adaptOverloaded(ref)
42694269
}
4270-
case poly: PolyType if !(ctx.mode is Mode.Type) =>
4270+
case poly: PolyType
4271+
if !(ctx.mode is Mode.Type) && dummyTreeOfType.unapply(tree).isEmpty =>
4272+
// If we are in a conversion from a TermRef with polymorphic underlying
4273+
// type, give up. In this case the typed `null` literal cannot be instantiated.
4274+
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
4275+
// We leave test for this condition in as a defensive measure in case
4276+
// it arises somewhere else.
42714277
if isApplyProxy(tree) then newExpr
42724278
else if pt.isInstanceOf[PolyProto] then tree
42734279
else

0 commit comments

Comments
 (0)