Skip to content

Commit 8424af8

Browse files
committed
Only disallow synthesized lambdas in statement position
For user-written lambdas, presumably the devs know what they are doing. User-written numbers aappear in quite a lot of test cases. A second change of this commit is that closure methods now get a more accurate span.
1 parent 781b9b1 commit 8424af8

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,9 +1313,10 @@ object desugar {
13131313
* def $anonfun(params) = body
13141314
* Closure($anonfun)
13151315
*/
1316-
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree = null, isContextual: Boolean)(using Context): Block =
1316+
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree = null, isContextual: Boolean, span: Span)(using Context): Block =
13171317
Block(
13181318
DefDef(nme.ANON_FUN, params :: Nil, if (tpt == null) TypeTree() else tpt, body)
1319+
.withSpan(span)
13191320
.withMods(synthetic | Artifact),
13201321
Closure(Nil, Ident(nme.ANON_FUN), if (isContextual) ContextualEmptyTree else EmptyTree))
13211322

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ class Typer extends Namer
13491349
else cpy.ValDef(param)(
13501350
tpt = untpd.TypeTree(
13511351
inferredParamType(param, protoFormal(i)).translateFromRepeated(toArray = false)))
1352-
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual)
1352+
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual, tree.span)
13531353
}
13541354
typed(desugared, pt)
13551355
}
@@ -3823,8 +3823,11 @@ class Typer extends Namer
38233823
&& isPureExpr(tree)
38243824
&& !isSelfOrSuperConstrCall(tree)
38253825
then tree match
3826-
case closureDef(_) => missingArgs(tree, tree.tpe.widen)
3827-
case _ => report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)
3826+
case closureDef(meth) if meth.span == meth.rhs.span.toSynthetic =>
3827+
// it's a synthesized lambda, for instance via an eta expansion: report a hard error
3828+
missingArgs(tree, tree.tpe.widen)
3829+
case _ =>
3830+
report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)
38283831

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

tests/neg-custom-args/erased/erased-pathdep-1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
object Test {
44

55
fun1(new Bar)
6-
fun2(new Bar)
7-
fun3(new Bar)
6+
val _ = fun2(new Bar)
7+
val _ = fun3(new Bar)
88

99
def fun1[F >: Bar <: Foo](erased f: F): f.X = null.asInstanceOf[f.X] // error // error
1010
def fun2[F >: Bar <: Foo](erased f: F)(erased bar: f.B): f.B = null.asInstanceOf[f.B] // error // error // error

tests/neg/i11671.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ def go(x: Int): Unit =
44
go // error
55

66
def foo: Unit =
7-
(x: Int) => go(x) // error
7+
(x: Int) => go(x) // warning
88

tests/neg/i5311.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E007] Type Mismatch Error: tests/neg/i5311.scala:11:9 --------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/i5311.scala:11:8 --------------------------------------------------------------
22
11 | baz((x : s.T[Int]) => x) // error
3-
| ^^^^^^^^^^^^^^^^^^
4-
| Found: s.T[Int] => s.T[Int]
5-
| Required: m.Foo
3+
| ^^^^^^^^^^^^^^^^^^^
4+
| Found: s.T[Int] => s.T[Int]
5+
| Required: m.Foo
66

77
longer explanation available when compiling with `-explain`

tests/neg/i7359-g.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E007] Type Mismatch Error: tests/neg/i7359-g.scala:5:25 ------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/i7359-g.scala:5:19 ------------------------------------------------------------
22
5 |val m : SAMTrait = () => "Hello" // error
3-
| ^^^^^^^
4-
| Found: () => String
5-
| Required: SAMTrait
3+
| ^^^^^^^^^^^^^
4+
| Found: () => String
5+
| Required: SAMTrait
66

77
longer explanation available when compiling with `-explain`

tests/pos/i3873.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test {
22
inline def sum2(ys: List[Int]): Unit = {
3-
ys.foldLeft(1)
3+
val _ = ys.foldLeft(1)
44
}
55
val h1 = (xs: List[Int]) => sum2(xs)
66
}

tests/pos/typedapply.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ object typedapply {
66

77
foo[Int, String](1, "abc")
88

9-
foo[Int, String] _
9+
val x = foo[Int, String] _
1010

1111
}

0 commit comments

Comments
 (0)