mirah / mirah

The Mirah Programming Language
http://mirah.org
Apache License 2.0
864 stars 62 forks source link

Mirah compiler infers wrong type bound of type variable. #417

Open felixvf opened 8 years ago

felixvf commented 8 years ago

Consider this Java code

public class ClassWithSelfReferencingTypeParameter<P extends ClassWithSelfReferencingTypeParameter<P>> {

    P self;

    @SuppressWarnings("unchecked")
    public ClassWithSelfReferencingTypeParameter() {
        this.self = (P) this;
    }

    public P foo() {
        return self;
    }

    public P bar() {
        return self;
    }

    public void baz() {
        System.out.println("baz");
    }
}

and consider this Mirah code

ClassWithSelfReferencingTypeParameter.new.foo.bar.baz

Then it is expected that his code should compiles and prints "baz\n".

However, what is actually observed is this compiler error:

ERROR: Can't find method java.lang.Object.bar()
uujava commented 8 years ago

I'm not quite sure, as my stream a bit offset from master, but this fix possible brakes some cases with generics inference. Non trivial real life example below: infering/xx_type_fixture.java

package infering;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class xx_type_fixture {
    public CompletableFuture<List<Runnable>> load(List filter, int flags){
        return null;
    }
}

infering/xx_type_invoker_test.mirah

package infering

import java.util.function.BiConsumer
import java.util.Map
import java.util.List

class xx_type_invoker_test

   def initialize(filters:List, flags:int)
      @filters = filters
      @flags = flags
      @loader = xx_type_fixture.new
      @future = nil
   end

   def run():void
       @future = @loader.load(@filters, @flags)
   end

   def handle(block:BiConsumer):xx_type_invoker_test
      @future.whenComplete(block)
      self
   end

   def join():void
      @future.join
   end

end

With 417 fix it rise NPE in @future.whenComplete(block) while infering