Skip to content

Commit

Permalink
Avoid crash arising from trying to find conversions from polymorphic …
Browse files Browse the repository at this point in the history
…singleton types

This is an alternative fix for scala#18695, which already got fixed in a different way by scala#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 scala#18719.
  • Loading branch information
odersky committed Oct 25, 2023
1 parent 38559d7 commit f995a8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 6 additions & 8 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,19 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def Apply(fn: Tree, args: List[Tree])(using Context): Apply = fn match
case Block(Nil, expr) =>
Apply(expr, args)
case _: RefTree | _: GenericApply | _: Inlined | _: Hole =>
ta.assignType(untpd.Apply(fn, args), fn, args)
case _ =>
assert(
fn.isInstanceOf[RefTree | GenericApply | Inlined | Hole] || ctx.reporter.errorsReported,
s"Illegal Apply function prefix: $fn"
)
assert(ctx.reporter.errorsReported)
ta.assignType(untpd.Apply(fn, args), fn, args)

def TypeApply(fn: Tree, args: List[Tree])(using Context): TypeApply = fn match
case Block(Nil, expr) =>
TypeApply(expr, args)
case _: RefTree | _: GenericApply =>
ta.assignType(untpd.TypeApply(fn, args), fn, args)
case _ =>
assert(
fn.isInstanceOf[RefTree | GenericApply] || ctx.reporter.errorsReported,
s"Illegal TypeApply function prefix: $fn"
)
assert(ctx.reporter.errorsReported, s"unexpected tree for type application: $fn")
ta.assignType(untpd.TypeApply(fn, args), fn, args)

def Literal(const: Constant)(using Context): Literal =
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4366,7 +4366,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case _ =>
adaptOverloaded(ref)
}
case poly: PolyType if !(ctx.mode is Mode.Type) =>
case poly: PolyType
if !(ctx.mode is Mode.Type) && dummyTreeOfType.unapply(tree).isEmpty =>
// If we are in a conversion from a TermRef with polymorphic underlying
// type, give up. In this case the typed `null` literal cannot be instantiated.
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
// We leave test for this condition in as a defensive measure in case
// it arises somewhere else.
if isApplyProxy(tree) then newExpr
else if pt.isInstanceOf[PolyProto] then tree
else
Expand Down

0 comments on commit f995a8c

Please sign in to comment.