Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Improve the error message when calling unary with parentheses #17637

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

Closed
cacapouh opened this issue Apr 22, 2023 · 2 comments
Closed

Improve the error message when calling unary with parentheses #17637

cacapouh opened this issue Apr 22, 2023 · 2 comments

Comments

@cacapouh
Copy link

Improve the error message to something like 'Do not put parentheses on unary methods', for the compilation error mentioned in the following issue.

#17325

@cacapouh cacapouh changed the title Improve the error message when calling unary without parentheses Improve the error message when calling unary with parentheses Apr 22, 2023
@som-snytt
Copy link
Contributor

I would expect that I can "elevate" warnings to errors and see the underlying problem.

But:

scalac -d /tmp -feature -Werror i17325.scala

says

-- [E100] Syntax Error: i17325.scala:12:4 ------------------------------------------------------------------------------
12 |    ~(0.5) // compilation error
   |    ^^^^^^
   |    method unary_~ in class DoubleOps must be called with () argument
   |
   | longer explanation available when compiling with `-explain`
-- Error: i17325.scala:7:15 --------------------------------------------------------------------------------------------
7 |  implicit def doubleOps(d: Double): DoubleOps = new DoubleOps(
  |               ^
  |               Definition of implicit conversion method doubleOps should be enabled
  |               by adding the import clause 'import scala.language.implicitConversions'
  |               or by setting the compiler option -language:implicitConversions.
  |               See the Scala docs for value scala.language.implicitConversions for a discussion
  |               why the feature should be explicitly enabled.
2 errors found

The expected warning is not boosted to error.

That is, for the text

@main
def main() = {
  class DoubleOps(d: Double) {
    def unary_~(): Double = scala.math.ceil(d)
  }
  implicit def doubleOps(d: Double): DoubleOps = new DoubleOps(
    d
  )

  println {
    //~(0.5) // compilation error
    "hi"
  }
}

and

➜  scalac -d /tmp -feature -Werror -language:implicitConversions i17325.scala
-- Error: i17325.scala:5:8 ---------------------------------------------------------------------------------------------
5 |    def unary_~(): Double = scala.math.ceil(d)
  |        ^
  |        unary_<op> method cannot take empty parameter list.
  |
  |        Possible fix: remove the `()` arguments.
1 error found

The "meta" issue is that I should be able to ask the compiler to tell me everything it knows about my code. That might include warnings or also open tickets.

@som-snytt
Copy link
Contributor

Actually Scala 2 is the converse because "empty apply" is only a warning. So you see both warnings, or you see one error.

➜  scalac -d /tmp -Xlint ti17325.scala
ti17325.scala:6: warning: unary prefix operator definition with empty parameter list is deprecated: instead, remove () to declare as `def unary_~ : Double = scala.math.ceil(d)`
    def unary_~(): Double = scala.math.ceil(d)
        ^
ti17325.scala:12: warning: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method unary_~,
or remove the empty argument list from its definition (Java-defined methods are exempt).
In Scala 3, an unapplied method like this will be eta-expanded into a function.
      ~(0.5) // compilation error
      ^
2 warnings
➜  scalac -d /tmp -Xlint -Xsource:3 ti17325.scala
ti17325.scala:6: error: unary prefix operator definition with empty parameter list is unsupported: instead, remove () to declare as `def unary_~ : Double = scala.math.ceil(d)`
    def unary_~(): Double = scala.math.ceil(d)
        ^
1 error

@ckipp01 ckipp01 transferred this issue from lampepfl/dotty-feature-requests May 31, 2023
@scala scala locked and limited conversation to collaborators May 31, 2023
@ckipp01 ckipp01 converted this issue into discussion #17638 May 31, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants