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

Error on TypeNode#instance_vars, #has_inner_pointers? macros in top-level scope #15293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 43 additions & 3 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1785,9 +1785,30 @@ module Crystal
end
end

it "executes instance_vars" do
assert_macro("{{x.instance_vars.map &.stringify}}", %(["bytesize", "length", "c"])) do |program|
{x: TypeNode.new(program.string)}
describe "#instance_vars" do
it "executes instance_vars" do
assert_macro("{{x.instance_vars.map &.stringify}}", %(["bytesize", "length", "c"])) do |program|
{x: TypeNode.new(program.string)}
end
end

it "errors when called from top-level scope" do
assert_error <<-CRYSTAL, "`TypeNode#instance_vars` cannot be called in the top-level scope: instance vars are not yet initialized"
class Foo
end
{{ Foo.instance_vars }}
CRYSTAL
end

it "does not error when called from def scope" do
assert_type <<-CRYSTAL {|program| program.string }
module Moo
end
def moo
{{ Moo.instance_vars.stringify }}
end
moo
CRYSTAL
end
end

Expand Down Expand Up @@ -2485,6 +2506,25 @@ module Crystal
{x: TypeNode.new(program.proc_of(program.void))}
end
end

it "errors when called from top-level scope" do
assert_error <<-CRYSTAL, "`TypeNode#has_inner_pointers?` cannot be called in the top-level scope: instance vars are not yet initialized"
class Foo
end
{{ Foo.has_inner_pointers? }}
CRYSTAL
end

it "does not error when called from def scope" do
assert_type <<-CRYSTAL {|program| program.bool }
module Moo
end
def moo
{{ Moo.has_inner_pointers? }}
end
moo
CRYSTAL
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ end
def prepare_macro_call(macro_body, flags = nil, &)
program = new_program
program.flags.concat(flags.split) if flags
program.top_level_semantic_complete = true
args = yield program

macro_params = args.try &.keys.join(", ")
Expand Down
25 changes: 22 additions & 3 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ module Crystal
when "type_vars"
interpret_check_args { TypeNode.type_vars(type) }
when "instance_vars"
interpret_check_args { TypeNode.instance_vars(type) }
interpret_check_args { TypeNode.instance_vars(type, name_loc) }
when "class_vars"
interpret_check_args { TypeNode.class_vars(type) }
when "ancestors"
Expand Down Expand Up @@ -2021,7 +2021,7 @@ module Crystal
end
end
when "has_inner_pointers?"
interpret_check_args { BoolLiteral.new(type.has_inner_pointers?) }
interpret_check_args { TypeNode.has_inner_pointers?(type, name_loc) }
else
super
end
Expand Down Expand Up @@ -2077,8 +2077,15 @@ module Crystal
end
end

def self.instance_vars(type)
def self.instance_vars(type, name_loc)
if type.is_a?(InstanceVarContainer)
unless type.program.top_level_semantic_complete?
if name_loc
raise Crystal::TypeException.new("`TypeNode#instance_vars` cannot be called in the top-level scope: instance vars are not yet initialized", name_loc)
else
raise Crystal::TypeException.new("`TypeNode#instance_vars` cannot be called in the top-level scope: instance vars are not yet initialized")
end
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
end
ArrayLiteral.map(type.all_instance_vars) do |name, ivar|
meta_var = MetaMacroVar.new(name[1..-1], ivar.type)
meta_var.var = ivar
Expand All @@ -2090,6 +2097,18 @@ module Crystal
end
end

def self.has_inner_pointers?(type, name_loc)
unless type.program.top_level_semantic_complete?
if name_loc
raise Crystal::TypeException.new("`TypeNode#has_inner_pointers?` cannot be called in the top-level scope: instance vars are not yet initialized", name_loc)
else
raise Crystal::TypeException.new("`TypeNode#has_inner_pointers?` cannot be called in the top-level scope: instance vars are not yet initialized")
end
end

BoolLiteral.new(type.has_inner_pointers?)
end

def self.class_vars(type)
if type.is_a?(ClassVarContainer)
ArrayLiteral.map(type.all_class_vars) do |name, ivar|
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/crystal/semantic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ class Crystal::Program
end
end

self.top_level_semantic_complete = true

{node, processor}
end

# This property indicates that the compiler has finished the top-level semantic
# stage.
# At this point, instance variables are declared and macros `#instance_vars`
# and `#has_internal_pointers?` provide meaningful information.
#
# FIXME: Introduce a more generic method to track progress of compiler stages
# (potential synergy with `ProcessTracker`?).
property? top_level_semantic_complete = false
end
Loading