Skip to content

feat: allow taking pointerof(self) in structs #15903

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,6 @@ module Crystal
assert_syntax_error "case when .foo? then 1; end"
assert_syntax_error "macro foo;{%end};end"
assert_syntax_error "foo {1, 2}", %(unexpected token: ",")
assert_syntax_error "pointerof(self)", "can't take address of self"
assert_syntax_error "def foo 1; end"

assert_syntax_error %<{"x": [] of Int32,\n}\n1.foo(>, "unterminated call", 3, 6
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,14 @@ module Crystal
def pointerof_var(node)
case exp = node.exp
when Var
if exp.name == "self"
exp.accept self

unless exp.type.struct?
node.raise "can only take address of self in struct"
end
end

meta_var = @meta_vars[exp.name]
meta_var.assigned_to = true
meta_var
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5919,10 +5919,6 @@ module Crystal
check :OP_LPAREN
next_token_skip_space_or_newline

if @token.keyword?(:self)
raise "can't take address of self", @token.line_number, @token.column_number
end

exp = parse_op_assign
skip_space_or_newline

Expand Down