tupl-tufts / rdl

Types, type checking, and contracts for Ruby
BSD 3-Clause "New" or "Revised" License
602 stars 38 forks source link

A reference to Module causes `uninitialized constant RDL::Typecheck::Bar` #77

Closed mame closed 5 years ago

mame commented 6 years ago
require "rdl"

module Foo
  extend RDL::Annotate

  module Bar
  end

  type "() -> %any", typecheck: :now
  def refer_bar
    Bar
  end
end
$ ruby t.rb
Traceback (most recent call last):
    10: from t.rb:3:in `<main>'

*snip*

     1: from /home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:636:in `tc'
/home/mame/local/lib/ruby/gems/2.5.0/gems/rdl-2.1.0/lib/rdl/typecheck.rb:636:in `const_get': uninitialized constant RDL::Typecheck::Bar (NameError)

This error occurs even if I replace Bar with Foo::Bar in refer_bar.

The following patch fixes this issue, but I'm unsure if this is a right way.

diff --git a/lib/rdl/typecheck.rb b/lib/rdl/typecheck.rb
index a1962ce..390fb77 100644
--- a/lib/rdl/typecheck.rb
+++ b/lib/rdl/typecheck.rb
@@ -630,11 +630,8 @@ module RDL::Typecheck
         raise "const other not implemented yet"
       end
       case c
-      when TrueClass, FalseClass, Complex, Rational, Integer, Float, Symbol, Class
+      when TrueClass, FalseClass, Complex, Rational, Integer, Float, Symbol, Class, Module
         [env, RDL::Type::SingletonType.new(c)]
-      when Module
-        t = RDL::Type::SingletonType.new(const_get(e.children[1]))
-        [env, t]
       else
         [env, RDL::Type::NominalType.new(const_get(e.children[1]).class)]
       end
owickstrom commented 6 years ago

I've hit this as well, and the proposed fix worked for me. :+1:

mckaz commented 5 years ago

This is fixed in dev, and will be merged with master shortly.