diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 095ccd29bdfe..076e49027281 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1548,17 +1548,21 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(implicit ctx: Context): Tree = { val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto - var res = typed(qual, pt1) - if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) { - ctx.errorOrMigrationWarning(i"not a function: ${res.tpe}; cannot be followed by `_'", tree.pos) - if (ctx.scala2Mode) { - // Under -rewrite, patch `x _` to `(() => x)` - patch(Position(tree.pos.start), "(() => ") - patch(Position(qual.pos.end, tree.pos.end), ")") - res = typed(untpd.Function(Nil, untpd.TypedSplice(res))) - } + val nestedCtx = ctx.fresh.setNewTyperState() + val res = typed(qual, pt1)(nestedCtx) + res match { + case res @ closure(_, _, _) => + case _ => + ctx.errorOrMigrationWarning(i"not a function: $qual; cannot be followed by `_'", tree.pos) + if (ctx.scala2Mode) { + // Under -rewrite, patch `x _` to `(() => x)` + patch(Position(tree.pos.start), "(() => ") + patch(Position(qual.pos.end, tree.pos.end), ")") + return typed(untpd.Function(Nil, qual), pt) + } } - else if (ctx.settings.strict.value) { + nestedCtx.typerState.commit() + if (ctx.settings.strict.value) { lazy val (prefix, suffix) = res match { case Block(mdef @ DefDef(_, _, vparams :: Nil, _, _) :: Nil, _: Closure) => val arity = vparams.length diff --git a/tests/neg/i3246.scala b/tests/neg/i3246.scala new file mode 100644 index 000000000000..e98db368695b --- /dev/null +++ b/tests/neg/i3246.scala @@ -0,0 +1,4 @@ +class Test { + def foo(x: Int) = 1 + val bar: () => Int = foo _ +} diff --git a/tests/pos-scala2/i3246.scala b/tests/pos-scala2/i3246.scala new file mode 100644 index 000000000000..1a263bcea58b --- /dev/null +++ b/tests/pos-scala2/i3246.scala @@ -0,0 +1,7 @@ +class Test { + def foo(x: => Int) = bar(x _) + def bar(x: () => Int) = ??? + def baz = 1 + def bam: () => Int = baz _ + def ban: () => Int = 1 _ +}