Closed
Description
The pattern match on the lhs side doesn't exhaustively check if the pattern is of valid type for singletons of union.
In this example both 240
and 4H
are not a valid subtype of both TimeframeN
and Timeframe
respectively, but compiler appears to compile without reporting type mismatch errors. It appears as if union type is widen'ed prematurely.
Compiler version
3.0.1-RC1
Minimized code
import scala.language.implicitConversions
type Timeframe = "1m" | "2m" | "1H"
type TimeframeN = 1 | 2 | 60
def manualConvertToN(tf: Timeframe): TimeframeN = tf match
case "1m" => 1
case "2m" => 2
case "1H" => 60
case "4H" => ??? // incorrect but compiles
given Conversion[Timeframe, TimeframeN] =
case "1m" => 1
case "2m" => 2
case "1H" => 60
case "4H" => ??? // incorrect but compiles
given Conversion[TimeframeN, Timeframe] =
case 1 => "1m"
case 2 => "2m"
case 60 => "1H"
case 240 => ??? // incorrect but compiles
Output
No errors on compilation
Expectation
Type mismatch errors. Hypothetical sample:
[error] -- [E007] Type Mismatch Error: /Users/swoorup.joshi/playground/test/src/main/scala/Main.scala:18:15
[error] 18 | case "4H" => ???
[error] | ^^^
[error] | Found: ("4H" : String)
[error] | Required: Timeframe
[error] -- [E007] Type Mismatch Error: /Users/swoorup.joshi/playground/test/src/main/scala/Main.scala:24:15
[error] 24 | case "4H" => ???
[error] | ^^^
[error] | Found: ("4H" : String)
[error] | Required: Timeframe
[error] -- [E007] Type Mismatch Error: /Users/swoorup.joshi/playground/test/src/main/scala/Main.scala:30:15
[error] 30 | case 240 => ???
[error] | ^^^
[error] | Found: (240 : Int)
[error] | Required: TimeframeN
Note that this doesn't happen if the returning subtype is invalid. In such cases, compiler reports a type mismatch error correctly.