scala3
scala3 copied to clipboard
very obfuscated error when using `val` in for comprehension
It seems a legacy part of scala 2.13 syntax isn't rejected cleanly in Scala 3
Compiler version
3.5.1
Minimized example
def test = {
for {
x <- List(1,2,3)
// ^
// here is where the error is reported
val p = x * 3
// ^
// here is where the error *should be*
} yield p
}
Output Error/Warning message
[error] ./foo.scala:3:21
[error] pattern expected
[error] x <- List(1,2,3)
[error] ^
[error] ./foo.scala:4:18
[error] Found: (??? : => Nothing)
[error] Required: ?{ map: ? }
[error] Note that implicit conversions were not tried because the result of an implicit conversion
[error] must be more specific than ?{ map: [applied to (
[error] x$1 =>
[error] x$1 match
[error] {
[error] case _root_.scala.Predef.??? => p
[error] }
[error] ) returning IterableOnce[B]] }
Why this Error/Warning was not helpful
The message was unhelpful because the code snippet is not the problematic piece of code, and then there is an awful knock-on error.
Suggested improvement
It could be made more helpful by specifically looking ahead for val
and marking that as an unexpected keyword.
This seems to be some legacy that is still allowed in Scala 2.13, for example, I found this in the Mill codebase