Skip to content

Commit

Permalink
Deprecate the splat operators in macro expressions (crystal-lang#13939)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <[email protected]>
  • Loading branch information
HertzDevil and straight-shoota authored Nov 7, 2023
1 parent e838701 commit 2173371
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 27 deletions.
6 changes: 1 addition & 5 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3145,11 +3145,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
{% operands = instruction[:operands] || [] of Nil %}

def {{name.id}}(
{% if operands.empty? %}
*, node : ASTNode?
{% else %}
{{*operands}}, *, node : ASTNode?
{% end %}
{{operands.splat(", ")}}*, node : ASTNode?
) : Nil
node = @node_override || node
@instructions.nodes[instructions_index] = node if node
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,14 @@ module Crystal
end

def visit(node : Splat)
warnings.add_warning(node, "Deprecated use of splat operator. Use `#splat` instead")
node.exp.accept self
@last = @last.interpret("splat", [] of ASTNode, nil, nil, self, node.location)
false
end

def visit(node : DoubleSplat)
warnings.add_warning(node, "Deprecated use of double splat operator. Use `#double_splat` instead")
node.exp.accept self
@last = @last.interpret("double_splat", [] of ASTNode, nil, nil, self, node.location)
false
Expand Down
20 changes: 10 additions & 10 deletions src/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,49 +82,49 @@ macro record(__name name, *properties, **kwargs)
{% end %}

def initialize({{
*properties.map do |field|
properties.map do |field|
"@#{field.id}".id
end
end.splat
}})
end

{{yield}}

def copy_with({{
*properties.map do |property|
properties.map do |property|
if property.is_a?(Assign)
"#{property.target.id} _#{property.target.id} = @#{property.target.id}".id
elsif property.is_a?(TypeDeclaration)
"#{property.var.id} _#{property.var.id} = @#{property.var.id}".id
else
"#{property.id} _#{property.id} = @#{property.id}".id
end
end
end.splat
}})
self.class.new({{
*properties.map do |property|
properties.map do |property|
if property.is_a?(Assign)
"_#{property.target.id}".id
elsif property.is_a?(TypeDeclaration)
"_#{property.var.id}".id
else
"_#{property.id}".id
end
end
end.splat
}})
end

def clone
self.class.new({{
*properties.map do |property|
properties.map do |property|
if property.is_a?(Assign)
"@#{property.target.id}.clone".id
elsif property.is_a?(TypeDeclaration)
"@#{property.var.id}.clone".id
else
"@#{property.id}.clone".id
end
end
end.splat
}})
end
end
Expand All @@ -150,7 +150,7 @@ macro pp!(*exps)
::print %prefix
::pp({{exp}})
{% else %}
%names = { {{*exps.map(&.stringify)}} }
%names = { {{exps.map(&.stringify).splat}} }
%max_size = %names.max_of &.size
{
{% for exp, i in exps %}
Expand Down Expand Up @@ -184,7 +184,7 @@ macro p!(*exps)
::print %prefix
::p({{exp}})
{% else %}
%names = { {{*exps.map(&.stringify)}} }
%names = { {{exps.map(&.stringify).splat}} }
%max_size = %names.max_of &.size
{
{% for exp, i in exps %}
Expand Down
6 changes: 3 additions & 3 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ class Object
# end
# ```
macro {{macro_prefix}}property!(*names)
{{macro_prefix}}getter! \{{*names}}
{{macro_prefix}}getter! \{{names.splat}}

\{% for name in names %}
\{% if name.is_a?(TypeDeclaration) %}
Expand Down Expand Up @@ -1377,8 +1377,8 @@ class Object
# end
# ```
macro def_equals_and_hash(*fields)
def_equals {{*fields}}
def_hash {{*fields}}
def_equals {{fields.splat}}
def_hash {{fields.splat}}
end

# Forwards missing methods to *delegate*.
Expand Down
2 changes: 1 addition & 1 deletion src/regex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class Regex
str.each_byte do |byte|
{% begin %}
case byte.unsafe_chr
when {{*SPECIAL_CHARACTERS}}
when {{SPECIAL_CHARACTERS.splat}}
result << '\\'
result.write_byte byte
else
Expand Down
2 changes: 1 addition & 1 deletion src/slice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Slice(T)
{% if @type.name != "Slice(T)" && T < Number %}
{{T}}.slice({{args.splat(", ")}}read_only: {{read_only}})
{% else %}
%ptr = Pointer(typeof({{*args}})).malloc({{args.size}})
%ptr = Pointer(typeof({{args.splat}})).malloc({{args.size}})
{% for arg, i in args %}
%ptr[{{i}}] = {{arg}}
{% end %}
Expand Down
2 changes: 1 addition & 1 deletion src/static_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct StaticArray(T, N)
# * `Number.static_array` is a convenient alternative for designating a
# specific numerical item type.
macro [](*args)
%array = uninitialized StaticArray(typeof({{*args}}), {{args.size}})
%array = uninitialized StaticArray(typeof({{args.splat}}), {{args.size}})
{% for arg, i in args %}
%array.to_unsafe[{{i}}] = {{arg}}
{% end %}
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/aarch64-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ module Syscall

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
def self.{{name.id}}({{*args}}) : {{return_type}}
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

{% if args.size == 0 %}
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/arm-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ module Syscall

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
def self.{{name.id}}({{*args}}) : {{return_type}}
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

{% if args.size == 0 %}
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/i386-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ module Syscall

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
def self.{{name.id}}({{*args}}) : {{return_type}}
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

{% if args.size == 0 %}
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/x86_64-linux.cr
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ module Syscall

macro def_syscall(name, return_type, *args)
@[AlwaysInline]
def self.{{name.id}}({{*args}}) : {{return_type}}
def self.{{name.id}}({{args.splat}}) : {{return_type}}
ret = uninitialized {{return_type}}

{% if args.size == 0 %}
Expand Down
2 changes: 1 addition & 1 deletion src/xml/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class XML::Builder
end

private macro call(name, *args)
ret = LibXML.xmlTextWriter{{name}}(@writer, {{*args}})
ret = LibXML.xmlTextWriter{{name}}(@writer, {{args.splat}})
check ret, {{@def.name.stringify}}
end

Expand Down
2 changes: 1 addition & 1 deletion src/yaml/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class YAML::Builder
end

private macro emit(event_name, *args)
LibYAML.yaml_{{event_name}}_event_initialize(pointerof(@event), {{*args}})
LibYAML.yaml_{{event_name}}_event_initialize(pointerof(@event), {{args.splat}})
yaml_emit({{event_name.stringify}})
end

Expand Down

0 comments on commit 2173371

Please sign in to comment.