-
Notifications
You must be signed in to change notification settings - Fork 1.1k
space following unary minus produces misleading 'then expected' error message #13410
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
Comments
You can use This isn't due to optional braces but to quiet control syntax, which detects the operator as "leading infix" and therefore a continuation of the conditional. The doc says misleadingly that the parens may be omitted if there is a then; since there is no then, it's reasonable to expect it to compile. Edit: TIL "inferred then", which is why the leading infix test was added. Edit: TIL inferred then was removed last year. The leading infix test should also be removed, so it will always look ahead to check if a My comment on the ticket says I started writing |
Thanks for the suggestion, and the explanation! |
Not sure if this is related, but the following code fails unless the last line is preceded by a blank line:' Source file object Main:
def func(a: Int, b: Int) : Int =
val (lo,hi) = if (a > b) (b,a) else (a,b)
- (hi - lo)/2 3 compile errors reported: $ scalac3 -classpath /opt/uejlib3/vastblue_3.jar func.sc
-- [E008] Not Found Error: func.sc:4:4 -----------------------------------------------------------------------------------------------------------------------------------------------------------------
3 | val (lo,hi) = if (a > b) (b,a) else (a,b)
4 | - (hi - lo)/2
| ^
| value - is not a member of (Int, Int).
| Note that `-` is treated as an infix operator in Scala 3.
| If you do not want that, insert a `;` or empty line in front
| or drop any spaces behind the operator.
-- [E045] Cyclic Error: func.sc:3:4 --------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 | val (lo,hi) = if (a > b) (b,a) else (a,b)
| ^
| Recursive value $1$ needs type
longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: func.sc:4:17 ------------------------------------------------------------------------------------------------------------------------------------------------------------
4 | - (hi - lo)/2
| ^
| Found: Unit
| Required: Int
longer explanation available when compiling with `-explain`
3 errors found This version compiles with no errors: object IntFunc:
def func(a: Int, b: Int) : Int =
val (lo,hi) = if (a > b) (b,a) else (a,b)
- (hi - lo)/2
Another fix is to remove the space following the minus sign on the last line: object IntFunc:
def func(a: Int, b: Int) : Int =
val (lo,hi) = if (a > b) (b,a) else (a,b)
-(hi - lo)/2 |
It's not related. It is in fact intentional that |
Probably unintended to reopen. |
Yes indeed. |
Compiler version
Minimized example
Output
Expectation
The scala2 compiler compiles without error, and recognizes the expression
- 1
asnegative one
.The scala3 compiler seems to interpret the minus sign as Boolean operator?, resulting in an invalid boolean expression.
Perhaps the "optional braces" feature mandates that unary minus tokenization rules must be tightened relative to scala2. If so, it might be worth some effort to improve the error message here.
This particular example is based on code generated automatically by Intellij when converting a java ternary operator to scala. I'm not sure why it inserted a space after the minus sign, but it's valid scala2. There might be a fair amount of scala2 code out there that will lead to this compile error.
The text was updated successfully, but these errors were encountered: