Skip to content

Commit 96a9074

Browse files
committed
closures in closures fixes: 155
1 parent 77f110f commit 96a9074

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/mirah/ast/structure.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def prepare(typer, method)
145145
mirah = typer.transformer
146146
interface = method.argument_types[-1]
147147
outer_class = scope.defining_class
148-
148+
149149
binding = scope.binding_type(mirah)
150150

151151
name = "#{outer_class.name}$#{mirah.tmp}"
@@ -157,6 +157,8 @@ def prepare(typer, method)
157157
mirah.eval("@binding = binding", '-', c, 'binding')
158158
end
159159

160+
@defining_class = klass.static_scope.self_type
161+
160162
# TODO We need a special scope here that allows access to the
161163
# outer class.
162164
static_scope.self_type = typer.infer(klass, true)
@@ -174,6 +176,15 @@ def prepare(typer, method)
174176
typer.infer(instance, true)
175177
end
176178

179+
def defining_class
180+
@defining_class
181+
end
182+
183+
# TODO extract this & matching methods into a module
184+
def binding_type(mirah=nil)
185+
static_scope.binding_type(defining_class, mirah)
186+
end
187+
177188
def add_methods(klass, binding, typer)
178189
method_definitions = body.select{ |node| node.kind_of? MethodDefinition }
179190

@@ -183,8 +194,10 @@ def add_methods(klass, binding, typer)
183194
# TODO warn if there are non method definition nodes
184195
# they won't be used at all currently--so it'd be nice to note that.
185196
method_definitions.each do |node|
197+
186198
node.static_scope = static_scope
187199
node.binding_type = binding
200+
# node.children.each {|child| child.instance_variable_set '@scope', nil }
188201
klass.append_node(node)
189202
end
190203
end

test/jvm/blocks_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,20 @@ def foo c:Callable
243243
end
244244
end
245245

246+
def test_closure_in_closure_doesnt_raise_error
247+
parse_and_type(<<-CODE)
248+
interface Bar do;def run:void;end;end
249+
250+
class Foo
251+
def foo(a:Bar)
252+
1
253+
end
254+
end
255+
Foo.new.foo do
256+
Foo.new.foo do
257+
1
258+
end
259+
end
260+
CODE
261+
end
246262
end

0 commit comments

Comments
 (0)