Skip to content
This repository

VerifyError with member variables assigned to nil #177

Open
shepheb opened this Issue March 30, 2012 · 3 comments

4 participants

Braden Shepherdson Brendan Ribera ribrdb Nick Howard
Braden Shepherdson
Collaborator

This is a weakness of the type inferencer and results in a VerifyError. The following is a minimal test case that reproduces the issue:

package test;

class A
  def initialize
    @foo = nil
  end

  def foo:String
    @foo
  end
  def foo=(v:String)
    @foo = v
  end
end

package test;

class B
  a = A.new
  a.foo = "Test"
  puts(a.foo)
end

running mirah gives the VerifyError. Running mirahc and then decompiling with jd-gui, the cause becomes plain: foo is declared as private Object foo = null; not as a String.

If I get a chance to work on my secondary hackathon goal of improving the inferencer by making it global, hopefully it could solve this problem. I'm not sure if there's any fix other than making the inferencer smarter, but at least this bug will serve as documentation for others. Doing

def initialize
  @foo = ""
  @foo = nil
end

fixes the problem, but that may not be possible in all cases.

Brendan Ribera
Collaborator

You're right that this is less than ideal. The convention I use works pretty well; I'm not sure of cases where it breaks down:

def initialize
  @foo = String(nil)
end
ribrdb
Owner

I tried this in the newast branch, and strange things are going on. If I run mirah -V or mirahc without -V you'll get this at runtime:

Exception in thread "main" java.lang.NoSuchFieldError: foo
    at A.foo(broken.mirah:6)
    at Broken.main(broken.mirah:15)

But if you run mirahc -V or mirah without -V you get:

Inference Error:
examples/broken.mirah:6: Cannot assign Type(null) to #<StringType java.lang.String>
Nick Howard
Collaborator

The class B bit doesn't work, but if I change it to

class A
  def initialize
    @foo = nil
  end

  def foo:String
    @foo
  end
  def foo=(v:String)
    @foo = v
  end
end

a = A.new
a.foo = "Test"
puts(a.foo)

I get the same result as @ribrdb for mirahc, but mirah by itself is now the same as mirahc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.