Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Macros: undefined method `block' for nil:NilClass #185

Closed
jstepien opened this Issue · 4 comments

2 participants

@jstepien
$ cat Macros.mirah 
class Macros
  def initialize
    @foo = "It works."
  end

  macro def attr_reader(name_node)
    name = name_node.string_value
    quote {
      def `name`
        @`name`
      end
    }
  end

  attr_reader :foo
end

mirahc Macros.mirah with Mirah 0.0.11 and JRuby 1.6.7 works fine, however translating to Java causes a following error.

$ mirahc --java Macros.mirah 
Parsing...
  Macros.mirah
Inferring types...
Compiling...
  Macros.mirah
Macros.mirah:15: undefined method `block' for nil:NilClass
  attr_reader :foo
^^^^^^^^^^^^^^^^^^
Mirah::InternalCompilerError: undefined method `block' for nil:NilClass
             compile at mirah-0.0.11-java/lib/mirah/compiler/structure.rb:32
                send at org/jruby/RubyKernel.java:2084
  <Class:0x1de40494> at file:jruby-complete-1.6.7.jar!/META-INF/jruby.home/lib/ruby/1.8/delegate.rb:292
         maybe_store at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:351
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:357
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/base.rb:212
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:356
             compile at mirah-0.0.11-java/lib/mirah/compiler/structure.rb:21
         maybe_store at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:351
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:357
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/base.rb:212
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:356
             compile at mirah-0.0.11-java/lib/mirah/compiler/structure.rb:21
        define_class at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:65
                with at mirah-0.0.11-java/lib/mirah/jvm/compiler/base.rb:260
        define_class at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:61
             compile at mirah-0.0.11-java/lib/mirah/compiler/class.rb:20
         maybe_store at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:351
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:357
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/base.rb:212
                body at mirah-0.0.11-java/lib/mirah/jvm/compiler/java_source.rb:356
             compile at mirah-0.0.11-java/lib/mirah/compiler/structure.rb:21
         define_main at mirah-0.0.11-java/lib/mirah/jvm/compiler/base.rb:94
             compile at mirah-0.0.11-java/lib/mirah/compiler/structure.rb:50
         compile_ast at mirah-0.0.11-java/lib/mirah/compiler.rb:49
        compile_asts at mirah-0.0.11-java/lib/mirah/compiler.rb:40
                each at org/jruby/RubyArray.java:1615
        compile_asts at mirah-0.0.11-java/lib/mirah/compiler.rb:38
            generate at mirah-0.0.11-java/lib/mirah/generator.rb:38
             execute at mirah-0.0.11-java/lib/mirah/commands/compile.rb:26
        execute_base at mirah-0.0.11-java/lib/mirah/commands/base.rb:44
               catch at org/jruby/RubyKernel.java:1183
        execute_base at mirah-0.0.11-java/lib/mirah/commands/base.rb:42
             execute at mirah-0.0.11-java/lib/mirah/commands/compile.rb:23
             compile at mirah-0.0.11-java/lib/mirah.rb:40
              (root) at mirah-0.0.11-java/bin/mirahc:27
                load at org/jruby/RubyKernel.java:1058
              (root) at mirahc:19
@jstepien

The issue is present on current master, a86c365. Apparently the error is caused by calling @method.block in Mirah::JVM::Compiler::JavaSource#scoped_body with @method being nil.

@baroquebobcat

Thanks for the report, I'll look at it this weekend if someone else doesn't get to it.

@baroquebobcat

It appears to have something to do w/ how the AST is handled when there isn't an implicit main method. https://github.com/mirah/mirah/blob/master/test/jvm/macros_test.rb#L63 has the same error if you comment out the implicit main.

@baroquebobcat

Ah, maybe that's because when there's a main method, there's a @method in the generator the whole time.

@baroquebobcat baroquebobcat closed this issue from a commit
@baroquebobcat baroquebobcat fixes #185 ScopedBody's generated by macros outside methods should work
macros dump their contents into scoped bodies.
scoped bodies in the source generator were assumed to be only inside method definitions,
so they blew up in certain cases
6ec7338
@diosmosis diosmosis referenced this issue from a commit
@baroquebobcat baroquebobcat fixes #185 ScopedBody's generated by macros outside methods should work
macros dump their contents into scoped bodies.
scoped bodies in the source generator were assumed to be only inside method definitions,
so they blew up in certain cases
e31ed58
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.