Skip to content

Commit 0260d75

Browse files
authored
Merge pull request #15417 from dotty-staging/fix-15414
Allow for unqualified `apply` as context functions
2 parents 30f83f7 + 3a51e88 commit 0260d75

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,22 +3982,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39823982

39833983
/** Convert constructor proxy reference to a new expression */
39843984
def newExpr =
3985-
val Select(qual, nme.apply) = tree: @unchecked
3986-
val tycon = tree.tpe.widen.finalResultType.underlyingClassRef(refinementOK = false)
3985+
val qual = qualifier(tree)
39873986
val tpt = qual match
39883987
case Ident(name) =>
39893988
cpy.Ident(qual)(name.toTypeName)
39903989
case Select(pre, name) =>
39913990
cpy.Select(qual)(pre, name.toTypeName)
39923991
case qual: This if qual.symbol.is(ModuleClass) =>
39933992
cpy.Ident(qual)(qual.symbol.name.sourceModuleName.toTypeName)
3993+
val tycon = tree.tpe.widen.finalResultType.underlyingClassRef(refinementOK = false)
39943994
typed(
39953995
untpd.Select(
39963996
untpd.New(untpd.TypedSplice(tpt.withType(tycon))),
39973997
nme.CONSTRUCTOR),
39983998
pt)
39993999
.showing(i"convert creator $tree -> $result", typr)
40004000

4001+
def isApplyProxy(tree: Tree) = tree match
4002+
case Select(_, nme.apply) => tree.symbol.isAllOf(ApplyProxyFlags)
4003+
case _ => false
4004+
40014005
tree match {
40024006
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[?] | _: Closure => tree
40034007
case _ => tree.tpe.widen match {
@@ -4013,7 +4017,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
40134017
adaptOverloaded(ref)
40144018
}
40154019
case poly: PolyType if !(ctx.mode is Mode.Type) =>
4016-
if tree.symbol.isAllOf(ApplyProxyFlags) then newExpr
4020+
if isApplyProxy(tree) then newExpr
40174021
else if pt.isInstanceOf[PolyProto] then tree
40184022
else
40194023
var typeArgs = tree match
@@ -4027,7 +4031,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
40274031
readaptSimplified(handleStructural(tree))
40284032
else pt match {
40294033
case pt: FunProto =>
4030-
if tree.symbol.isAllOf(ApplyProxyFlags) then newExpr
4034+
if isApplyProxy(tree) then newExpr
40314035
else adaptToArgs(wtp, pt)
40324036
case pt: PolyProto if !wtp.isImplicitMethod =>
40334037
tryInsertApplyOrImplicit(tree, pt, locked)(tree) // error will be reported in typedTypeApply

tests/neg/i15414.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class C()
2+
object C {
3+
val c = apply() // error
4+
}

0 commit comments

Comments
 (0)