Skip to content

Commit

Permalink
Can't use self in toplevel method (#5227)
Browse files Browse the repository at this point in the history
Related issue #5225
  • Loading branch information
makenowjust authored and RX14 committed Nov 2, 2017
1 parent bf3dd80 commit 7acedf8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/semantic/def_spec.cr
Expand Up @@ -476,4 +476,14 @@ describe "Semantic: def" do
foo(1)
)) { int32.metaclass }
end

it "can't use self in toplevel method" do
assert_error %(
def foo
self
end
foo
), "there's no self in this scope"
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/main_visitor.cr
Expand Up @@ -341,6 +341,10 @@ module Crystal
def visit(node : Var)
var = @vars[node.name]?
if var
if var.type?.is_a?(Program) && node.name == "self"
node.raise "there's no self in this scope"
end

meta_var = @meta_vars[node.name]
check_closured meta_var

Expand Down

0 comments on commit 7acedf8

Please sign in to comment.