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

Don't use {{yield}} outside a macro #5307

Merged
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
4 changes: 4 additions & 0 deletions spec/compiler/macro/macro_expander_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,8 @@ describe "MacroExpander" do
it "does not expand when macro expression is {% ... %}" do
assert_macro "", %({% 1 %}), [] of ASTNode, ""
end

it "can't use `yield` outside a macro" do
assert_error %({{yield}}), "can't use `{{yield}}` outside a macro"
end
end
11 changes: 8 additions & 3 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Crystal
getter last : ASTNode
property free_vars : Hash(String, TypeVar)?

def self.new(program, scope : Type, path_lookup : Type, a_macro : Macro, call, a_def : Def? = nil)
def self.new(program, scope : Type, path_lookup : Type, a_macro : Macro, call, a_def : Def? = nil, in_macro = false)
vars = {} of String => ASTNode
splat_index = a_macro.splat_index
double_splat = a_macro.double_splat
Expand Down Expand Up @@ -68,14 +68,15 @@ module Crystal
vars[macro_block_arg.name] = call_block || Nop.new
end

new(program, scope, path_lookup, a_macro.location, vars, call.block, a_def)
new(program, scope, path_lookup, a_macro.location, vars, call.block, a_def, in_macro)
end

record MacroVarKey, name : String, exps : Array(ASTNode)?

def initialize(@program : Program,
@scope : Type, @path_lookup : Type, @location : Location?,
@vars = {} of String => ASTNode, @block : Block? = nil, @def : Def? = nil)
@vars = {} of String => ASTNode, @block : Block? = nil, @def : Def? = nil,
@in_macro = false)
@str = IO::Memory.new(512) # Can't be String::Builder because of `{{debug()}}
@last = Nop.new
end
Expand Down Expand Up @@ -362,6 +363,10 @@ module Crystal
end

def visit(node : Yield)
unless @in_macro
node.raise "can't use `{{yield}}` outside a macro"
end

if block = @block
if node.exps.empty?
@last = block.body.clone
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/macros/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class Crystal::Program
end

def expand_macro(a_macro : Macro, call : Call, scope : Type, path_lookup : Type? = nil, a_def : Def? = nil)
interpreter = MacroInterpreter.new self, scope, path_lookup || scope, a_macro, call, a_def
interpreter = MacroInterpreter.new self, scope, path_lookup || scope, a_macro, call, a_def, in_macro: true
a_macro.body.accept interpreter
interpreter.to_s
end

def expand_macro(node : ASTNode, scope : Type, path_lookup : Type? = nil, free_vars = nil, a_def : Def? = nil)
interpreter = MacroInterpreter.new self, scope, path_lookup || scope, node.location, def: a_def
interpreter = MacroInterpreter.new self, scope, path_lookup || scope, node.location, def: a_def, in_macro: false
interpreter.free_vars = free_vars
node.accept interpreter
interpreter.to_s
Expand Down