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

Add Crystal::Macros::ProcNotation#resolve and #resolve? #11373

Merged
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
18 changes: 18 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,24 @@ module Crystal
it "gets empty output" do
assert_macro %({{x.output}}), "nil", {x: ProcNotation.new([Path.new("SomeType")] of ASTNode)}
end

it "executes resolve" do
assert_macro %({{x.resolve}}), "Proc(Int32, String)", {x: ProcNotation.new(([Path.new("Int32")] of ASTNode), Path.new("String"))}

assert_macro_error(%({{x.resolve}}), "undefined constant Foo") do
{x: ProcNotation.new(([Path.new("Foo")] of ASTNode))}
end

assert_macro_error(%({{x.resolve}}), "undefined constant Foo") do
{x: ProcNotation.new(([] of ASTNode), Path.new("Foo"))}
end
end

it "executes resolve?" do
assert_macro %({{x.resolve?}}), "Proc(Int32, String)", {x: ProcNotation.new(([Path.new("Int32")] of ASTNode), Path.new("String"))}
assert_macro %({{x.resolve?}}), "nil", {x: ProcNotation.new(([Path.new("Foo")] of ASTNode))}
assert_macro %({{x.resolve?}}), "nil", {x: ProcNotation.new(([] of ASTNode), Path.new("Foo"))}
end
end

describe "proc literal methods" do
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,16 @@ module Crystal::Macros
# Returns the output type, or nil if there is no return type.
def output : ASTNode | NilLiteral
end

# Resolves this proc notation to a `TypeNode` if it denotes a type,
# or otherwise gives a compile-time error.
def resolve : ASTNode
end

# Resolves this proc notation to a `TypeNode` if it denotes a type,
# or otherwise returns a `NilLiteral`.
def resolve? : ASTNode | NilLiteral
end
end

# A method definition.
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,12 @@ module Crystal
end
end

def resolve(node : Generic)
def resolve(node : Generic | ProcNotation)
type = @path_lookup.lookup_type(node, self_type: @scope, free_vars: @free_vars)
TypeNode.new(type)
end

def resolve?(node : Generic)
def resolve?(node : Generic | ProcNotation)
resolve(node)
rescue Crystal::CodeError
nil
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,10 @@ module Crystal
interpret_check_args { ArrayLiteral.new(@inputs || [] of ASTNode) }
when "output"
interpret_check_args { @output || NilLiteral.new }
when "resolve"
interpret_check_args { interpreter.resolve(self) }
when "resolve?"
interpret_check_args { interpreter.resolve?(self) || NilLiteral.new }
else
super
end
Expand Down