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

User-defined annotations #6063

Merged
merged 16 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion spec/compiler/codegen/alias_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe "Code gen: alias" do
alias Foo = Moo
end
))
result.program.link_attributes
result.program.link_annotations
end

it "doesn't crash on cast to as recursive alias (#639)" do
Expand Down
21 changes: 21 additions & 0 deletions spec/compiler/codegen/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,27 @@ describe "Code gen: macro" do
)).to_i.should eq(10)
end

it "gets default value of instance variable of inherited type that also includes module" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instance_vars includes variable from module included in inherited type better describes the purpose of this spec?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. This is about the default value (that was the bug), and "default value" doesn't appear your suggested sentence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay. I figured the default value was only used as a vehicle to test instance_vars.

run(%(
module Moo
@moo = 10
end

class Foo
include Moo

def foo
{{ @type.instance_vars.first.default_value }}
end
end

class Bar < Foo
end

Bar.new.foo
)).to_i.should eq(10)
end

it "determines if variable has default value" do
run(%(
class Foo
Expand Down
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1118,4 +1118,7 @@ describe Crystal::Formatter do
assert_format "alias X = ((Y, Z) ->)"

assert_format "def x(@y = ->(z) {})\nend"

assert_format "class X; annotation FooAnnotation ; end ; end", "class X\n annotation FooAnnotation; end\nend"
assert_format "class X\n annotation FooAnnotation \n end \n end", "class X\n annotation FooAnnotation\n end\nend"
end
3 changes: 2 additions & 1 deletion spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ describe "Lexer" do
:begin, :lib, :fun, :type, :struct, :union, :enum, :macro, :out, :require,
:case, :when, :select, :then, :of, :abstract, :rescue, :ensure, :is_a?, :alias,
:pointerof, :sizeof, :instance_sizeof, :as, :as?, :typeof, :for, :in,
:with, :self, :super, :private, :protected, :asm, :uninitialized, :nil?]
:with, :self, :super, :private, :protected, :asm, :uninitialized, :nil?,
:annotation]
it_lexes_idents ["ident", "something", "with_underscores", "with_1", "foo?", "bar!", "fooBar",
"❨╯°□°❩╯︵┻━┻"]
it_lexes_idents ["def?", "if?", "else?", "elsif?", "end?", "true?", "false?", "class?", "while?",
Expand Down
12 changes: 12 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,18 @@ describe "macro methods" do
[TypeNode.new(program.reference)] of ASTNode
end
end

it "executes nilable? (false)" do
assert_macro("x", "{{x.nilable?}}", "false") do |program|
[TypeNode.new(program.string)] of ASTNode
end
end

it "executes nilable? (true)" do
assert_macro("x", "{{x.nilable?}}", "true") do |program|
[TypeNode.new(program.union_of(program.string, program.nil))] of ASTNode
end
end
end

describe "type declaration methods" do
Expand Down
21 changes: 13 additions & 8 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1125,15 +1125,16 @@ describe "Parser" do

it_parses "a = 1; class Foo; @x = a; end", [Assign.new("a".var, 1.int32), ClassDef.new("Foo".path, Assign.new("@x".instance_var, "a".call))]

it_parses "@[Foo]", Attribute.new("Foo")
it_parses "@[Foo()]", Attribute.new("Foo")
it_parses "@[Foo(1)]", Attribute.new("Foo", [1.int32] of ASTNode)
it_parses "@[Foo(\"hello\")]", Attribute.new("Foo", ["hello".string] of ASTNode)
it_parses "@[Foo(1, foo: 2)]", Attribute.new("Foo", [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo(1, foo: 2\n)]", Attribute.new("Foo", [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo(\n1, foo: 2\n)]", Attribute.new("Foo", [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo]", Annotation.new("Foo".path)
it_parses "@[Foo()]", Annotation.new("Foo".path)
it_parses "@[Foo(1)]", Annotation.new("Foo".path, [1.int32] of ASTNode)
it_parses "@[Foo(\"hello\")]", Annotation.new("Foo".path, ["hello".string] of ASTNode)
it_parses "@[Foo(1, foo: 2)]", Annotation.new("Foo".path, [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo(1, foo: 2\n)]", Annotation.new("Foo".path, [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo(\n1, foo: 2\n)]", Annotation.new("Foo".path, [1.int32] of ASTNode, [NamedArgument.new("foo", 2.int32)])
it_parses "@[Foo::Bar]", Annotation.new(Path.new(["Foo", "Bar"]))

it_parses "lib LibC\n@[Bar]; end", LibDef.new("LibC", Attribute.new("Bar"))
it_parses "lib LibC\n@[Bar]; end", LibDef.new("LibC", Annotation.new("Bar".path))

it_parses "Foo(_)", Generic.new("Foo".path, [Underscore.new] of ASTNode)

Expand Down Expand Up @@ -1679,6 +1680,10 @@ describe "Parser" do
assert_syntax_error "<<-HEREDOC", "Unexpected EOF on heredoc identifier"
assert_syntax_error "<<-HEREDOC\n", "Unterminated heredoc"

it_parses "annotation Foo; end", AnnotationDef.new("Foo".path)
it_parses "annotation Foo\n\nend", AnnotationDef.new("Foo".path)
it_parses "annotation Foo::Bar\n\nend", AnnotationDef.new(Path.new(["Foo", "Bar"]))

it "gets corrects of ~" do
node = Parser.parse("\n ~1")
loc = node.location.not_nil!
Expand Down
Loading