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 block type restriction to abstract def #15438

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2886,4 +2886,11 @@ describe Crystal::Formatter do
assert_format %(<<-'EOS'\n#{char}\nEOS)
end
end

describe "abstract def block type" do
assert_format "abstract def foo(& : Foo -> Bar)"
assert_format "abstract def foo(&block : Foo -> Bar)"
assert_format "abstract def foo(&)", "abstract def foo(& : -> _)"
assert_format "abstract def foo(&block)", "abstract def foo(&block : -> _)"
end
end
10 changes: 7 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1524,10 +1524,10 @@ module Crystal

def format_def_args(node : Def | Macro)
yields = node.is_a?(Def) && !node.block_arity.nil?
format_def_args node.args, node.block_arg, node.splat_index, false, node.double_splat, yields
format_def_args node, node.args, node.block_arg, node.splat_index, false, node.double_splat, yields
end

def format_def_args(args : Array, block_arg, splat_index, variadic, double_splat, yields)
def format_def_args(node, args : Array, block_arg, splat_index, variadic, double_splat, yields)
# If there are no args, remove extra "()"
if args.empty? && !block_arg && !double_splat && !variadic
if @token.type.op_lparen?
Expand Down Expand Up @@ -1594,6 +1594,10 @@ module Crystal

to_skip += 1 if at_skip?
block_arg.accept self

if block_arg.restriction.nil? && abstract_def?(node)
write " : -> _"
end
end
elsif yields
wrote_newline = format_def_arg(wrote_newline, false) do
Expand Down Expand Up @@ -1718,7 +1722,7 @@ module Crystal
end
end

format_def_args node.args, nil, nil, node.varargs?, nil, false
format_def_args node, node.args, nil, nil, node.varargs?, nil, false

if return_type = node.return_type
skip_space
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/event_loop/polling.cr
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ abstract class Crystal::EventLoop::Polling < Crystal::EventLoop
protected abstract def system_del(fd : Int32, closing = true) : Nil

# Identical to `#system_del` but yields on error.
protected abstract def system_del(fd : Int32, closing = true, &) : Nil
protected abstract def system_del(fd : Int32, closing = true, & : -> _) : Nil

# Arm a timer to interrupt a run at *time*. Set to `nil` to disarm the timer.
private abstract def system_set_timer(time : Time::Span?) : Nil
Expand Down
6 changes: 3 additions & 3 deletions src/crystal/syntax_highlighter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ abstract class Crystal::SyntaxHighlighter
abstract def render(type : TokenType, value : String)

# Renders a delimiter sequence.
abstract def render_delimiter(&)
abstract def render_delimiter(& : -> _)

# Renders an interpolation sequence.
abstract def render_interpolation(&)
abstract def render_interpolation(& : -> _)

# Renders a string array sequence.
abstract def render_string_array(&)
abstract def render_string_array(& : -> _)

# Describes the type of a highlighter token.
enum TokenType
Expand Down