Skip to content

Commit 781b9b1

Browse files
committed
Disallow lambdas in statement position
Disallow lambdas in statement position or if the expected type is Unit. Fixes #11671
1 parent 66e180d commit 781b9b1

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ object ErrorReporting {
4343
def wrongNumberOfTypeArgs(fntpe: Type, expectedArgs: List[ParamInfo], actual: List[untpd.Tree], pos: SrcPos)(using Context): ErrorType =
4444
errorType(WrongNumberOfTypeArgs(fntpe, expectedArgs, actual), pos)
4545

46+
def missingArgs(tree: Tree, mt: Type)(using Context): Unit =
47+
val meth = err.exprStr(methPart(tree))
48+
mt match
49+
case mt: MethodType if mt.paramNames.isEmpty =>
50+
report.error(MissingEmptyArgumentList(meth), tree.srcPos)
51+
case _ =>
52+
report.error(em"missing arguments for $meth", tree.srcPos)
53+
4654
class Errors(using Context) {
4755

4856
/** An explanatory note to be added to error messages

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,12 +3107,9 @@ class Typer extends Namer
31073107
def readapt(tree: Tree, shouldTryGadtHealing: Boolean = tryGadtHealing)(using Context) = adapt(tree, pt, locked, shouldTryGadtHealing)
31083108
def readaptSimplified(tree: Tree)(using Context) = readapt(simplify(tree, pt, locked))
31093109

3110-
def missingArgs(mt: MethodType) = {
3111-
val meth = err.exprStr(methPart(tree))
3112-
if (mt.paramNames.length == 0) report.error(MissingEmptyArgumentList(meth), tree.srcPos)
3113-
else report.error(em"missing arguments for $meth", tree.srcPos)
3110+
def missingArgs(mt: MethodType) =
3111+
ErrorReporting.missingArgs(tree, mt)
31143112
tree.withType(mt.resultType)
3115-
}
31163113

31173114
def adaptOverloaded(ref: TermRef) = {
31183115
val altDenots =
@@ -3406,19 +3403,19 @@ class Typer extends Namer
34063403
// - we reference a typelevel method
34073404
// - we are in a pattern
34083405
// - the current tree is a synthetic apply which is not expandable (eta-expasion would simply undo that)
3409-
if (arity >= 0 &&
3410-
!tree.symbol.isConstructor &&
3411-
!tree.symbol.isAllOf(InlineMethod) &&
3412-
!ctx.mode.is(Mode.Pattern) &&
3413-
!(isSyntheticApply(tree) && !functionExpected)) {
3406+
if arity >= 0
3407+
&& !tree.symbol.isConstructor
3408+
&& !tree.symbol.isAllOf(InlineMethod)
3409+
&& !ctx.mode.is(Mode.Pattern)
3410+
&& !(isSyntheticApply(tree) && !functionExpected)
3411+
then
34143412
if (!defn.isFunctionType(pt))
34153413
pt match {
34163414
case SAMType(_) if !pt.classSymbol.hasAnnotation(defn.FunctionalInterfaceAnnot) =>
34173415
report.warning(ex"${tree.symbol} is eta-expanded even though $pt does not have the @FunctionalInterface annotation.", tree.srcPos)
34183416
case _ =>
34193417
}
34203418
simplify(typed(etaExpand(tree, wtp, arity), pt), pt, locked)
3421-
}
34223419
else if (wtp.paramInfos.isEmpty && isAutoApplied(tree.symbol))
34233420
readaptSimplified(tpd.Apply(tree, Nil))
34243421
else if (wtp.isImplicitMethod)
@@ -3825,8 +3822,9 @@ class Typer extends Namer
38253822
&& !tree.isInstanceOf[Inlined]
38263823
&& isPureExpr(tree)
38273824
&& !isSelfOrSuperConstrCall(tree)
3828-
then
3829-
report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)
3825+
then tree match
3826+
case closureDef(_) => missingArgs(tree, tree.tpe.widen)
3827+
case _ => report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)
38303828

38313829
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
38323830
private def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree =

tests/neg/i11671.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def go(x: Int): Unit =
2+
go // error
3+
go // error
4+
go // error
5+
6+
def foo: Unit =
7+
(x: Int) => go(x) // error
8+

0 commit comments

Comments
 (0)