Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Under -Xsource:3, warn when eta-expanding SAMs #8941

Merged
merged 1 commit into from May 6, 2020

Conversation

dwijnand
Copy link
Member

@dwijnand dwijnand commented Apr 30, 2020

Dotty emits the following:

-- Warning: target/tests/EtaX/EtaX.meth1/EtaX.meth1/EtaX.meth1.01.scala:10:24 --
10 |  val t5b: Sam1S      = meth1                   // ok, but warning
   |                        ^^^^^
   |method meth1 is eta-expanded even though Sam1S does not have the @FunctionalInterface annotation.
1 warning found

(given the following source:)

trait Sam1S { def apply(x: Any): Any }

class Test {
  def meth1(x: Any) = ""
  val t5b: Sam1S      = meth1                   // ok, but warning
}

So it makes sense for Scala 2.13 to do the same, under -Xsource:3.

Now it actually also emits in Dotty under -source 3.0-migration (which used to be called -language:Scala2Compat), so there's an argument that it should be on by default in 2.13... But let's do -Xsource:3 at the very least.

@scala-jenkins scala-jenkins added this to the 2.13.3 milestone Apr 30, 2020
@joroKr21
Copy link
Member

joroKr21 commented May 1, 2020

I've never seen anyone add @FunctionalInterface to Scala traits.

@joroKr21
Copy link
Member

joroKr21 commented May 1, 2020

But I have seen a lot of code use this feature, perhaps courtesy of Intellij inspection suggesting it.

@joroKr21
Copy link
Member

joroKr21 commented May 1, 2020

Or maybe I got this wrong - is this about SAM without @FunctionalInterface in general or only about eta-expansion? If we define the function explicitly there will be no warning?

@dwijnand
Copy link
Member Author

dwijnand commented May 1, 2020

Hey, @joroKr21. Yeah, that's right: this is only about eta-expanding methods where a SAM is expected. I don't know the history around @FunctionalInterface but I think a call was made that those will continue to eta-expand cleanly, while others eventually (somewhere in 3.x) won't.

Dotty emits the following:

    -- Warning: target/tests/EtaX/EtaX.meth1/EtaX.meth1/EtaX.meth1.01.scala:10:24 --
    10 |  val t5b: Sam1S      = meth1                   // ok, but warning
       |                        ^^^^^
       |method meth1 is eta-expanded even though Sam1S does not have the @FunctionalInterface annotation.
    1 warning found

(given the following source:)

    trait Sam1S { def apply(x: Any): Any }

    class Test {
      def meth1(x: Any) = ""
      val t5b: Sam1S      = meth1                   // ok, but warning
    }

So it makes sense for Scala 2.13 to do the same, under `-Xsource:3`.

Now it actually also emits under `-source 3.0-migration` (which used to
be called `-language:Scala2Compat`), so there's an argument that it
should be on by default in 2.13...  But let's do `-Xsource:3` at the
very least.
@lrytz lrytz merged commit b53b5f3 into scala:2.13.x May 6, 2020
@dwijnand dwijnand deleted the fast-deprecate-eta-sam branch May 6, 2020 09:39
@SethTisue SethTisue added the release-notes worth highlighting in next release notes label May 6, 2020
@smarter
Copy link
Member

smarter commented May 12, 2020

I don't know the history around @FunctionalInterface but I think a call was made that those will continue to eta-expand cleanly, while others eventually (somewhere in 3.x) won't.

I believe we'll keep the current behavior forever, the warning is there to prevent confusion and is easily solvable by making the eta-expansion explicit, see discussion in scala/scala3#4364

@neko-kai
Copy link
Contributor

@smarter @dwijnand Then the error message should mention explicit expansion first as it's totally confusing as to what's the purpose of the warning otherwise.

@dwijnand
Copy link
Member Author

Are you saying the "write out the equivalent function literal" doesn't convey "explicit expansion" and/or that it's too late in the message?

I'd be happy if we were to tweak the messaging. Want to send a PR with what you have in mind would work better?

@neko-kai
Copy link
Contributor

neko-kai commented May 13, 2020

@dwijnand The former, "write out the equivalent function literal" is extremely heavy and doesn't give any hint that appending underscore f _ will fix the issue - by contrast the error messages when automatic eta-expansion cannot be performed due to overloading give a very clear guide - 'add underscope and it will compile'.

Want to send a PR with what you have in mind would work better?

I am strictly against this warning in its entirety, both in Scala 2 and in Dotty, i'll give an example below why. This particular change makes -Xsource:3.0 come with a large amount of un-disableable warnings, which is regrettable if i need to access the changes there such as fixed contravariant typeclass resolution.

I use eta-SAM syntax mostly to concisely implement type classes, e.g.:

trait Encoder[A] {
  def encode(a: A): Json
}
object Encoder {
  implicit val jsonEncoder: Encoder[Json] = identity
}

I don't find the linter complaints very relevant in this case, neither do i find the original example in scala/scala3#4364 (comment) surprising or justifying the warning, so I'm simply opposed to the warning.

@SethTisue SethTisue changed the title Fast-track emitting "eta-sam" warnings Under -Xsource:3, warn when eta-expanding SAMs Jun 23, 2020
@SethTisue SethTisue removed the release-notes worth highlighting in next release notes label Jun 23, 2020
@SethTisue
Copy link
Member

@dwijnand We're release-noting #9049, so we don't need to also release-note this one... right?

@dwijnand
Copy link
Member Author

Yep, these are back to error again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants