Scala Programming Language
  1. Scala Programming Language
  2. SI-7015

Using a null in a context that expect something other than null causes redundant bytecode

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.11.0
    • Component/s: None
    • Labels:
      None

      Description

      class X {
        def f(x: String) = x
      
        f(null)
      }
      

      Produces the icode

        def g(): String {
        locals: 
        startBlock: 1
        blocks: [1]
        
        1: 
          7	THIS(X)
          7	CONSTANT(null)
          ?	DROP REF(class Null)
          ?	CONSTANT(null)
          7	CALL_METHOD X.f (dynamic)
          7	RETURN(REF(class String))
          
        }

      Note the CONSTANT(null) DROP REF(class Null) CONSTANT(NULL)

      And that generates the bytecode

      public java.lang.String g();
        Code:
         0:	aload_0
         1:	aconst_null
         2:	pop
         3:	aconst_null
         4:	invokevirtual	#18; //Method f:(Ljava/lang/String;)Ljava/lang/String;
         7:	areturn
      

      Again, the redundant push null, pop, push null sequence.

        Issue Links

          Activity

          Show
          James Iry added a comment - https://github.com/scala/scala/pull/2147
          Hide
          Miguel Garcia added a comment -

          For the example

          class X {
          
            def f(x: String) = x
          
            def g { f(null) }
          
          }
          

          GenBCode emits:

            public void g();
              flags: ACC_PUBLIC
          
              Code:
                stack=2, locals=1, args_size=1
                   0: aload_0       
                   1: aconst_null   
                   2: invokevirtual #13                 // Method f:(Ljava/lang/String;)Ljava/lang/String;
                   5: pop           
                   6: return        
          

          Details at http://magarciaepfl.github.com/scala/

          Show
          Miguel Garcia added a comment - For the example class X { def f(x: String) = x def g { f(null) } } GenBCode emits: public void g(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: aconst_null 2: invokevirtual #13 // Method f:(Ljava/lang/String;)Ljava/lang/String; 5: pop 6: return Details at http://magarciaepfl.github.com/scala/
          Hide
          James Iry added a comment -

          With this fix, so does GenICode

          Show
          James Iry added a comment - With this fix, so does GenICode

            People

            • Assignee:
              James Iry
              Reporter:
              James Iry
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development