-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New offsetof expression #7589
New offsetof expression #7589
Conversation
src/compiler/crystal/tools/playground/public/vendor/codemirror-5.38.0/mode/crystal/crystal.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome pull request!
assert_error "module Foo; @a = 0; end; offsetof(Foo, @a)", "Foo is neither a class nor a struct, it's a module" | ||
end | ||
|
||
it "errors on offsetof uninstantiated generic type" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there should also be a successful example for offsetof(Foo(T), @a)
src/compiler/crystal/macros.cr
Outdated
end | ||
|
||
# Returns the name of the instance variable used in this `offsetof` expression as a MacroId. | ||
def member : MacroId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're using the name instance_variable
in other places, so this should follow.
src/compiler/crystal/tools/playground/public/vendor/codemirror-5.38.0/mode/crystal/crystal.js
Outdated
Show resolved
Hide resolved
5860357
to
89aa7cb
Compare
0e7c586
to
6c29df5
Compare
All specs, including the ones I just added, should be working now. I've also undone the changes to the codemirror file and renamed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
But I'd like to hear some thoughts about #7566 (comment)
@straight-shoota Sorry, didn't look into the issue once more. I'd definitely go with the |
|
||
instance_vars = type.all_instance_vars | ||
ivar_name = node.instance_var.as(InstanceVar).name | ||
ivar_index = instance_vars.keys.index(ivar_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of this can be replaced with type.index_of_instance_var(node.instance_var.as(InstanceVar).name)
(extracting node.instance_var.as(InstanceVar).name
to ivar_name
is fine)
elsif type && type.instance_type.devirtualize.class? | ||
expanded = NumberLiteral.new(@program.instance_offset_of(type.sizeof_type, ivar_index).to_s, :i32) | ||
expanded.type = @program.int32 | ||
node.expanded = expanded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only difference between these two branches is:
@program.offset_of(type.sizeof_type, ivar_index)
# and
program.instance_offset_of(type.sizeof_type, ivar_index)
we can assign that to a variable and do all of the expanded = ...; expanded.type = ...; node.expanded = ...
outside of the if
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved with some minor suggestions. Looks great, thanks!
ac43880
to
ba04e09
Compare
ba04e09
to
a8a502b
Compare
This adds a new
offsetof
expression to the language, as discussed in issue #7566.