You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Eta-expand 0-arity method if expected type is Function0
Reverse course from the deprecation introduced in 2.12.
(4.3) condition for eta-expansion by -Xsource level:
- until 2.13:
- for arity > 0: function or sam type is expected
- for arity == 0: Function0 is expected -- SAM types do not eta-expand
because it could be an accidental SAM scala/bug#9489
- 2.14:
- for arity > 0: unconditional
- for arity == 0: a function-ish type of arity 0 is expected (including SAM)
warnings:
- 2.12: eta-expansion of zero-arg methods was deprecated (scala/bug#7187)
- 2.13: deprecation dropped in favor of setting the scene for uniform eta-expansion in 3.0
warning still available under -Xlint:eta-zero
- 2.14: expected type is a SAM that is not annotated with `@FunctionalInterface`
if it's a Java-defined interface (under `-Xlint:eta-sam`)
(4.2) condition for auto-application by -Xsource level:
- until 2.14: none (assuming condition for (4.3) was not met)
- in 3.0: `meth.isJavaDefined`
TODO decide -- currently the condition is more involved to give slack to
Scala methods overriding Java-defined ones; I think we should resolve that
by introducing slack in overriding e.g. a Java-defined `def toString()`
by a Scala-defined `def toString`. This also works better for dealing
with accessors overriding Java-defined methods.
The current strategy in methodSig is problematic:
> // Add a () parameter section if this overrides some method with () parameters
> val vparamSymssOrEmptyParamsFromOverride =
This means an accessor that overrides a Java-defined method gets a
MethodType instead of a `NullaryMethodType`, which breaks lots of
assumptions about accessors
Copy file name to clipboardExpand all lines: src/compiler/scala/tools/nsc/settings/Warnings.scala
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -109,6 +109,8 @@ trait Warnings {
109
109
valImplicitNotFound=LintWarning("implicit-not-found", "Check @implicitNotFound and @implicitAmbiguous messages.")
110
110
valSerial=LintWarning("serial", "@SerialVersionUID on traits and non-serializable classes.")
111
111
valValPattern=LintWarning("valpattern", "Enable pattern checks in val definitions.")
112
+
valEtaZero=LintWarning("eta-zero", "Warn on eta-expansion (rather than auto-application) of zero-ary method.")
113
+
valEtaSam=LintWarning("eta-sam", "Warn on eta-expansion to meet a Java-defined functional interface that is not explicitly annotated with @FunctionalInterface.")
if (doIt &&!expectingFunctionOfArity && settings.warnEtaSam) warnEtaSam()
956
+
957
+
doIt
913
958
}
914
-
// (4.3) eta-expand method value when function or sam type is expected (for experimentation, always eta-expand under 2.14 source level)
915
-
elseif (isFunctionProto(pt) || settings.isScala214) { // TODO: decide on `settings.isScala214`
916
-
if (settings.isScala212 && mt.params.isEmpty) // implies isFunctionType(pt)
917
-
currentRun.reporting.deprecationWarning(tree.pos, NoSymbol, "Eta-expansion of zero-argument methods is deprecated. "+
918
-
s"To avoid this warning, write ${Function(Nil, Apply(tree, Nil))}.", "2.12.0")
919
959
920
-
typedEtaExpansion(tree, mode, pt)
960
+
961
+
// (4.2) condition for auto-application by -Xsource level
962
+
//
963
+
// until 2.14: none (assuming condition for (4.3) was not met)
964
+
// in 3.0: `meth.isJavaDefined`
965
+
// (TODO decide -- currently the condition is more involved to give slack to Scala methods overriding Java-defined ones;
966
+
// I think we should resolve that by introducing slack in overriding e.g. a Java-defined `def toString()` by a Scala-defined `def toString`.
967
+
// This also works better for dealing with accessors overriding Java-defined methods. The current strategy in methodSig is problematic:
968
+
// > // Add a () parameter section if this overrides some method with () parameters
969
+
// > val vparamSymssOrEmptyParamsFromOverride =
970
+
// This means an accessor that overrides a Java-defined method gets a MethodType instead of a NullaryMethodType, which breaks lots of assumptions about accessors)
971
+
defcheckCanAutoApply():Boolean= {
972
+
if (sourceLevel2_14 &&!meth.isJavaDefined)
973
+
context.deprecationWarning(tree.pos, NoSymbol, s"Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method ${meth.decodedName},\n"+
974
+
s"or remove the empty argument list from its definition (Java-defined methods are exempt).\n"+
975
+
s"In Scala 3, an unapplied method like this will be eta-expanded into a function.", "2.14.0")
976
+
true
921
977
}
922
-
else cantAdapt
978
+
979
+
if (!meth.isConstructor && checkCanEtaExpand()) typedEtaExpansion(tree, mode, pt)
val t2: () => Any = m2 // error, no eta-expansion of zero-args methods
6
+
t7187-2.14.scala:14: warning: An unapplied 0-arity method was eta-expanded (due to the expected type () => Any), rather than applied to `()`.
7
+
Write m2() to invoke method m2, or change the expected type.
8
+
val t2: () => Any = m2 // eta-expanded with lint warning
9
+
^
10
+
t7187-2.14.scala:15: warning: An unapplied 0-arity method was eta-expanded (due to the expected type AcciSamZero, which is SAM-equivalent to () => Int), rather than applied to `()`.
11
+
Write m2() to invoke method m2, or change the expected type.
12
+
val t2AcciSam: AcciSamZero = m2 // eta-expanded with lint warning + sam warning
13
+
^
14
+
t7187-2.14.scala:15: warning: Eta-expansion performed to meet expected type AcciSamZero, which is SAM-equivalent to () => Int,
15
+
even though trait AcciSamZero is not annotated with `@FunctionalInterface`;
16
+
to suppress warning, add the annotation or write out the equivalent function literal.
17
+
val t2AcciSam: AcciSamZero = m2 // eta-expanded with lint warning + sam warning
18
+
^
19
+
t7187-2.14.scala:16: warning: An unapplied 0-arity method was eta-expanded (due to the expected type SamZero, which is SAM-equivalent to () => Int), rather than applied to `()`.
20
+
Write m2() to invoke method m2, or change the expected type.
21
+
val t2Sam: SamZero = m2 // eta-expanded with lint warning
0 commit comments