Skip to content

Commit ce5bf5a

Browse files
committed
Refine condition when a lambda is synthetic
1 parent 2642d2e commit ce5bf5a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,8 +3823,17 @@ class Typer extends Namer
38233823
&& isPureExpr(tree)
38243824
&& !isSelfOrSuperConstrCall(tree)
38253825
then tree match
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
3826+
case closureDef(meth)
3827+
if meth.span == meth.rhs.span.toSynthetic
3828+
&& !original.isInstanceOf[untpd.Function] =>
3829+
// It's a synthesized lambda, for instance via an eta expansion: report a hard error
3830+
// There are two tests for synthetic lambdas which both have to be true.
3831+
// The first test compares spans of closure definition with the closure's right hand
3832+
// side. This is usually accurate but can fail for compiler-generated test code.
3833+
// See repl.DocTests for two failing tests. The second tests rules out closures
3834+
// if the original tree was a lambda. This does not work always either since
3835+
// sometimes we do not have the original anymore and use the transformed tree instead.
3836+
// But taken together, the two criteria are quite accurate.
38283837
missingArgs(tree, tree.tpe.widen)
38293838
case _ =>
38303839
report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)

0 commit comments

Comments
 (0)