Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another codegen error involving instance variables with Iterator type #14317

Open
HertzDevil opened this issue Feb 23, 2024 · 0 comments
Open

Comments

@HertzDevil
Copy link
Contributor

Taken from straight-shoota/crinja#70:

class Crinja
  module Object
  end

  alias Raw = Int32 | Crinja::Object | Array(Value)

  struct Value
    getter raw : Raw

    def initialize(@raw : Raw)
    end

    def raw_each
      case object = @raw
      when Iterable(Value)
        RawIterator.new(object.each)
      else
        raise RuntimeError.new("#{object.class} is not iterable")
      end
    end

    class RawIterator
      include Iterator(Raw)

      def initialize(@iterator : Iterator(Value))
      end

      def next
        case value = @iterator.next
        when Value
          value.raw
        else
          stop
        end
      end
    end
  end

  class Tuple
    include Indexable(Value)
    include Object

    def unsafe_fetch(index : Int)
      Crinja::Value.new(1)
    end

    def size
      1
    end
  end
end

struct StructWithIterator
  include Crinja::Object
  include ::Iterable(Crinja::Value)

  def each
    iter = uninitialized Iterator(Crinja::Value)
    iter
  end
end

# also crashes with an invalid memory access sometimes
Crinja::Value.new(Crinja::Tuple.new).raw_each.next # => #<Iterator::Stop:0x7f24db1b8fe0>

The above should print 1 instead. The error goes away if either include inside StructWithIterator is removed, or if StructWithIterator#each returns a more specific type like [Crinja::Value.new(1)].each. This might be related to #7044 or #10967.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant