Skip to content
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

Merged
merged 8 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename OffsetOf instance variables
  • Loading branch information
malte-v authored and Malte Voos committed Mar 26, 2019
commit 6c29df555cb5e3965f3bf46db0670cc8cc0f053a
8 changes: 4 additions & 4 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1383,12 +1383,12 @@ module Crystal
end

describe "offsetof methods" do
it "executes structure" do
assert_macro "x", %({{x.structure}}), [OffsetOf.new("SomeType".path, "@some_ivar".instance_var)] of ASTNode, "SomeType"
it "executes offsetof_type" do
assert_macro "x", %({{x.offsetof_type}}), [OffsetOf.new("SomeType".path, "@some_ivar".instance_var)] of ASTNode, "SomeType"
end

it "executes member" do
assert_macro "x", %({{x.member}}), [OffsetOf.new("SomeType".path, "@some_ivar".instance_var)] of ASTNode, "@some_ivar"
it "executes instance_var" do
assert_macro "x", %({{x.instance_var}}), [OffsetOf.new("SomeType".path, "@some_ivar".instance_var)] of ASTNode, "@some_ivar"
end
end

Expand Down
70 changes: 35 additions & 35 deletions spec/compiler/semantic/offsetof_spec.cr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
require "../../spec_helper"
describe "Semantic: offsetof" do
it "types offsetof" do
assert_type("offsetof(String, @length)") { int32 }
end
it "can be used with generic types" do
assert_type("struct Foo(T); @a : T = 0; end; offsetof(Foo(Int32), @a)") { int32 }
end
it "can be used with classes" do
assert_type("class Foo; @a = 0; end; offsetof(Foo, @a)") { int32 }
end
it "errors on undefined instance variable" do
assert_error "struct Foo; @a = 0; end; offsetof(Foo, @b)", "type Foo doesn't have an instance variable called @b"
end
it "errors on typeof inside offsetof expression" do
assert_error "struct Foo; @a = 0; end; foo = Foo.new; offsetof(typeof(foo), @a)", "can't use typeof inside offsetof expression"
end
it "gives error if using offsetof on something that can't have instance variables" do
assert_error "offsetof(Int32, @foo)", "type Int32 can't have instance variables"
end
it "gives error if using offsetof on something that's neither a class nor a struct" do
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 element of uninstantiated generic type" do
assert_error "struct Foo(T); @a = 0; end; offsetof(Foo, @a)", "can't take offsetof element @a of uninstantiated generic type Foo(T)"
end
end
require "../../spec_helper"

describe "Semantic: offsetof" do
it "types offsetof" do
assert_type("offsetof(String, @length)") { int32 }
end

it "can be used with generic types" do
assert_type("struct Foo(T); @a : T = 0; end; offsetof(Foo(Int32), @a)") { int32 }
end

it "can be used with classes" do
assert_type("class Foo; @a = 0; end; offsetof(Foo, @a)") { int32 }
end

it "errors on undefined instance variable" do
assert_error "struct Foo; @a = 0; end; offsetof(Foo, @b)", "type Foo doesn't have an instance variable called @b"
end

it "errors on typeof inside offsetof expression" do
assert_error "struct Foo; @a = 0; end; foo = Foo.new; offsetof(typeof(foo), @a)", "can't use typeof inside offsetof expression"
end

it "gives error if using offsetof on something that can't have instance variables" do
assert_error "offsetof(Int32, @foo)", "type Int32 can't have instance variables"
end

it "gives error if using offsetof on something that's neither a class nor a struct" do
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 element of uninstantiated generic type" do
assert_error "struct Foo(T); @a = 0; end; offsetof(Foo, @a)", "can't take offsetof element @a of uninstantiated generic type Foo(T)"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
var keywords = wordRegExp([
"abstract", "alias", "as", "asm", "begin", "break", "case", "class", "def", "do",
"else", "elsif", "end", "ensure", "enum", "extend", "for", "fun", "if",
"include", "instance_sizeof", "lib", "macro", "module", "next", "of", "offsetof", "out", "pointerof",
"include", "instance_sizeof", "lib", "macro", "module", "next", "of", "out", "pointerof",
"private", "protected", "rescue", "return", "require", "select", "sizeof", "struct",
"super", "then", "type", "typeof", "uninitialized", "union", "unless", "until", "when", "while", "with",
"yield", "__DIR__", "__END_LINE__", "__FILE__", "__LINE__"
Expand Down