Skip to content

Commit 041199c

Browse files
committedJun 3, 2017
Fixed #4481: Nil assertion failed when using super outside method
1 parent a3b77d3 commit 041199c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed
 

‎spec/compiler/semantic/super_spec.cr

+9
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,13 @@ describe "Semantic: super" do
357357
),
358358
"undefined method 'Base#method()'"
359359
end
360+
361+
it "errors on super outside method (#4481)" do
362+
assert_error %(
363+
class Foo
364+
super
365+
end
366+
),
367+
"can't use 'super' outside method"
368+
end
360369
end

‎src/compiler/crystal/semantic/call.cr

+11-6
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ class Crystal::Call
551551
raise "there's no superclass in this scope"
552552
end
553553

554-
enclosing_def = enclosing_def()
554+
enclosing_def = enclosing_def("super")
555555

556556
# TODO: do this better
557557
lookup = enclosing_def.owner
@@ -594,7 +594,7 @@ class Crystal::Call
594594
end
595595

596596
def lookup_previous_def_matches(arg_types, named_args_types)
597-
enclosing_def = enclosing_def()
597+
enclosing_def = enclosing_def("previous_def")
598598

599599
previous_item = enclosing_def.previous
600600
unless previous_item
@@ -623,13 +623,18 @@ class Crystal::Call
623623
typed_defs
624624
end
625625

626-
def enclosing_def
626+
def enclosing_def(context)
627627
fun_literal_context = parent_visitor.fun_literal_context
628628
if fun_literal_context.is_a?(Def)
629-
fun_literal_context
630-
else
631-
parent_visitor.untyped_def
629+
return fun_literal_context
630+
end
631+
632+
untyped_def = parent_visitor.untyped_def?
633+
if untyped_def
634+
return untyped_def
632635
end
636+
637+
raise "can't use '#{context}' outside method"
633638
end
634639

635640
def on_new_subclass

0 commit comments

Comments
 (0)
Please sign in to comment.