Skip to content

Commit

Permalink
Showing 3 changed files with 23 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/compiler/codegen/macro_spec.cr
Original file line number Diff line number Diff line change
@@ -1342,4 +1342,19 @@ describe "Code gen: macro" do
{1, 3}.foo(1)
)).to_i.should eq(2)
end

it "implicitly marks method as macro def when using @type" do
run(%(
class Foo
def method
{{@type.stringify}}
end
end
class Bar < Foo
end
(Bar.new as Foo).method
)).to_string.should eq("Bar")
end
end
1 change: 1 addition & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
@@ -694,6 +694,7 @@ describe "Parser" do
it_parses "macro def foo : String; 1; end", Def.new("foo", body: 1.int32, return_type: "String".path, macro_def: true)
it_parses "macro def foo(x) : String; 1; end", Def.new("foo", ["x".arg], 1.int32, return_type: "String".path, macro_def: true)
it_parses "macro def foo; 1; end", Def.new("foo", body: 1.int32, macro_def: true)
it_parses "def foo;{{@type}};end", Def.new("foo", body: Expressions.from([MacroExpression.new("@type".instance_var)] of ASTNode), macro_def: true)

it_parses "macro foo;bar{% begin %}body{% end %}baz;end", Macro.new("foo", [] of Arg, Expressions.from(["bar".macro_literal, MacroIf.new(true.bool, "body".macro_literal), "baz;".macro_literal] of ASTNode))

8 changes: 7 additions & 1 deletion src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ module Crystal
@calls_initialize = false
@calls_previous_def = false
@uses_block_arg = false
@is_macro_def = false
@assigns_special_var = false
@def_nest = 0
@type_nest = 0
@@ -1089,6 +1090,9 @@ module Crystal
when :CONST
parse_ident_or_literal
when :INSTANCE_VAR
if @in_macro_expression && @token.value == "@type"
@is_macro_def = true
end
new_node_check_type_declaration InstanceVar
when :CLASS_VAR
new_node_check_type_declaration ClassVar
@@ -2458,6 +2462,7 @@ module Crystal
@uses_block_arg = false
@assigns_special_var = false
@block_arg_name = nil
@is_macro_def = false
a_def
end

@@ -2468,6 +2473,7 @@ module Crystal
@uses_block_arg = false
@block_arg_name = nil
@assigns_special_var = false
@is_macro_def = false
end

def parse_macro
@@ -3048,7 +3054,7 @@ module Crystal
@doc_enabled = !!@wants_doc
pop_def

node = Def.new name, args, body, receiver, block_arg, return_type, is_macro_def, @yields, is_abstract, splat_index, double_splat: double_splat
node = Def.new name, args, body, receiver, block_arg, return_type, (is_macro_def || @is_macro_def), @yields, is_abstract, splat_index, double_splat: double_splat
node.name_column_number = name_column_number
set_visibility node
node.end_location = end_location

0 comments on commit 29b0056

Please sign in to comment.