Skip to content

Commit 2acee5b

Browse files
oderskyliufengyun
authored andcommitted
Fix #3246: Refine handling of postfix _ for non-functions
1 parent 6398448 commit 2acee5b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,17 +1548,19 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
15481548
def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(implicit ctx: Context): Tree = {
15491549
val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree
15501550
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
1551-
var res = typed(qual, pt1)
1552-
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
1551+
val nestedCtx = ctx.fresh.setNewTyperState()
1552+
var res = typed(qual, pt1)(nestedCtx)
1553+
if (!defn.isFunctionClass(res.tpe.classSymbol)) {
15531554
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(res.tpe), tree.pos)
15541555
if (ctx.scala2Mode) {
15551556
// Under -rewrite, patch `x _` to `(() => x)`
15561557
patch(Position(tree.pos.start), "(() => ")
15571558
patch(Position(qual.pos.end, tree.pos.end), ")")
1558-
res = typed(untpd.Function(Nil, untpd.TypedSplice(res)))
1559+
return typed(untpd.Function(Nil, res))
15591560
}
15601561
}
1561-
else if (ctx.settings.strict.value) {
1562+
nestedCtx.typerState.commit()
1563+
if (ctx.settings.strict.value) {
15621564
lazy val (prefix, suffix) = res match {
15631565
case Block(mdef @ DefDef(_, _, vparams :: Nil, _, _) :: Nil, _: Closure) =>
15641566
val arity = vparams.length

tests/pos-scala2/i3246.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Test {
2+
def foo(x: => Int) = bar(x _)
3+
def bar(x: () => Int) = ???
4+
def baz = 1
5+
def bam: () => Int = baz _
6+
def ban: () => Int = 1 _
7+
}

0 commit comments

Comments
 (0)