scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

No type-mismatch error in fewer-braces code when using context function

Open WojciechMazur opened this issue 8 months ago • 0 comments

Compiler version

3.4.2

Minimized example

trait Zone
object Zone:
  def apply[T](f: Zone ?=> T): T = ???

def foo(str: String)(using Zone): String = ???

def test: Int = Zone:
  val x = foo("bar")

Output Error/Warning message

[error] -- [E172] Type Error: /Users/wmazur/projects/scala-native/sandbox/src/main/scala/Test.scala:12:20 
[error] 12 |  val x = foo("bar")
[error]    |                    ^
[error]    |No given instance of type Zone was found for parameter x$2 of method foo
[error] one error found
[error] (sandbox3 / Compile / compileIncremental) Compilation failed

Why this Error/Warning was not helpful

There is no information about type-mismatch error that causes actual compilation error. Replacing fewer-braces block with braces

def test: Int = Zone {
  val x = foo("bar")
}

Produces 2 error messages:

[error] -- [E172] Type Error: /Users/wmazur/projects/scala-native/sandbox/src/main/scala/Test.scala:8:20 
[error] 8 |  val x = foo("bar")
[error]   |                    ^
[error]   |  No given instance of type Zone was found for parameter x$2 of method foo
[error] -- [E007] Type Mismatch Error: /Users/wmazur/projects/scala-native/sandbox/src/main/scala/Test.scala:9:1 
[error] 9 |}
[error]   | ^
[error]   | Found:    Unit
[error]   | Required: Int
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] two errors found

Suggested improvement

It should either report both messages or report only type mismatch error.

Case 2

Removing local variable makes the code report only missing implicit, even when using braces:

def test: Int = Zone {
  foo("bar")
}
[error] -- [E172] Type Error: /Users/wmazur/projects/scala-native/sandbox/src/main/scala/Test.scala:8:12 
[error] 8 |  foo("bar")
[error]   |            ^
[error]   |  No given instance of type Zone was found for parameter x$2 of method foo
[error] one error found

WojciechMazur avatar Jun 13 '24 08:06 WojciechMazur