Skip to content

Commit

Permalink
Fixed #4481: Nil assertion failed when using super outside method
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jun 3, 2017
1 parent a3b77d3 commit 041199c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 9 additions & 0 deletions spec/compiler/semantic/super_spec.cr
Expand Up @@ -357,4 +357,13 @@ describe "Semantic: super" do
),
"undefined method 'Base#method()'"
end

it "errors on super outside method (#4481)" do
assert_error %(
class Foo
super
end
),
"can't use 'super' outside method"
end
end
17 changes: 11 additions & 6 deletions src/compiler/crystal/semantic/call.cr
Expand Up @@ -551,7 +551,7 @@ class Crystal::Call
raise "there's no superclass in this scope"
end

enclosing_def = enclosing_def()
enclosing_def = enclosing_def("super")

# TODO: do this better
lookup = enclosing_def.owner
Expand Down Expand Up @@ -594,7 +594,7 @@ class Crystal::Call
end

def lookup_previous_def_matches(arg_types, named_args_types)
enclosing_def = enclosing_def()
enclosing_def = enclosing_def("previous_def")

previous_item = enclosing_def.previous
unless previous_item
Expand Down Expand Up @@ -623,13 +623,18 @@ class Crystal::Call
typed_defs
end

def enclosing_def
def enclosing_def(context)
fun_literal_context = parent_visitor.fun_literal_context
if fun_literal_context.is_a?(Def)
fun_literal_context
else
parent_visitor.untyped_def
return fun_literal_context
end

untyped_def = parent_visitor.untyped_def?
if untyped_def
return untyped_def
end

raise "can't use '#{context}' outside method"
end

def on_new_subclass
Expand Down

0 comments on commit 041199c

Please sign in to comment.