Skip to content

Matches on type aliases for Unions generates incorrect warning #5494

Closed
@bishabosha

Description

@bishabosha

Valid for Dotty 0.23.0-RC1 as of 5/04/2020:

Minimised example

class A
class B

type AorB = A | B

def foo(any: Any) = any match {
  case aorb: AorB =>
    println("got AorB")
}
[warn] -- Warning: .../unions.scala:5:7 
[warn] 5 |      case aorb: AorB =>
[warn]   |           ^^^^^^^^^^
[warn]   |        the type test for AorB cannot be checked at runtime
[warn] one warning found

This warning is incorrect as the correct instanceof checks are generated in the class file:

 public void foo(java.lang.Object);
    descriptor: (Ljava/lang/Object;)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=5, args_size=2
         0: aload_1
         1: astore_2
         2: aload_2
         3: instanceof    #20                 // class example/A
         6: ifne          16
         9: aload_2
        10: instanceof    #22                 // class example/B
        13: ifeq          35
        16: aload_2
        17: checkcast     #4                  // class java/lang/Object
        20: astore_3
        21: aload_3
        22: astore        4
        24: getstatic     #27                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
        27: ldc           #40                 // String got AorB
        29: invokevirtual #32                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
        32: goto          46
        35: getstatic     #27                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
        38: ldc           #34                 // String got Any
        40: invokevirtual #32                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
        43: goto          46
        46: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
           22      13     4    ab   Ljava/lang/Object;
            0      47     0  this   Lexample/Demo$;
            0      47     1   foo   Ljava/lang/Object;
      LineNumberTable:
        line 19: 0
        line 20: 0
        line 22: 2
        line 23: 24
        line 25: 35
      StackMapTable: number_of_entries = 3
        frame_type = 252 /* append */
          offset_delta = 16
          locals = [ class java/lang/Object ]
        frame_type = 18 /* same */
        frame_type = 10 /* same */

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions